Class: HistoricBankRates::Rates

Inherits:
Object
  • Object
show all
Defined in:
lib/historic_bank_rates/rates.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_scraperObject

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_dateObject (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

#currenciesObject

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

Returns:

  • (Boolean)


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

#ratesObject

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