Class: UruguayanExchangeRates::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/uruguayan_exchange_rates/util.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Util

Returns a new instance of Util.



8
9
10
11
12
13
14
15
16
# File 'lib/uruguayan_exchange_rates/util.rb', line 8

def initialize(options = {})
  unless block_given?
    options.each do |key, value|
      send(:"#{key}=", value)
    end
  else
    yield(self)
  end
end

Instance Attribute Details

#currency_rawObject

Returns the value of attribute currency_raw.



6
7
8
# File 'lib/uruguayan_exchange_rates/util.rb', line 6

def currency_raw
  @currency_raw
end

Instance Method Details

#extract_values(currency_data) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/uruguayan_exchange_rates/util.rb', line 28

def extract_values(currency_data)
  # Remove unnecessary spaces
  values = currency_data.text.strip
  # Remove name
  values.gsub!(currency_raw, '')
  # Remove spaces
  values.gsub!(/(?:\n\r?|\r\n?)/, ' ').strip!
  buy, sell = values.split(/\s+/).map{ |v| v.to_f }
  { buy: buy, sell: sell }
end

#find_currency(data) ⇒ Object



24
25
26
# File 'lib/uruguayan_exchange_rates/util.rb', line 24

def find_currency(data)
  values = data.css('#exchangeRatesLarge').at('tr:contains("' + currency_raw + '")')
end

#request_dataObject



18
19
20
21
22
# File 'lib/uruguayan_exchange_rates/util.rb', line 18

def request_data
  uri = URI.parse(SERVICE_HOST + SERVICE_PATH)
  result = Net::HTTP.get(uri)
  data = Nokogiri::HTML(result)
end