Class: Currency::Exchange::Rate::Source::TheFinancials

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

Overview

Connects to www.thefinancials.com and parses XML.

This is for demonstration purposes.

Constant Summary collapse

PIVOT_CURRENCY =

Defines the pivot currency for thefinancials.com/.

:USD

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

#available?, #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) ⇒ TheFinancials

Returns a new instance of TheFinancials.



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

def initialize(*opt)
  @raw_rates = nil
  self.uri_path = 'syndicated/UNKNOWN/fxrates.xml'
  super(*opt)
  self.uri = "http://www.thefinancials.com/#{self.uri_path}"
end

Instance Method Details

#clear_ratesObject



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

def clear_rates
  @raw_rates = nil
  super
end

#load_rates(time = nil) ⇒ Object

Return a list of known base rates.



84
85
86
87
# File 'lib/currency/exchange/rate/source/the_financials.rb', line 84

def load_rates(time = nil)
  self.date = time
  parse_rates
end

#nameObject

Returns ‘thefinancials.com’.



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

def name
  'thefinancials.org'
end

#parse_rates(data = nil) ⇒ Object

Parses XML for rates.



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
78
79
80
# File 'lib/currency/exchange/rate/source/the_financials.rb', line 51

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

  @raw_rates = { }

  # $stderr.puts "parse_rates: data = #{data}"

  doc = REXML::Document.new(data).root
  doc.elements.to_a('//record').each do | record |
    c1_c2 = record.elements.to_a('symbol')[0].text
    md = /([A-Z][A-Z][A-Z]).*?([A-Z][A-Z][A-Z])/.match(c1_c2)
    c1, c2 = md[1], md[2]

    c1 = c1.upcase.intern
    c2 = c2.upcase.intern
    
    rate = record.elements.to_a('last')[0].text.to_f

    date = record.elements.to_a('date')[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
  end

  rates
end

#raw_ratesObject



44
45
46
47
# File 'lib/currency/exchange/rate/source/the_financials.rb', line 44

def raw_rates
  rates
  @raw_rates
end

#test_contentObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/currency/exchange/rate/source/the_financials.rb', line 90

def test_content
  <<EOF
<?xml version="1.0" ?> 
<TFCRecords>
<record>
<symbol>USD/EUR</symbol> 
<date>10/25/2001</date> 
<last>1.115822</last> 
</record>
<record>
<symbol>USD/AUD</symbol> 
<date>10/25/2001</date> 
<last>1.975114</last> 
</record>
<record>
<symbol>USD/CAD</symbol> 
<date>10/25/2001</date> 
<last>1.57775</last> 
</record>
<record>
<symbol>USD/CNY</symbol> 
<date>10/25/2001</date> 
<last>8.2769</last> 
</record>
<record>
<symbol>USD/ESP</symbol> 
<date>10/25/2001</date> 
<last>185.65725</last> 
</record>
<record>
<symbol>USD/GBP</symbol> 
<date>10/25/2001</date> 
<last>0.698849867830019</last> 
</record>
<record>
<symbol>USD/HKD</symbol> 
<date>10/25/2001</date> 
<last>7.7999</last> 
</record>
<record>
<symbol>USD/IDR</symbol> 
<date>10/25/2001</date> 
<last>10265</last> 
</record>
<record>
<symbol>USD/INR</symbol> 
<date>10/25/2001</date> 
<last>48.01</last> 
</record>
<record>
<symbol>USD/JPY</symbol> 
<date>10/25/2001</date> 
<last>122.68</last> 
</record>
<record>
<symbol>USD/KRW</symbol> 
<date>10/25/2001</date> 
<last>1293.5</last> 
</record>
<record>
<symbol>USD/MYR</symbol> 
<date>10/25/2001</date> 
<last>3.8</last> 
</record>
<record>
<symbol>USD/NZD</symbol> 
<date>10/25/2001</date> 
<last>2.41485</last> 
</record>
<record>
<symbol>USD/PHP</symbol> 
<date>10/25/2001</date> 
<last>52.05</last> 
</record>
<record>
<symbol>USD/PKR</symbol> 
<date>10/25/2001</date> 
<last>61.6</last> 
</record>
<record>
<symbol>USD/SGD</symbol> 
<date>10/25/2001</date> 
<last>1.82615</last> 
</record>
<record>
<symbol>USD/THB</symbol> 
<date>10/25/2001</date> 
<last>44.88</last> 
</record>
<record>
<symbol>USD/TWD</symbol> 
<date>10/25/2001</date> 
<last>34.54</last> 
</record>
</TFCRecords>
EOF
end