Class: Currency::Exchange::Rate::Source::Provider

Inherits:
Base
  • Object
show all
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

FederalReserve, NewYorkFed, Test, TheFinancials, Xe

Defined Under Namespace

Classes: ParserError

Instance Attribute Summary collapse

Attributes inherited from Base

#pivot_currency, #time_quantitizer, #verbose

Instance Method Summary collapse

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

#dateObject

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

#uriObject 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_pathObject

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.

Returns:

  • (Boolean)


110
111
112
# File 'lib/currency/exchange/rate/source/provider.rb', line 110

def available?(time = nil)
  true
end

#clear_ratesObject

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_DDObject

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_MMObject

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_YYYYObject

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_contentObject

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_uriObject

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