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.



27
28
29
30
# File 'lib/currency/exchange/rate/source/provider.rb', line 27

def initialize(*args)
  super
  @rates = { }
end

Instance Attribute Details

#dateObject

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

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

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.

Returns:

  • (Boolean)


113
114
115
# File 'lib/currency/exchange/rate/source/provider.rb', line 113

def available?(time = nil)
  true
end

#clear_ratesObject

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_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.



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_uriObject

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