Module: ActiveExchange
- Defined in:
- lib/active_exchange/active_exchange.rb
Class Method Summary collapse
-
.exchange_rate_on(date, currency_from = "USD", currency_to = "AUD") ⇒ Object
used to find an exchange rate on a certain day.
- .get_daily_data ⇒ Object
-
.retrieve_data(date = Date.today - 1) ⇒ Object
We’ll need to have a task to look for new data and parse it into DB, to cut down calls.
Class Method Details
.exchange_rate_on(date, currency_from = "USD", currency_to = "AUD") ⇒ Object
used to find an exchange rate on a certain day. this will make a call to the api.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/active_exchange/active_exchange.rb', line 30 def self.exchange_rate_on(date, currency_from = "USD", currency_to = "AUD") xml = retrieve_data(date) us_to_eur = 1.0 au_to_eur = 1.0 xml.elements.each("//exchange_rates/fx") { |el| if el.elements[1].text == currency_from us_to_eur = el.elements[2].text.to_f rescue 1.0 end if el.elements[1].text == currency_to au_to_eur = el.elements[2].text.to_f rescue 1.0 end } return us_to_eur/au_to_eur end |
.get_daily_data ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/active_exchange/active_exchange.rb', line 18 def self.get_daily_data #Get data daily_rates = ActiveExchange.retrieve_data #put into database. Run the migrations, obviously, before you do this. if daily_rates.root.attributes["responsecode"] == "200" daily_rates.elements.each("//exchange_rates/fx") { |element| ExchangeRate.create(:currency => element.elements[1].text, :rate => element.elements[2].text) } end end |
.retrieve_data(date = Date.today - 1) ⇒ Object
We’ll need to have a task to look for new data and parse it into DB, to cut down calls.
12 13 14 15 16 |
# File 'lib/active_exchange/active_exchange.rb', line 12 def self.retrieve_data(date = Date.today - 1) url = URI.parse("http://api.finance.xaviermedia.com/api/#{date.year}/#{date.strftime("%m")}/#{date.strftime("%d")}.xml") resp = Net::HTTP.get(url) xml = REXML::Document.new(resp) end |