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.

No rates are available on Saturday and Sunday.

Constant Summary collapse

PIVOT_CURRENCY =

Defines the pivot currency for xe.com/.

:USD
@@swap_units =

The fed swaps rates on some currency pairs! See www.newyorkfed.org/markets/fxrates/noon.cfm (LISTS AUD!) www.newyorkfed.org/xml/fx.html (DOES NOT LIST AUD!)

{
  :AUD => true,
  :EUR => true,
  :NZD => true,
  :GBP => true,
}

Instance Attribute Summary

Attributes inherited from Provider

#date, #uri, #uri_path

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

#__subclass_responsibility, #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

#available?(time = nil) ⇒ Boolean

New York Fed rates are not available on Saturday and Sunday.

Returns:

  • (Boolean)


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

def available?(time = nil)
  time ||= Time.now
  ! [0, 6].include?(time.wday) ? true : false
end

#clear_ratesObject



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

def clear_rates
  @raw_rates = nil
  super
end

#load_rates(time = nil) ⇒ Object

Return a list of known base rates.



117
118
119
120
121
# File 'lib/currency/exchange/rate/source/new_york_fed.rb', line 117

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.

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/currency/exchange/rate/source/new_york_fed.rb', line 64

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
  x_series = doc.elements.to_a('//frbny:Series')
  raise ParserError, "no UNIT attribute" unless x_series
  x_series.each do | series |
    c1 = series.attributes['UNIT'] # WHAT TO DO WITH @UNIT_MULT?
    raise ParserError, "no UNIT attribute" unless c1
    c1 = c1.upcase.intern

    c2 = series.elements.to_a('frbny:Key/frbny:CURR')[0].text
    raise ParserError, "no frbny:CURR element" unless c2
    c2 = c2.upcase.intern
    
    rate = series.elements.to_a('frbny:Obs/frbny:OBS_VALUE')[0]
    raise ParserError, 'no frbny:OBS_VALUE' unless rate
    rate = rate.text.to_f

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

    # Handle arbitrary rate reciprocals!
    if @@swap_units[c1] || @@swap_units[c2]
      c1, c2 = c2, c1
    end

    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}"
  raise ::Currency::Exception::UnavailableRates, 
  [
   "No rates found in #{get_uri.inspect}",
   :uri, get_uri,
  ] if rates.empty?

  rates
end

#raw_ratesObject



46
47
48
49
# File 'lib/currency/exchange/rate/source/new_york_fed.rb', line 46

def raw_rates
  rates
  @raw_rates
end