Class: HistoricBankRates::Rates
- Inherits:
-
Object
- Object
- HistoricBankRates::Rates
- Defined in:
- lib/historic_bank_rates/rates.rb
Instance Attribute Summary collapse
-
#rate_scraper ⇒ Object
Returns the value of attribute rate_scraper.
-
#start_date ⇒ Object
readonly
Returns the value of attribute start_date.
Instance Method Summary collapse
-
#currencies ⇒ Object
Returns a list of ISO currencies.
-
#has_rates? ⇒ Boolean
Returns true when reading the website was successful.
-
#import!(import_date = nil) ⇒ Object
Triggers the scraping.
-
#initialize(rate_scraper, start_date = nil) ⇒ Rates
constructor
A new instance of Rates.
- #rate(iso_from, iso_to) ⇒ Object
-
#rates ⇒ Object
Returns all rates imported.
Constructor Details
#initialize(rate_scraper, start_date = nil) ⇒ Rates
Returns a new instance of Rates.
12 13 14 15 |
# File 'lib/historic_bank_rates/rates.rb', line 12 def initialize rate_scraper, start_date=nil @rate_scraper = rate_scraper @start_date = start_date || date_yesterday end |
Instance Attribute Details
#rate_scraper ⇒ Object
Returns the value of attribute rate_scraper.
10 11 12 |
# File 'lib/historic_bank_rates/rates.rb', line 10 def rate_scraper @rate_scraper end |
#start_date ⇒ Object (readonly)
Returns the value of attribute start_date.
9 10 11 |
# File 'lib/historic_bank_rates/rates.rb', line 9 def start_date @start_date end |
Instance Method Details
#currencies ⇒ Object
Returns a list of ISO currencies
29 30 31 |
# File 'lib/historic_bank_rates/rates.rb', line 29 def currencies rates.keys end |
#has_rates? ⇒ Boolean
Returns true when reading the website was successful
40 41 42 |
# File 'lib/historic_bank_rates/rates.rb', line 40 def has_rates? @rates && @rates.any? end |
#import!(import_date = nil) ⇒ Object
Triggers the scraping
45 46 47 48 49 |
# File 'lib/historic_bank_rates/rates.rb', line 45 def import! import_date=nil @start_date = import_date unless import_date.nil? @rates = rate_scraper.call(start_date) has_rates? end |
#rate(iso_from, iso_to) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/historic_bank_rates/rates.rb', line 17 def rate(iso_from, iso_to) case rate_scraper.base_currency when iso_from rates[iso_to] ? 1/rates[iso_to] : nil when iso_to rates[iso_from] else fail_if_rates_missing end end |
#rates ⇒ Object
Returns all rates imported
34 35 36 37 |
# File 'lib/historic_bank_rates/rates.rb', line 34 def rates fail_if_rates_missing @rates end |