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.
27 28 29 30 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 27 def initialize(*args) super @rates = { } end |
Instance Attribute Details
#date ⇒ Object
Returns the date to query for rates. Defaults to yesterday.
22 23 24 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 22 def date @date end |
#uri ⇒ Object Also known as: name
The URI used to access the rate source.
15 16 17 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 15 def uri @uri end |
#uri_path ⇒ Object
The URI path relative to uri used to access the rate source.
18 19 20 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 18 def uri_path @uri_path end |
Instance Method Details
#available?(time = nil) ⇒ Boolean
Returns true if a rate provider is available.
113 114 115 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 113 def available?(time = nil) true end |
#clear_rates ⇒ Object
Clear cached rates from this source.
76 77 78 79 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 76 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.
68 69 70 71 72 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 68 def get_page_content data = open(get_uri) { |data| data.read } data end |
#get_rate(c1, c2, time) ⇒ Object Also known as: get_rate_base
Return a matching base rate.
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 98 def get_rate(c1, c2, time) rates.each do | rate | return rate if rate.c1 == c1 && rate.c2 == c2 && (! 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 63 64 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 58 def get_uri uri = self.uri uri = "\"#{uri}\"" uri = instance_eval(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.
92 93 94 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 92 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.
83 84 85 86 |
# File 'lib/currency/exchange/rate/source/provider.rb', line 83 def rates(time = nil) time = time && normalize_time(time) @rates["#{time}"] ||= load_rates(time) end |