class Dolar::Bna::Exchange
Public Class Methods
new(fecha=Date.today)
click to toggle source
# File lib/dolar/bna/exchange.rb, line 8 def initialize(fecha=Date.today) @fecha ||= fecha end
Public Instance Methods
perform_bna_billete()
click to toggle source
# File lib/dolar/bna/exchange.rb, line 12 def perform_bna_billete begin Timeout.timeout(15) do data = check_cotization("Billete", @fecha) save_in_db(data, "Billete") unless data.blank? return data end rescue => ex pp ex.message return nil end end
perform_bna_divisa()
click to toggle source
# File lib/dolar/bna/exchange.rb, line 25 def perform_bna_divisa begin Timeout.timeout(15) do data = check_cotization("Divisa", @fecha) save_in_db(data, "Divisa") unless data.blank? return data end rescue => ex pp ex.message return nil end end
variation_billete()
click to toggle source
# File lib/dolar/bna/exchange.rb, line 38 def variation_billete begin Timeout.timeout(15) do today_dolar = check_cotization("Billete", @fecha) yesterday_dolar = check_cotization("Billete", (@fecha - 1.days)) unless (today_dolar.nil? || yesterday_dolar.nil?) porcentual_variation = ((today_dolar[:venta] - yesterday_dolar[:venta]) / (yesterday_dolar[:venta])) porcentual_variation = "#{(porcentual_variation * 100).round(2)}%" return porcentual_variation else return "0%" end end rescue => ex pp ex.message return nil end end
Private Instance Methods
check_cotization(dolar_type, date)
click to toggle source
# File lib/dolar/bna/exchange.rb, line 59 def check_cotization dolar_type, date query = Dolar::Bna::DolarCotization.where(date: date.to_date, dolar_type: dolar_type).first ddolar = nil if query.nil? if dolar_type == "Divisa" ddolar = get_dolar_divisa() else ddolar = get_dolar() end else ddolar = {compra: query.dolar_buy, venta: query.dolar_sell} end return ddolar end
get_dolar()
click to toggle source
# File lib/dolar/bna/exchange.rb, line 74 def get_dolar require "open-uri" data = {} mechanize = Mechanize.new mechanize.user_agent_alias = "Android" begin Timeout.timeout(15) do url = "https://www.bna.com.ar/Cotizador/HistoricoPrincipales?id=billetes&fecha=#{@fecha.day}%2F#{@fecha.month}%2F#{@fecha.year}&filtroEuro=0&filtroDolar=1" value = obtain_dolar_from_html(url, mechanize, data, "billete") return value end rescue => ex pp ex.message return nil end end
get_dolar_divisa()
click to toggle source
# File lib/dolar/bna/exchange.rb, line 91 def get_dolar_divisa require "open-uri" data = {} mechanize = Mechanize.new mechanize.user_agent_alias = "Android" begin Timeout.timeout(15) do url = "https://www.bna.com.ar/Cotizador/MonedasHistorico" value = obtain_dolar_from_html(url, mechanize, data, "billete") return value end rescue => ex pp ex.message return nil end end
obtain_dolar_from_html(url, mechanize, data, d_type)
click to toggle source
# File lib/dolar/bna/exchange.rb, line 108 def obtain_dolar_from_html(url, mechanize, data, d_type) page = mechanize.get(url) doc = Nokogiri::HTML(page.body, "UTF-8") doc.xpath("//td").each_with_index do |node, index| data[index] = node.text end correct_date = "#{@fecha.day.to_i}/#{@fecha.month.to_i}/#{@fecha.year.to_i}" i = data.key(correct_date) if !i.nil? dolar_compra = BigDecimal(data[i - 2].tr(",", ".")).truncate(3).to_f dolar_venta = BigDecimal(data[i - 1].tr(",", ".")).truncate(3).to_f else dolar_compra = BigDecimal(data[1].tr(",", ".")).truncate(3).to_f dolar_venta = BigDecimal(data[2].tr(",", ".")).truncate(3).to_f end return {compra: dolar_compra, venta: dolar_venta} end
save_in_db(data, dolar_type)
click to toggle source
# File lib/dolar/bna/exchange.rb, line 126 def save_in_db data, dolar_type unless data.nil? dr = Dolar::Bna::DolarCotization.where(date: @fecha.to_date, dolar_type: dolar_type, dolar_buy: data[:compra], dolar_sell: data[:venta]).first_or_initialize if dr.save pp "todo ok" else pp dr.errors end end end