Skip to content

Instantly share code, notes, and snippets.

@ElMassimo
Last active July 12, 2023 12:10
Show Gist options
  • Save ElMassimo/2a9b5ce5d2e0c157c09c7e4ffb04a96f to your computer and use it in GitHub Desktop.
Save ElMassimo/2a9b5ce5d2e0c157c09c7e4ffb04a96f to your computer and use it in GitHub Desktop.
Banco Central del Uruguay - Billete Comprador Dólar Estadounidense - Última cotización anterior a la fecha
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'savon'
# Public: Permite obtener cotizaciones utilizando los webservices del BCU.
#
# Si se ejecuta como script pasandole un argumento, imprime la ultima cotizacion
# de compra del dolar billete anterior a la fecha:
# ./cotizaciones_bcu.rb 2019-02-18
# DLS. USA BILLETE 2019-02-15 32,615
#
# Para correr en modo interactivo, recuerda que puedes ejecutarlo mediante irb:
# irb -r cotizaciones_bcu
# > Cotizaciones.imprimir Cotizaciones.anterior('2019-02-18')
# > Cotizaciones.imprimir *Cotizaciones.obtener(desde: '2019-02-12', hasta: '2019-02-18')
module Cotizaciones
BCU_SERVLET_URL = "https://cotizaciones.bcu.gub.uy/wscotizaciones/servlet/"
DOLAR_BILLETE = 2225
# Public: Imprime la ultima cotizacion del dia anterior a la fecha.
def self.anterior(fecha)
obtener(desde: Date.parse(fecha) - 5, hasta: Date.parse(fecha) - 1)[-1]
end
# Public: Devuelve una lista con las cotizaciones correspondientes a la fecha
# especificada.
#
# Por defecto, cotizaciones del dolar interbancario en la fecha del ultimo
# cierre.
#
# grupo - 1: Mercado Internacional, 2: Cotizaciones Locales, 0: Ambos
def self.obtener(hasta: self.ultimo_cierre, desde: hasta, moneda: DOLAR_BILLETE, grupo: 0)
response = request('wsbcucotizaciones', message: {
'Entrada' => [
'FechaDesde' => to_date(desde),
'FechaHasta' => to_date(hasta),
'Grupo' => grupo,
'Moneda' => ['item' => moneda],
]
})
cotizacion = response.fetch(:salida).fetch(:datoscotizaciones).fetch(:datoscotizaciones_dato)
cotizacion.is_a?(Array) ? cotizacion : [cotizacion]
rescue => e
response
end
# Public: Devuelve la fecha del ultimo cierre.
def self.ultimo_cierre
request('wsultimocierre').fetch(:salida).fetch(:fecha)
end
# Internal: Imprime una cotizacion de forma legible.
def self.imprimir(*cotizaciones)
cotizaciones.each do |cotizacion|
puts [
cotizacion.fetch(:nombre),
cotizacion.fetch(:fecha).to_s,
cotizacion.fetch(:tcc).sub('.', ',').sub(/000$/, ''),
].join("\t")
end
end
# Internal: Asegura que la fecha se encuentre en el formato YYYY-mm-dd.
def self.to_date(date)
date.is_a?(String) ? Date.strptime(date, '%Y-%m-%d') : date
end
# Internal: Envía un request a un webservice del BCU y devuelve el contenido.
def self.request(endpoint_name, **attrs)
web_service("a#{ endpoint_name }").call(:execute, attrs)
.body.fetch(:"#{ endpoint_name }_execute_response")
end
# Internal: Devuelve un cliente SOAP para un webservice del BCU.
def self.web_service(endpoint_name)
Savon.client(
wsdl: "#{ BCU_SERVLET_URL }#{ endpoint_name }?wsdl",
strip_namespaces: true,
ssl_verify_mode: :none,
)
end
end
if fecha = ARGV[0]
Cotizaciones.imprimir Cotizaciones.anterior(fecha)
end
@gastonmorixe
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment