Class: Currency::Exchange::Rate::Source::Provider
- Defined in:
- lib/currency/exchange/rate/source/provider.rb
Overview
Base class for rate data providers. Assumes that rate sources provide more than one rate per query.
Direct Known Subclasses
Defined Under Namespace
Classes: ParserError
Instance Attribute Summary collapse
-
#date ⇒ Object
Returns the date to query for rates.
-
#uri ⇒ Object
(also: #name)
The URI used to access the rate source.
-
#uri_path ⇒ Object
The URI path relative to uri used to access the rate source.
Attributes inherited from Base
#pivot_currency, #time_quantitizer, #verbose
Instance Method Summary collapse
-
#available?(time = nil) ⇒ Boolean
Returns true if a rate provider is available.
-
#clear_rates ⇒ Object
Clear cached rates from this source.
-
#date_DD ⇒ Object
Returns day of query date.
-
#date_MM ⇒ Object
Return month of query date.
-
#date_YYYY ⇒ Object
Returns year of query date.
-
#get_page_content ⇒ Object
Returns the URI content.
-
#get_rate(c1, c2, time) ⇒ Object
(also: #get_rate_base)
Return a matching base rate.
-
#get_uri ⇒ Object
Returns the URI string as evaluated with this object.
-
#initialize(*args) ⇒ Provider
constructor
A new instance of Provider.
-
#load_rates(time = nil) ⇒ Object
Returns an array of base Rates from the rate source.
-
#rates(time = nil) ⇒ Object
Returns current base Rates or calls load_rates to load them from the source.
Methods inherited from Base
#__subclass_responsibility, #clear_rate, #convert, #currencies, #get_rates, #new_rate, #normalize_time, #rate, #to_s
Constructor Details
#initialize(*args) ⇒ Provider
Returns a new instance of Provider.
26 27 28 29 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 26 def initialize(*args) super @rates = { } end |
Instance Attribute Details
#date ⇒ Object
Returns the date to query for rates. Defaults to yesterday. TODO: use ActiveSupport here?
21 22 23 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 21 def date @date end |
#uri ⇒ Object Also known as: name
The URI used to access the rate source.
14 15 16 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 14 def uri @uri end |
#uri_path ⇒ Object
The URI path relative to uri used to access the rate source.
17 18 19 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 17 def uri_path @uri_path end |
Instance Method Details
#available?(time = nil) ⇒ Boolean
Returns true if a rate provider is available.
110 111 112 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 110 def available?(time = nil) true end |
#clear_rates ⇒ Object
Clear cached rates from this source.
71 72 73 74 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 71 def clear_rates @rates.clear super end |
#date_DD ⇒ Object
Returns day of query date.
52 53 54 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 52 def date_DD '%02d' % date.day end |
#date_MM ⇒ Object
Return month of query date.
46 47 48 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 46 def date_MM '%02d' % date.month end |
#date_YYYY ⇒ Object
Returns year of query date.
40 41 42 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 40 def date_YYYY '%04d' % date.year end |
#get_page_content ⇒ Object
Returns the URI content.
65 66 67 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 65 def get_page_content open(get_uri) { |data| data.read } end |
#get_rate(c1, c2, time) ⇒ Object Also known as: get_rate_base
Return a matching base rate.
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 93 def get_rate(c1, c2, time) # puts "Getting rates for c1: #{c1} and c2: #{c2} at #{time}\n rates: #{rates.map{|r| "#{r.c1}->#{r.c2}"}.inspect}" rates.each do | rate | # puts " >#{rate.c1} == #{c1} && #{rate.c2} == #{c2} #{rate.c1 == c1} && #{rate.c2.to_s == c2.to_s}< " return rate if rate.c1 == c1 && rate.c2.to_s == c2.to_s && # FIXME: understand why this second param needs to be cast to String for specs to pass (! time || normalize_time(rate.date) == time) end nil end |
#get_uri ⇒ Object
Returns the URI string as evaluated with this object.
58 59 60 61 62 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 58 def get_uri uri = instance_eval("\"#{self.uri}\"") $stderr.puts "#{self}: uri = #{uri.inspect}" if @verbose uri end |
#load_rates(time = nil) ⇒ Object
Returns an array of base Rates from the rate source.
Subclasses must define this method.
87 88 89 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 87 def load_rates(time = nil) raise Currency::Exception::SubclassResponsibility, :load_rates end |
#rates(time = nil) ⇒ Object
Returns current base Rates or calls load_rates to load them from the source.
78 79 80 81 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 78 def rates(time = nil) time = time && normalize_time(time) @rates["#{time}"] ||= load_rates(time) end |