Class: Currency::Exchange::Rate::Source::NewYorkFed

Inherits:
Provider show all
Defined in:
lib/currency/exchange/rate/source/new_york_fed.rb

Overview

Connects to www.newyorkfed.org/markets/fxrates/FXtoXML.cfm ?FEXdate=2007%2D02%2D14%2000%3A00%3A00%2E0&FEXtime=1200 and parses XML.

This is for demonstration purposes.

Constant Summary collapse

PIVOT_CURRENCY =

Defines the pivot currency for xe.com/.

:USD

Instance Attribute Summary

Attributes inherited from Provider

#date, #uri

Attributes inherited from Base

#pivot_currency, #time_quantitizer, #verbose

Instance Method Summary collapse

Methods inherited from Provider

#date_DD, #date_MM, #date_YYYY, #get_page_content, #get_rate, #get_uri, #rates

Methods inherited from Base

#clear_rate, #convert, #currencies, #get_rate, #get_rate_base, #get_rates, #new_rate, #normalize_time, #rate, #rates, #to_s

Constructor Details

#initialize(*opt) ⇒ NewYorkFed

Returns a new instance of NewYorkFed.



20
21
22
23
24
# File 'lib/currency/exchange/rate/source/new_york_fed.rb', line 20

def initialize(*opt)
  self.uri = 'http://www.newyorkfed.org/markets/fxrates/FXtoXML.cfm?FEXdate=#{date_YYYY}%2D#{date_MM}%2D#{date_DD}%2000%3A00%3A00%2E0&FEXtime=1200'
  @raw_rates = nil
  super(*opt)
end

Instance Method Details

#clear_ratesObject



33
34
35
36
# File 'lib/currency/exchange/rate/source/new_york_fed.rb', line 33

def clear_rates
  @raw_rates = nil
  super
end

#load_rates(time = nil) ⇒ Object

Return a list of known base rates.



81
82
83
84
85
# File 'lib/currency/exchange/rate/source/new_york_fed.rb', line 81

def load_rates(time = nil)
  # $stderr.puts "#{self}: load_rates(#{time})" if @verbose
  self.date = time
  parse_rates
end

#nameObject

Returns ‘newyorkfed.org’.



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

def name
  'newyorkfed.org'
end

#parse_rates(data = nil) ⇒ Object

Parses XML for rates.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/currency/exchange/rate/source/new_york_fed.rb', line 46

def parse_rates(data = nil)
  data = get_page_content unless data
  
  rates = [ ]

  @raw_rates = { }

  $stderr.puts "#{self}: parse_rates: data =\n#{data}" if @verbose

  doc = REXML::Document.new(data).root
  doc.elements.to_a('//frbny:Series').each do | series |
    c1 = series.attributes['UNIT'] # WHAT TO DO WITH @UNIT_MULT?
    c1 = c1.upcase.intern

    c2 = series.elements.to_a('frbny:Key/frbny:CURR')[0].text
    c2 = c2.upcase.intern
    
    rate = series.elements.to_a('frbny:Obs/frbny:OBS_VALUE')[0].text.to_f

    date = series.elements.to_a('frbny:Obs/frbny:TIME_PERIOD')[0].text
    date = Time.parse("#{date} 12:00:00 -05:00") # USA NY => EST

    rates << new_rate(c1, c2, rate, date)

    (@raw_rates[c1] ||= { })[c2] ||= rate
    (@raw_rates[c2] ||= { })[c1] ||= 1.0 / rate
  end

  # $stderr.puts "rates = #{rates.inspect}"

  rates
end

#raw_ratesObject



39
40
41
42
# File 'lib/currency/exchange/rate/source/new_york_fed.rb', line 39

def raw_rates
  rates
  @raw_rates
end