class CurrencyQuote::Dolar

Constants

BASE_URL

Attributes

buy[RW]
date[RW]
doc[RW]
sell[RW]

Public Class Methods

new(date:) click to toggle source
# File lib/currency_quote/dolar.rb, line 11
def initialize(date:)
        @date = date
end

Public Instance Methods

get_quotation() click to toggle source
# File lib/currency_quote/dolar.rb, line 15
def get_quotation
        begin
                make_get_to_bna
                extract_data
        rescue Timeout::Error => e
                @data = @sell = @buy = 0
        end
        return struct
end

Private Instance Methods

extract_data() click to toggle source
# File lib/currency_quote/dolar.rb, line 40
def extract_data
        data = @doc.css("td").map(&:text)
        if data.empty?
                @data = @sell = @buy = 0
        else
                @date = data[data.size - 1]
                @sell = data[data.size - 2].gsub(",", ".").to_f
                @buy  = data[data.size - 3].gsub(",", ".").to_f
        end
end
make_get_to_bna() click to toggle source
# File lib/currency_quote/dolar.rb, line 27
def make_get_to_bna
        params = Addressable::URI.new
        params.query_values = {
                id: 'billetes',
                fecha: date,
                filtroEuro: 0,
                filtroDolar: 1
        }
        url = "#{BASE_URL}?#{params.query}"

        @doc = Nokogiri::HTML( open(url) )
end
struct() click to toggle source
# File lib/currency_quote/dolar.rb, line 51
def struct
        s = Struct.new(:date, :sell, :buy)
        s.new(date, sell, buy)
end