Skip to content

Commit

Permalink
added UF operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose Bovet committed Jul 30, 2015
1 parent 6602dcb commit 0f3905c
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/main/groovy/cl/kleedy/sbif/api/BasicOperations.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class BasicOperations {
* @param includeDay :true
* @return url
*/
private String buildDate(Map params = [:], boolean includeDay = true) {
private static String buildDate(Map params = [:], boolean includeDay = true) {
def url = ''
def day = '', month = '', year = '', year2 = '', month2 = '', period = ''
def instance = Calendar.getInstance();
Expand Down
2 changes: 1 addition & 1 deletion src/main/groovy/cl/kleedy/sbif/api/EuroOperations.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ interface EuroOperations {
* @param year
* @return <Euro>
*/
def List<Euro> getEuroLaterYear(int year);
def List<Euro> getEuroLaterYear(int year) throws SBIFClientException;

/***
* Permite obtener un listado con el valor del Euro para los años y meses siguientes se indique.
Expand Down
29 changes: 8 additions & 21 deletions src/main/groovy/cl/kleedy/sbif/api/SBIFTemplate.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class SBIFTemplate implements SBIF {

EuroOperations euroOperations

UFOperations ufOperations

RESTClient restClient

String apiKey
Expand Down Expand Up @@ -91,26 +93,11 @@ class SBIFTemplate implements SBIF {
* inicializa las operaciones permitidas con SBIF
*/
private void initSubApis() {
dolarOperations = new DolarOperationTemplate(getRestClient(), getApiKey())
tmcOperations = new TMCOperationTemplate(getRestClient(), getApiKey())
ipcOperations = new IPCOperationTemplate(getRestClient(), getApiKey())
utmOperations = new UTMOperationTemplate(getRestClient(), getApiKey())
euroOperations = new EuroOperationTemplate(getRestClient(), getApiKey())
}

/***
*
* @return RESTClient
*/
RESTClient getRestClient() {
return restClient
}

/***
*
* @return apiKey
*/
String getApiKey() {
return apiKey
dolarOperations = new DolarOperationTemplate(restClient, apiKey)
tmcOperations = new TMCOperationTemplate(restClient, apiKey)
ipcOperations = new IPCOperationTemplate(restClient, apiKey)
utmOperations = new UTMOperationTemplate(restClient, apiKey)
euroOperations = new EuroOperationTemplate(restClient, apiKey)
ufOperations = new UFOperationTemplate(restClient, apiKey)
}
}
71 changes: 71 additions & 0 deletions src/main/groovy/cl/kleedy/sbif/api/UFOperations.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cl.kleedy.sbif.api

import cl.kleedy.sbif.api.indicadores.UF

/**
* Operaciones con la unidad de fomento
* Created by josebovet on 7/30/15.
*/
interface UFOperations {

/***
* Permite obtener el valor de la UF para el día actual.
* @return
* @throws SBIFClientException
*/
def UF getUF() throws SBIFClientException;

/***
* Permite obtener un listado con el valor de la UF para cada día del año que se indique.
* @param year
* @return
* @throws SBIFClientException
*/
def List<UF> getUFByYear(int year) throws SBIFClientException;

/***
* Permite obtener un listado con el valor de la UF para cada día del mes y año que se indique.
* @param year
* @param month
* @return
* @throws SBIFClientException
*/
def List<UF> getUFByYear(int year, int month) throws SBIFClientException;

/***
* Permite obtener el valor de la UF para el día del mes y año que se indique.
* @param year
* @param month
* @param day
* @return
* @throws SBIFClientException
*/
def UF getUFByYear(int year, int month, int day) throws SBIFClientException;

/***
* Permite obtener un listado con el valor de la UF para una fecha posterior al año que se indique.
* @param year
* @return
* @throws SBIFClientException
*/
def List<UF> getUFLaterYear(int year) throws SBIFClientException;

/***
* Permite obtener un listado con el valor de la UF para una fecha posterior al mes y año que se indique.
* @param year
* @param month
* @return
* @throws SBIFClientException
*/
def List<UF> getUFLaterYear(int year, int month) throws SBIFClientException;

/***
* Permite obtener el valor de la UF para una fecha posterior al año que se indique.
* @param year
* @param month
* @param day
* @return UF
* @throws SBIFClientException
*/
def UF getUFLaterYear(int year, int month, int day) throws SBIFClientException;
}
87 changes: 87 additions & 0 deletions src/main/groovy/cl/kleedy/sbif/api/impl/UFOperationTemplate.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package cl.kleedy.sbif.api.impl

import cl.kleedy.sbif.api.BasicOperations
import cl.kleedy.sbif.api.SBIFClientException
import cl.kleedy.sbif.api.UFOperations
import cl.kleedy.sbif.api.indicadores.UF
import wslite.rest.RESTClient

/**
* Implemetacion operaciones UF
* Created by josebovet on 7/30/15.
*/
class UFOperationTemplate extends BasicOperations implements UFOperations {

UFOperationTemplate(RESTClient restClient, String apiKey) {
super(restClient, apiKey)
}

@Override
UF getUF() throws SBIFClientException {
return getResource([:], 'uf', 'getUFs').first()
}

@Override
List<UF> getUFByYear(int year) throws SBIFClientException {
return getUFByDate(year,0,0)
}

@Override
List<UF> getUFByYear(int year, int month) throws SBIFClientException {
return getUFByDate(year, month,0)
}

@Override
UF getUFByYear(int year, int month, int day) throws SBIFClientException {
return getUFByDate(year,month,day).first()
}

private List<UF> getUFByDate(int year, int month, int day){
def params = ['year': year]
if (month) params.month = month
if (day) params.day = day
return getResource(params, 'uf', 'getUFs')
}

@Override
List<UF> getUFLaterYear(int year) throws SBIFClientException {
return getUFLaterYear(year,0)
}

@Override
List<UF> getUFLaterYear(int year, int month) throws SBIFClientException {
return getUFLaterByDate(year, month,0)
}

@Override
UF getUFLaterYear(int year, int month, int day) throws SBIFClientException {
return getUFLaterByDate(year, month, day).first()
}

private List<UF> getUFLaterByDate(int year, int month, int day) throws SBIFClientException {
def params = ['year': year]
if (month) params.month = month
if (day) params.day = day
return getResource(params, 'uf','/posteriores','getUFs')
}

/***
* Llama al recurso UF, parseando la respuesta en formato json
* @param url
* @return List<UF>
*/
private List<UF> getUFs(String url) throws SBIFClientException {
def ufList = call(url).json.UFs
def list = []
ufList.each() { data ->
def v = data.Valor.trim()
if (v.any { it == ',' }) {
v = v.substring(0, v.indexOf(','))
}
//TODO revisar tipo de mascara para Double
v = (v != null && v.length() > 0) ? Double.parseDouble(v.replace(".", '')) : 0
list << new UF(valor: v, fecha: Date.parse('yyyy-MM-dd', data.Fecha))
}
return list
}
}
7 changes: 7 additions & 0 deletions src/main/groovy/cl/kleedy/sbif/api/indicadores/UF.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cl.kleedy.sbif.api.indicadores

/**
* Created by josebovet on 7/30/15.
*/
class UF extends Indicador {
}
71 changes: 71 additions & 0 deletions src/test/groovy/cl/kleedy/sbif/api/UFClientSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cl.kleedy.sbif.api

/**
* Created by josebovet on 7/30/15.
*/
class UFClientSpec extends SBIFClientSpec {

UFOperations client

void setup() {
client = sbifTemplate.getUfOperations()
}

void "should retrieve today UF"() {
when:
def uf = client.getUF()
uf.valor >= 24000

then:
uf.fecha != null
}

void "should retrieve UF list from 2015"() {
when:
def ufList = client.getUFByYear(2015)

then:
ufList.size() >= 0
}

void "should retrieve UF list from 2015-05"() {
when:
def ufList = client.getUFByYear(2015,5)

then:
ufList.size() == 31
}

void "should retrieve UF list from 2015-5-5"() {
when:
def uf = client.getUFByYear(2015,5,5)

then:
uf.valor >= 24779.0
}

void "should retrieve UF list later from 2014"() {
//TODO test with 2015 404
when:
def ufList = client.getUFLaterYear(2014)

then:
ufList.size()==221
}

void "should retrieve UF list later from 2014-5"() {
when:
def ufList = client.getUFLaterYear(2014,5)

then:
ufList.size()>=0
}

void "should retrieve UF list later from 2014-5-6"() {
when:
def uf = client.getUFLaterYear(2014,5,5)

then:
uf.valor >= 0
}
}

0 comments on commit 0f3905c

Please sign in to comment.