Skip to content

Commit

Permalink
Merge pull request #125 from MauricioRobayo/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
MauricioRobayo authored Jul 14, 2021
2 parents ef02cb9 + 0a2e611 commit e98bb88
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 113 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cSpell.words": ["unidad", "vigenciadesde", "vigenciahasta"]
}
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ The `TrmApi` class provides five methods: [`latest()`](#latest), [`between(optio
```js
const TrmApi = require('trm-api').default;

const trmapi = new TrmApi();
const trmApi = new TrmApi();
```

### ES6 Modules

```js
import TrmApi from 'trm-api';

const trmapi = new TrmApi();
const trmApi = new TrmApi();
```

### TypeScript
Expand All @@ -50,7 +50,7 @@ With an app token, your application is guaranteed access to it's own pool of req
You can pass your app token in the constructor:

```js
const trmapi = new TrmApi('YOUR-APP-TOKEN-HERE');
const trmApi = new TrmApi('YOUR-APP-TOKEN-HERE');
```

### Methods
Expand All @@ -66,7 +66,7 @@ const trmapi = new TrmApi('YOUR-APP-TOKEN-HERE');
Provides the most recent quote:

```js
trmapi
trmApi
.latest()
.then((data) => console.log(data))
.catch((error) => console.log(error));
Expand Down Expand Up @@ -96,7 +96,7 @@ The `options` argument is an object with the following fields:
| order? | Optional | Can be `'ASC'` or `'DESC'`. Defaults to `'ASC'`. |

```js
trmapi.trmapi
trmApi
.between({ startAt: '2020-07-02', endAt: '2020-07-7', order: 'DESC' })
.then((data) => console.log(data))
.catch((error) => console.log(error));
Expand Down Expand Up @@ -145,7 +145,7 @@ The `options` optional argument is an object with the following fields:
| limit? | Optional | Maximum number of results to return. Defaults to 1,000. Maximum of 50,000. |

```js
trmapi
trmApi
.history({ limit: 30 })
.then((data) => console.log(data))
.catch((error) => console.log(error));
Expand All @@ -156,8 +156,8 @@ trmapi
Returns the TRM for an specific date given in `YYYY-MM-DD` format.

```js
trmapi
.date('2020-08-10')
trmApi
.date('2020-08-09')
.then((data) => console.log(data))
.catch((error) => console.log(error));
```
Expand All @@ -166,10 +166,10 @@ The response is a single object with the information for the given date:

```js
{
valor: '3792.98',
valor: '3769.67',
unidad: 'COP',
vigenciadesde: '2020-08-05T00:00:00.000',
vigenciahasta: '2020-08-05T00:00:00.000'
vigenciadesde: '2020-08-07T00:00:00.000',
vigenciahasta: '2020-08-10T00:00:00.000'
}
```

Expand All @@ -180,7 +180,7 @@ This method allows you to use any custom [`SoQL query`](https://dev.socrata.com/
For example:

```js
const data = await trmapi.query(
const data = await trmApi.query(
"SELECT valor, vigenciadesde WHERE valor >= 4150 AND vigenciadesde < '2020-08-01'"
);
```
Expand Down
51 changes: 0 additions & 51 deletions __mocks__/axios.ts

This file was deleted.

6 changes: 3 additions & 3 deletions bin/trm-api
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const TrmApi = require(`${__dirname}/../dist/trm-api`).default;

const trmapi = new TrmApi();
const trmApi = new TrmApi();

function errorHandler(error) {
if (error.response) {
Expand All @@ -21,12 +21,12 @@ function errorHandler(error) {
}

if (process.argv.length > 2) {
trmapi
trmApi
.date(process.argv[2])
.then(({ valor }) => console.log(valor))
.catch(errorHandler);
} else {
trmapi
trmApi
.latest()
.then(({ valor }) => console.log(valor))
.catch(errorHandler);
Expand Down
123 changes: 76 additions & 47 deletions src/trm-api.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,59 @@
import axios from 'axios';
import axios, { AxiosResponse } from 'axios';
import TrmApi from './trm-api';

const mockData = [
{
valor: '643.42',
unidad: 'COP',
vigenciadesde: '1991-12-02T00:00:00.000',
vigenciahasta: '1991-12-02T00:00:00.000',
},
{
valor: '639.22',
unidad: 'COP',
vigenciadesde: '1991-12-03T00:00:00.000',
vigenciahasta: '1991-12-03T00:00:00.000',
},
{
valor: '635.70',
unidad: 'COP',
vigenciadesde: '1991-12-04T00:00:00.000',
vigenciahasta: '1991-12-04T00:00:00.000',
},
{
valor: '631.51',
unidad: 'COP',
vigenciadesde: '1991-12-05T00:00:00.000',
vigenciahasta: '1991-12-05T00:00:00.000',
},
{
valor: '627.16',
unidad: 'COP',
vigenciadesde: '1991-12-06T00:00:00.000',
vigenciahasta: '1991-12-06T00:00:00.000',
},
];

const axiosResponse: AxiosResponse = {
data: mockData,
status: 200,
statusText: 'OK',
config: {},
headers: {},
};

const mockAxiosGet = jest.spyOn(axios, 'get').mockResolvedValue(axiosResponse);

afterEach(() => {
jest.clearAllMocks();
});

it('should call the API to get the latest data', async () => {
const trmapi = new TrmApi();
const data = await trmapi.latest();
expect(
axios.get,
).toBeCalledWith(
const trmApi = new TrmApi();
const data = await trmApi.latest();
expect(mockAxiosGet).toBeCalledWith(
'https://www.datos.gov.co/resource/32sa-8pi3.json?%24limit=1&%24order=vigenciahasta+DESC',
{ headers: {} },
{ headers: {} }
);
expect(data).toMatchObject({
valor: expect.any(String),
Expand All @@ -23,68 +64,58 @@ it('should call the API to get the latest data', async () => {
});

it('should call the API to get the data between two dates sorted ASC by default', async () => {
const trmapi = new TrmApi();
const data = await trmapi.between({
const trmApi = new TrmApi();
const data = await trmApi.between({
startAt: '1991-12-02',
endAt: '1991-12-02',
});
expect(
axios.get,
).toBeCalledWith(
expect(mockAxiosGet).toBeCalledWith(
'https://www.datos.gov.co/resource/32sa-8pi3.json?%24where=%28vigenciadesde+%3C%3D+%271991-12-02%27+AND+vigenciahasta+%3E%3D+%271991-12-02%27%29+OR+%28vigenciadesde+%3E%3D+%271991-12-02%27+AND+vigenciahasta+%3C%3D+%271991-12-02%27%29+OR+%28vigenciadesde+%3C%3D+%271991-12-02%27+AND+vigenciahasta+%3E%3D+%271991-12-02%27%29&%24order=vigenciadesde+ASC',
{ headers: {} },
{ headers: {} }
);
expect(Array.isArray(data)).toBe(true);
});

it('should call the API to get the data between two dates sorted DESC', async () => {
const trmapi = new TrmApi();
const data = await trmapi.between({
const trmApi = new TrmApi();
const data = await trmApi.between({
startAt: '1991-12-02',
endAt: '1991-12-02',
order: 'DESC',
});
expect(
axios.get,
).toBeCalledWith(
expect(mockAxiosGet).toBeCalledWith(
'https://www.datos.gov.co/resource/32sa-8pi3.json?%24where=%28vigenciadesde+%3C%3D+%271991-12-02%27+AND+vigenciahasta+%3E%3D+%271991-12-02%27%29+OR+%28vigenciadesde+%3E%3D+%271991-12-02%27+AND+vigenciahasta+%3C%3D+%271991-12-02%27%29+OR+%28vigenciadesde+%3C%3D+%271991-12-02%27+AND+vigenciahasta+%3E%3D+%271991-12-02%27%29&%24order=vigenciadesde+DESC',
{ headers: {} },
{ headers: {} }
);
expect(Array.isArray(data)).toBe(true);
});

it('should call the API to get the historic data limited to 1000 and sorted ASC by default', async () => {
const trmapi = new TrmApi();
const data = await trmapi.history();
expect(
axios.get,
).toBeCalledWith(
const trmApi = new TrmApi();
const data = await trmApi.history();
expect(mockAxiosGet).toBeCalledWith(
'https://www.datos.gov.co/resource/32sa-8pi3.json?%24limit=1000&%24order=vigenciadesde+ASC',
{ headers: {} },
{ headers: {} }
);
expect(Array.isArray(data)).toBe(true);
});

it('should call the API to get the historic data limited to 1000 and sorted ASC by default', async () => {
const trmapi = new TrmApi();
const data = await trmapi.history({ limit: 10, order: 'DESC' });
expect(
axios.get,
).toBeCalledWith(
const trmApi = new TrmApi();
const data = await trmApi.history({ limit: 10, order: 'DESC' });
expect(mockAxiosGet).toBeCalledWith(
'https://www.datos.gov.co/resource/32sa-8pi3.json?%24limit=10&%24order=vigenciadesde+DESC',
{ headers: {} },
{ headers: {} }
);
expect(Array.isArray(data)).toBe(true);
});

it('should call the API for a given date', async () => {
const trmapi = new TrmApi();
const data = await trmapi.date('1991-12-02');
expect(
axios.get,
).toBeCalledWith(
const trmApi = new TrmApi();
const data = await trmApi.date('1991-12-02');
expect(mockAxiosGet).toBeCalledWith(
'https://www.datos.gov.co/resource/32sa-8pi3.json?%24where=%28vigenciadesde+%3C%3D+%271991-12-02%27+AND+vigenciahasta+%3E%3D+%271991-12-02%27%29+OR+%28vigenciadesde+%3E%3D+%271991-12-02%27+AND+vigenciahasta+%3C%3D+%271991-12-02%27%29+OR+%28vigenciadesde+%3C%3D+%271991-12-02%27+AND+vigenciahasta+%3E%3D+%271991-12-02%27%29&%24order=vigenciadesde+ASC',
{ headers: {} },
{ headers: {} }
);
expect(data).toMatchObject({
valor: expect.any(String),
Expand All @@ -95,23 +126,21 @@ it('should call the API for a given date', async () => {
});

it('should call the API with a given query', async () => {
const trmapi = new TrmApi();
const data = await trmapi.query(
"SELECT valor, vigenciadesde WHERE valor >= 4150 AND vigenciadesde < '2020-08-01'",
const trmApi = new TrmApi();
const data = await trmApi.query(
"SELECT valor, vigenciadesde WHERE valor >= 4150 AND vigenciadesde < '2020-08-01'"
);
expect(
axios.get,
).toBeCalledWith(
expect(mockAxiosGet).toBeCalledWith(
'https://www.datos.gov.co/resource/32sa-8pi3.json?%24query=SELECT+valor%2C+vigenciadesde+WHERE+valor+%3E%3D+4150+AND+vigenciadesde+%3C+%272020-08-01%27',
{ headers: {} },
{ headers: {} }
);
expect(Array.isArray(data)).toBe(true);
});

it('should call the API with an app token', async () => {
const trmapi = new TrmApi('1234');
await trmapi.date('1991-12-02');
expect(axios.get).toBeCalledWith(expect.anything(), {
const trmApi = new TrmApi('1234');
await trmApi.date('1991-12-02');
expect(mockAxiosGet).toBeCalledWith(expect.anything(), {
headers: { 'X-App-Token': '1234' },
});
});

0 comments on commit e98bb88

Please sign in to comment.