nas-yahoo_stock 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +23 -1
- data/Manifest.txt +20 -4
- data/README.rdoc +62 -6
- data/lib/yahoo_stock/base.rb +37 -0
- data/lib/yahoo_stock/history.rb +13 -167
- data/lib/yahoo_stock/interface/history.rb +186 -0
- data/lib/yahoo_stock/interface/quote.rb +287 -0
- data/lib/yahoo_stock/interface/scrip_symbol.rb +74 -0
- data/lib/yahoo_stock/interface.rb +45 -217
- data/lib/yahoo_stock/quote.rb +13 -18
- data/lib/yahoo_stock/result/array_format.rb +27 -0
- data/lib/yahoo_stock/result/hash_format.rb +37 -0
- data/lib/yahoo_stock/result.rb +21 -0
- data/lib/yahoo_stock/scrip_symbol.rb +18 -56
- data/lib/yahoo_stock.rb +10 -3
- data/spec/yahoo_stock/base_spec.rb +48 -0
- data/spec/yahoo_stock/history_spec.rb +38 -153
- data/spec/yahoo_stock/interface/history_spec.rb +317 -0
- data/spec/yahoo_stock/interface/quote_spec.rb +414 -0
- data/spec/yahoo_stock/interface/scrip_symbol_spec.rb +120 -0
- data/spec/yahoo_stock/interface_spec.rb +73 -236
- data/spec/yahoo_stock/quote_spec.rb +27 -86
- data/spec/yahoo_stock/result/array_format_spec.rb +38 -0
- data/spec/yahoo_stock/result/hash_format_spec.rb +68 -0
- data/spec/yahoo_stock/result_spec.rb +33 -0
- data/spec/yahoo_stock/scrip_symbol_spec.rb +26 -91
- metadata +31 -37
@@ -0,0 +1,287 @@
|
|
1
|
+
=begin
|
2
|
+
|
3
|
+
Parameter source:
|
4
|
+
http://www.gummy-stuff.org/Yahoo-data.htm
|
5
|
+
http://finance.yahoo.com/d/quotes.csv?s=XOM+BBDb.TO+JNJ+MSFT&f=snd1l1yr
|
6
|
+
|
7
|
+
=end
|
8
|
+
|
9
|
+
module YahooStock
|
10
|
+
# ==DESCRIPTION:
|
11
|
+
#
|
12
|
+
# Class to generate the right url and interface with yahoo to get daily stock data
|
13
|
+
#
|
14
|
+
class Interface::Quote < Interface
|
15
|
+
|
16
|
+
class QuoteError < RuntimeError ; end
|
17
|
+
|
18
|
+
STD_PARAMETERS = {
|
19
|
+
:symbol => 's',
|
20
|
+
:name => 'n',
|
21
|
+
:last_trade_price_only => 'l1',
|
22
|
+
:last_trade_date => 'd1',
|
23
|
+
:last_trade_time => 't1',
|
24
|
+
:change_with_percent_change => 'c',
|
25
|
+
:change => 'c1',
|
26
|
+
:previous_close => 'p',
|
27
|
+
:change_in_percent => 'p2',
|
28
|
+
:open => 'o',
|
29
|
+
:day_low => 'g',
|
30
|
+
:day_high => 'h',
|
31
|
+
:volume => 'v',
|
32
|
+
:last_trade_with_time => 'l',
|
33
|
+
:day_range => 'm',
|
34
|
+
:ticker_trend => 't7',
|
35
|
+
:ask => 'a',
|
36
|
+
:average_daily_volume => 'a2',
|
37
|
+
:bid => 'b',
|
38
|
+
:bid_size => 'b4'
|
39
|
+
} unless defined?(STD_PARAMETERS)
|
40
|
+
|
41
|
+
EXTENDED_PARAMETERS = {
|
42
|
+
:symbol => 's',
|
43
|
+
:name => 'n',
|
44
|
+
:fifty_two_week_range => 'w',
|
45
|
+
:change_from_52_week_low => 'j5',
|
46
|
+
:percent_change_from_52_week_low => 'j6',
|
47
|
+
:change_from_52_week_high => 'k4',
|
48
|
+
:percent_change_from_52_week_high => 'k5',
|
49
|
+
:earnings_per_share => 'e',
|
50
|
+
:short_ratio => 's7',
|
51
|
+
:p_e_ratio => 'r',
|
52
|
+
:dividend_pay_date => 'r1',
|
53
|
+
:ex_dividend_date => 'q',
|
54
|
+
:dividend_per_share => 'd',
|
55
|
+
:dividend_yield => 'y',
|
56
|
+
:one_yr_target_price => 't8',
|
57
|
+
:market_capitalization => 'j1',
|
58
|
+
:eps_estimate_current_year => 'e7',
|
59
|
+
:eps_estimate_next_year => 'e8',
|
60
|
+
:eps_estimate_next_quarter => 'e9',
|
61
|
+
:peg_ratio => 'r5',
|
62
|
+
:price_eps_estimate_current_year => 'r6',
|
63
|
+
:price_eps_estimate_next_year => 'r7',
|
64
|
+
:book_value => 'b4',
|
65
|
+
:ebitda => 'j4',
|
66
|
+
:fifty_day_moving_average => 'm3',
|
67
|
+
:two_hundred_day_moving_average => 'm4',
|
68
|
+
:change_from_200_day_moving_average => 'm5',
|
69
|
+
:percent_change_from_200_day_moving_average => 'm6',
|
70
|
+
:change_from_50_day_moving_average => 'm7',
|
71
|
+
:percent_change_from_50_day_moving_average => 'm8',
|
72
|
+
:shares_owned => 's1',
|
73
|
+
:price_paid => 'p1',
|
74
|
+
:commission => 'c3',
|
75
|
+
:holdings_value => 'v1',
|
76
|
+
:day_value_change => 'w1',
|
77
|
+
:trade_date => 'd2',
|
78
|
+
:holdings_gain_percent => 'g1',
|
79
|
+
:annualized_gain => 'g3',
|
80
|
+
:holdings_gain => 'g4',
|
81
|
+
:stock_exchange => 'x',
|
82
|
+
:high_limit => 'l2',
|
83
|
+
:low_limit => 'l3',
|
84
|
+
:notes => 'n4',
|
85
|
+
:fifty_two_week_low => 'j',
|
86
|
+
:fifty_two_week_high => 'k',
|
87
|
+
:more_info => 'i',
|
88
|
+
} unless defined?(EXTENDED_PARAMETERS)
|
89
|
+
|
90
|
+
REALTIME_PARAMETERS = {
|
91
|
+
:symbol => 's',
|
92
|
+
:name => 'n',
|
93
|
+
:ask_real_time => 'b2',
|
94
|
+
:bid_real_time => 'b3',
|
95
|
+
:change => 'c1',
|
96
|
+
:change_real_time => 'c6',
|
97
|
+
:after_hours_change_real_time => 'c8',
|
98
|
+
:holdings_gain_percent_real_time => 'g5',
|
99
|
+
:holdings_gain_real_time => 'g6',
|
100
|
+
:order_book_real_time => 'i5',
|
101
|
+
:market_cap_real_time => 'j3',
|
102
|
+
:last_trade_real_time_with_time => 'k1',
|
103
|
+
:change_percent_real_time => 'k2',
|
104
|
+
:day_range_real_time => 'm2',
|
105
|
+
:p_e_ratio_real_time => 'r2',
|
106
|
+
:holdings_value_real_time => 'v7',
|
107
|
+
:day_value_change_real_time => 'w4',
|
108
|
+
} unless defined?(REALTIME_PARAMETERS)
|
109
|
+
|
110
|
+
attr_reader :stock_symbols
|
111
|
+
|
112
|
+
# The stock_params_hash parameter expects a hash with two key value pairs
|
113
|
+
#
|
114
|
+
# :stock_symbols => 'Array of stock symbols'
|
115
|
+
#
|
116
|
+
# e.g. :stock_symbols => ['YHOO']
|
117
|
+
#
|
118
|
+
# another hash :read_parameters => ['array of values']
|
119
|
+
#
|
120
|
+
# e.g. :read_parameters => [:last_trade_price_only, :last_trade_date]
|
121
|
+
def initialize(stock_params_hash)
|
122
|
+
unless stock_params_hash
|
123
|
+
raise QuoteError, 'You must pass a hash of stock symbols and the data you would like to see'
|
124
|
+
end
|
125
|
+
if !stock_params_hash[:stock_symbols] || stock_params_hash[:stock_symbols].length.zero?
|
126
|
+
raise(QuoteError, 'No stocks passed')
|
127
|
+
end
|
128
|
+
if !stock_params_hash[:read_parameters] || stock_params_hash[:read_parameters].length.zero?
|
129
|
+
raise QuoteError, 'Read parameters are not provided'
|
130
|
+
end
|
131
|
+
@stock_symbols = stock_params_hash[:stock_symbols]
|
132
|
+
@yahoo_url_parameters = stock_params_hash[:read_parameters]
|
133
|
+
@base_url = BASE_URLS[:quote]
|
134
|
+
add_observer(self)
|
135
|
+
end
|
136
|
+
|
137
|
+
def stock_symbols=(stock_symbol)
|
138
|
+
symbols_on_read = @stock_symbols.dup
|
139
|
+
@stock_symbols ||= []
|
140
|
+
@stock_symbols << stock_symbol unless @stock_symbols.include?(stock_symbol)
|
141
|
+
run_observers if symbols_on_read != @stock_symbols
|
142
|
+
end
|
143
|
+
|
144
|
+
def yahoo_url_parameters
|
145
|
+
return [] if !@yahoo_url_parameters || @yahoo_url_parameters.empty?
|
146
|
+
@yahoo_url_parameters.map(&:id2name).sort.map(&:to_sym)
|
147
|
+
end
|
148
|
+
|
149
|
+
def yahoo_url_parameters=(yahoo_url_parameter)
|
150
|
+
params_on_read = @yahoo_url_parameters.dup
|
151
|
+
unless allowed_parameters.include?(yahoo_url_parameter)
|
152
|
+
raise QuoteError, "Interface parameter #{yahoo_url_parameter} is not a valid parameter."
|
153
|
+
end
|
154
|
+
@yahoo_url_parameters ||= []
|
155
|
+
@yahoo_url_parameters << yahoo_url_parameter unless @yahoo_url_parameters.include?(yahoo_url_parameter)
|
156
|
+
run_observers if params_on_read != @yahoo_url_parameters
|
157
|
+
end
|
158
|
+
|
159
|
+
# Generate full url to be sent to yahoo
|
160
|
+
def uri
|
161
|
+
@all_stock_symbols = stock_symbols.join('+')
|
162
|
+
invalid_params = yahoo_url_parameters-allowed_parameters
|
163
|
+
unless invalid_params.length.zero?
|
164
|
+
raise QuoteError, "The parameters '#{invalid_params.join(', ')}' are not valid. Please check using YahooStock::Interface::Quote#allowed_parameters or YahooStock::Quote#valid_parameters"
|
165
|
+
end
|
166
|
+
@parameter_values = yahoo_url_parameters.collect {|v| parameters[v]}.join('')
|
167
|
+
if @all_stock_symbols.empty?
|
168
|
+
raise QuoteError, "You must add atleast one stock symbol to get stock data"
|
169
|
+
end
|
170
|
+
if @parameter_values.empty?
|
171
|
+
raise QuoteError, "You must add atleast one parameter to get stock data"
|
172
|
+
end
|
173
|
+
@uri_parameters = {:s => @all_stock_symbols, :f => @parameter_values}
|
174
|
+
super()
|
175
|
+
end
|
176
|
+
|
177
|
+
# Read the result using get method in super class
|
178
|
+
def get
|
179
|
+
uri
|
180
|
+
super()
|
181
|
+
end
|
182
|
+
|
183
|
+
# TODO MOVE TO THE HASH CLASS
|
184
|
+
# Returns results for the stock symbols as a hash.
|
185
|
+
# The hash keys are the stock symbols and the values are a hash of the keys and
|
186
|
+
# values asked for that stock.
|
187
|
+
def results
|
188
|
+
stock = {}
|
189
|
+
values.each_with_index do |values, index|
|
190
|
+
parsed_values = values.split(',')
|
191
|
+
stock[stock_symbols[index]] ||= {}
|
192
|
+
parsed_values.each_with_index do |value, i|
|
193
|
+
stock[stock_symbols[index]][yahoo_url_parameters[i]] = value
|
194
|
+
end
|
195
|
+
end
|
196
|
+
stock
|
197
|
+
end
|
198
|
+
|
199
|
+
# Add stock symbols to the url.
|
200
|
+
def add_symbols(*symbols)
|
201
|
+
symbols.each {|symbol| self.stock_symbols = symbol }
|
202
|
+
end
|
203
|
+
|
204
|
+
# Remove stock symbols from the url.
|
205
|
+
def remove_symbols(*symbols)
|
206
|
+
symbols.each do |symbol|
|
207
|
+
unless stock_symbols.include?(symbol)
|
208
|
+
raise QuoteError, "Cannot remove stock symbol #{symbol} as it is currently not present."
|
209
|
+
end
|
210
|
+
if stock_symbols.length == 1
|
211
|
+
raise QuoteError, "Symbol #{symbol} is the last symbol. Please add another symbol before removing this."
|
212
|
+
end
|
213
|
+
stock_symbols.reject!{|stock_sym| stock_sym == symbol}
|
214
|
+
run_observers
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
# Add parameters to the url.
|
219
|
+
def add_parameters(*parameters)
|
220
|
+
parameters.each {|parameter| self.yahoo_url_parameters = parameter }
|
221
|
+
end
|
222
|
+
|
223
|
+
# Remove parameters from the url.
|
224
|
+
def remove_parameters(*parameters)
|
225
|
+
parameters.each do |parameter|
|
226
|
+
unless yahoo_url_parameters.include?(parameter)
|
227
|
+
raise QuoteError, "Parameter #{parameter} is not present in current list"
|
228
|
+
end
|
229
|
+
if yahoo_url_parameters.length == 1
|
230
|
+
raise QuoteError, "Parameter #{parameter} is the last parameter. Please add another parameter before removing this."
|
231
|
+
end
|
232
|
+
@yahoo_url_parameters.reject!{|parameter_key| parameter_key == parameter}
|
233
|
+
run_observers
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
# Returns an array of parameters that can be passed to yahoo.
|
238
|
+
def allowed_parameters
|
239
|
+
parameters.keys
|
240
|
+
end
|
241
|
+
|
242
|
+
# Add standard parameters
|
243
|
+
def add_standard_params
|
244
|
+
add_grouped_parameters(STD_PARAMETERS.keys)
|
245
|
+
end
|
246
|
+
|
247
|
+
# Add extended parameters
|
248
|
+
def add_extended_params
|
249
|
+
add_grouped_parameters(EXTENDED_PARAMETERS.keys)
|
250
|
+
end
|
251
|
+
|
252
|
+
# Add realtime parameters
|
253
|
+
def add_realtime_params
|
254
|
+
add_grouped_parameters(REALTIME_PARAMETERS.keys)
|
255
|
+
end
|
256
|
+
|
257
|
+
# Clear all existing parameters from the current instance.
|
258
|
+
def clear_parameters
|
259
|
+
@yahoo_url_parameters.clear
|
260
|
+
run_observers
|
261
|
+
end
|
262
|
+
|
263
|
+
# Clear all existing symbols from the current instance.
|
264
|
+
def clear_symbols
|
265
|
+
stock_symbols.clear
|
266
|
+
run_observers
|
267
|
+
end
|
268
|
+
|
269
|
+
private
|
270
|
+
|
271
|
+
def add_grouped_parameters(parameter_group)
|
272
|
+
return if (parameter_group - yahoo_url_parameters).empty?
|
273
|
+
clear_parameters
|
274
|
+
parameter_group.each { |parameter| add_parameters(parameter) }
|
275
|
+
end
|
276
|
+
|
277
|
+
def run_observers
|
278
|
+
changed
|
279
|
+
notify_observers
|
280
|
+
end
|
281
|
+
|
282
|
+
def parameters
|
283
|
+
STD_PARAMETERS.merge(EXTENDED_PARAMETERS).merge(REALTIME_PARAMETERS)
|
284
|
+
end
|
285
|
+
|
286
|
+
end
|
287
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module YahooStock
|
2
|
+
# ==DESCRIPTION:
|
3
|
+
#
|
4
|
+
# Class to generate the right url and interface with yahoo to get Scrip / Stock Symbols
|
5
|
+
#
|
6
|
+
class Interface::ScripSymbol < Interface
|
7
|
+
|
8
|
+
class ScripSymbolError < RuntimeError ; end
|
9
|
+
|
10
|
+
attr_reader :company
|
11
|
+
# Scrapes the resulting page and gets data in between two points
|
12
|
+
def initialize(company)
|
13
|
+
@base_url = BASE_URLS[:scrip_symbol]
|
14
|
+
@company = remove_any_space(company)
|
15
|
+
@before_element = 'yfi_sym_results'
|
16
|
+
@after_element = 'yfi_fp_left_bottom'
|
17
|
+
add_observer(self)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Generate full uri with the help of uri method of the superclass
|
21
|
+
def uri
|
22
|
+
@uri_parameters = {:s => @company}
|
23
|
+
super()
|
24
|
+
end
|
25
|
+
|
26
|
+
# Get uri content with the help of get method of the super class
|
27
|
+
def get
|
28
|
+
uri
|
29
|
+
super()
|
30
|
+
end
|
31
|
+
|
32
|
+
def values
|
33
|
+
@values ||= get_values
|
34
|
+
end
|
35
|
+
|
36
|
+
def company=(company)
|
37
|
+
old_company = @company
|
38
|
+
@company = remove_any_space(company)
|
39
|
+
old_company != @company ? changed : changed(false)
|
40
|
+
notify_observers
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def remove_any_space(words)
|
46
|
+
words.gsub(/\s/,'+')
|
47
|
+
end
|
48
|
+
# returns only the text among two points
|
49
|
+
def text_range
|
50
|
+
body = get.gsub!(/\s*/,'')
|
51
|
+
pattern = /#{@before_element}.*#{@after_element}/
|
52
|
+
results = pattern.match(body)
|
53
|
+
return results[0] if results
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns all possible values in a single string
|
57
|
+
def get_values
|
58
|
+
data = []
|
59
|
+
rows = text_range.to_s.split(/\<\/tr>/)
|
60
|
+
rows.each_with_index do |row, row_i|
|
61
|
+
cells = row.split(/\<\/td>/)
|
62
|
+
row_data = []
|
63
|
+
cells.each_with_index do |cell, cell_i|
|
64
|
+
datum = cell.sub('</a>','').gsub(/\<.*\>/,'').strip
|
65
|
+
row_data << datum if !datum.nil? || datum.empty?
|
66
|
+
end
|
67
|
+
data << row_data.join(',') if row_data.length > 1
|
68
|
+
end
|
69
|
+
data.join("\r\n")
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
@@ -1,243 +1,71 @@
|
|
1
1
|
=begin
|
2
|
-
|
3
|
-
Parameter source:
|
4
|
-
http://www.gummy-stuff.org/Yahoo-data.htm
|
5
|
-
http://finance.yahoo.com/d/quotes.csv?s=XOM+BBDb.TO+JNJ+MSFT&f=snd1l1yr
|
6
|
-
|
2
|
+
Main class to interface with Yahoo
|
7
3
|
=end
|
8
4
|
|
9
5
|
require 'net/http'
|
6
|
+
require 'observer'
|
10
7
|
module YahooStock
|
11
8
|
# ==DESCRIPTION:
|
12
9
|
#
|
13
10
|
# Class to generate the right url and interface with yahoo
|
14
11
|
#
|
15
12
|
class Interface
|
16
|
-
|
13
|
+
include Observable
|
17
14
|
class InterfaceError < RuntimeError ; end
|
18
|
-
|
19
|
-
STD_PARAMETERS = {
|
20
|
-
:symbol => 's',
|
21
|
-
:name => 'n',
|
22
|
-
:last_trade_price_only => 'l1',
|
23
|
-
:last_trade_date => 'd1',
|
24
|
-
:last_trade_time => 't1',
|
25
|
-
:change_with_percent_change => 'c',
|
26
|
-
:change => 'c1',
|
27
|
-
:previous_close => 'p',
|
28
|
-
:change_in_percent => 'p2',
|
29
|
-
:open => 'o',
|
30
|
-
:day_low => 'g',
|
31
|
-
:day_high => 'h',
|
32
|
-
:volume => 'v',
|
33
|
-
:last_trade_with_time => 'l',
|
34
|
-
:day_range => 'm',
|
35
|
-
:ticker_trend => 't7',
|
36
|
-
:ask => 'a',
|
37
|
-
:average_daily_volume => 'a2',
|
38
|
-
:bid => 'b',
|
39
|
-
:bid_size => 'b4'
|
40
|
-
}
|
41
|
-
|
42
|
-
EXTENDED_PARAMETERS = {
|
43
|
-
:symbol => 's',
|
44
|
-
:name => 'n',
|
45
|
-
:fifty_two_week_range => 'w',
|
46
|
-
:change_from_52_week_low => 'j5',
|
47
|
-
:percent_change_from_52_week_low => 'j6',
|
48
|
-
:change_from_52_week_high => 'k4',
|
49
|
-
:percent_change_from_52_week_high => 'k5',
|
50
|
-
:earnings_per_share => 'e',
|
51
|
-
:short_ratio => 's7',
|
52
|
-
:p_e_ratio => 'r',
|
53
|
-
:dividend_pay_date => 'r1',
|
54
|
-
:ex_dividend_date => 'q',
|
55
|
-
:dividend_per_share => 'd',
|
56
|
-
:dividend_yield => 'y',
|
57
|
-
:one_yr_target_price => 't8',
|
58
|
-
:market_capitalization => 'j1',
|
59
|
-
:eps_estimate_current_year => 'e7',
|
60
|
-
:eps_estimate_next_year => 'e8',
|
61
|
-
:eps_estimate_next_quarter => 'e9',
|
62
|
-
:peg_ratio => 'r5',
|
63
|
-
:price_eps_estimate_current_year => 'r6',
|
64
|
-
:price_eps_estimate_next_year => 'r7',
|
65
|
-
:book_value => 'b4',
|
66
|
-
:ebitda => 'j4',
|
67
|
-
:fifty_day_moving_average => 'm3',
|
68
|
-
:two_hundred_day_moving_average => 'm4',
|
69
|
-
:change_from_200_day_moving_average => 'm5',
|
70
|
-
:percent_change_from_200_day_moving_average => 'm6',
|
71
|
-
:change_from_50_day_moving_average => 'm7',
|
72
|
-
:percent_change_from_50_day_moving_average => 'm8',
|
73
|
-
:shares_owned => 's1',
|
74
|
-
:price_paid => 'p1',
|
75
|
-
:commission => 'c3',
|
76
|
-
:holdings_value => 'v1',
|
77
|
-
:day_value_change => 'w1',
|
78
|
-
:trade_date => 'd2',
|
79
|
-
:holdings_gain_percent => 'g1',
|
80
|
-
:annualized_gain => 'g3',
|
81
|
-
:holdings_gain => 'g4',
|
82
|
-
:stock_exchange => 'x',
|
83
|
-
:high_limit => 'l2',
|
84
|
-
:low_limit => 'l3',
|
85
|
-
:notes => 'n4',
|
86
|
-
:fifty_two_week_low => 'j',
|
87
|
-
:fifty_two_week_high => 'k',
|
88
|
-
:more_info => 'i',
|
89
|
-
}
|
90
15
|
|
91
|
-
|
92
|
-
:
|
93
|
-
:
|
94
|
-
:
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
:day_range_real_time => 'm2',
|
106
|
-
:p_e_ratio_real_time => 'r2',
|
107
|
-
:holdings_value_real_time => 'v7',
|
108
|
-
:day_value_change_real_time => 'w4',
|
109
|
-
}
|
110
|
-
|
111
|
-
attr_accessor :stock_symbols, :yahoo_url_parameters
|
112
|
-
|
113
|
-
# The stock_params_hash parameter expects a hash with two key value pairs
|
114
|
-
#
|
115
|
-
# :stock_symbols => 'Array of stock symbols'
|
116
|
-
#
|
117
|
-
# e.g. :stock_symbols => ['YHOO']
|
118
|
-
#
|
119
|
-
# another hash :read_parameters => 'array of values'
|
120
|
-
#
|
121
|
-
# e.g. :read_parameters => [:last_trade_price_only, :last_trade_date]
|
122
|
-
def initialize(stock_params_hash)
|
123
|
-
unless stock_params_hash
|
124
|
-
raise InterfaceError, 'You must pass a hash of stock symbols and the data you would like to see'
|
125
|
-
end
|
126
|
-
if !stock_params_hash[:stock_symbols] || stock_params_hash[:stock_symbols].length.zero?
|
127
|
-
raise(InterfaceError, 'No stocks passed')
|
128
|
-
end
|
129
|
-
if !stock_params_hash[:read_parameters] || stock_params_hash[:read_parameters].length.zero?
|
130
|
-
raise InterfaceError, 'Dont know what data to get'
|
16
|
+
BASE_URLS = {
|
17
|
+
:quote => "http://download.finance.yahoo.com/d/quotes.csv",
|
18
|
+
:history => "http://ichart.finance.yahoo.com/table.csv",
|
19
|
+
:scrip_symbol => "http://finance.yahoo.com/lookup/all"
|
20
|
+
} unless defined?(BASE_URLS)
|
21
|
+
|
22
|
+
attr_accessor :base_url, :uri_parameters
|
23
|
+
|
24
|
+
# Send request to the uri and get results
|
25
|
+
def get
|
26
|
+
begin
|
27
|
+
response = Net::HTTP.get_response(URI.parse(uri))
|
28
|
+
rescue => e
|
29
|
+
raise InterfaceError, "#{e.message}\n\n#{e.backtrace}"
|
131
30
|
end
|
132
|
-
|
133
|
-
@yahoo_url_parameters = stock_params_hash[:read_parameters]
|
134
|
-
@base_url = "http://download.finance.yahoo.com/d/quotes.csv"
|
31
|
+
response.code == '200' ? response.body : response_error(response)
|
135
32
|
end
|
136
33
|
|
137
|
-
#
|
138
|
-
def
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
parameter_values = yahoo_url_parameters.collect {|v| parameters[v]}.join('')
|
145
|
-
if all_stock_symbols.empty?
|
146
|
-
raise InterfaceError, "You must add atleast one stock symbol to get stock data"
|
147
|
-
end
|
148
|
-
if parameter_values.empty?
|
149
|
-
raise InterfaceError, "You must add atleast one parameter to get stock data"
|
150
|
-
end
|
151
|
-
"#{@base_url}?s=#{all_stock_symbols}&f=#{parameter_values}"
|
152
|
-
end
|
153
|
-
|
154
|
-
# Get the csv file and create an array of different stock symbols and values returned
|
155
|
-
# for the parameters passed based on line break.
|
156
|
-
def get_values
|
157
|
-
Net::HTTP.get(URI.parse(full_url)).gsub(/\"/,'').split(/\r\n/)
|
34
|
+
# Generates full url to connect to yahoo
|
35
|
+
def uri
|
36
|
+
raise InterfaceError, 'Base url is require to generate full uri.' unless @base_url
|
37
|
+
return @base_url if @uri_parameters.nil? || @uri_parameters.empty?
|
38
|
+
params_with_values = []
|
39
|
+
@uri_parameters.each {|k,v| params_with_values << "#{k}=#{v}"}
|
40
|
+
@base_url+'?'+params_with_values.join('&')
|
158
41
|
end
|
159
42
|
|
160
|
-
#
|
161
|
-
|
162
|
-
|
163
|
-
def results
|
164
|
-
stock = {}
|
165
|
-
get_values.each_with_index do |values, index|
|
166
|
-
parsed_values = values.split(',')
|
167
|
-
stock[stock_symbols[index]] ||= {}
|
168
|
-
parsed_values.each_with_index do |value, i|
|
169
|
-
stock[stock_symbols[index]][yahoo_url_parameters[i]] = value
|
170
|
-
end
|
171
|
-
end
|
172
|
-
stock
|
43
|
+
# Get result string
|
44
|
+
def values
|
45
|
+
@values ||= get
|
173
46
|
end
|
174
47
|
|
175
|
-
|
176
|
-
|
177
|
-
symbols.each do |symbol|
|
178
|
-
unless stock_symbols.include?(symbol)
|
179
|
-
stock_symbols << symbol
|
180
|
-
end
|
181
|
-
end
|
48
|
+
def update
|
49
|
+
@values = nil
|
182
50
|
end
|
183
51
|
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
52
|
+
private
|
53
|
+
|
54
|
+
# Generate error for debugging when something goes wrong while sending
|
55
|
+
# request to yahoo
|
56
|
+
def response_error(response)
|
57
|
+
trace = "Response \n\nbody : #{response.body}\n\ncode: #{response.code}\n\nmessage: #{response.message}"
|
58
|
+
case @base_url
|
59
|
+
when BASE_URLS[:quote]
|
60
|
+
raise InterfaceError, "Error :: Could not get stock data \n\n#{trace}"
|
61
|
+
when BASE_URLS[:history]
|
62
|
+
raise InterfaceError, "Error :: Could not get historical data \n\n#{trace}"
|
63
|
+
when BASE_URLS[:scrip_symbol]
|
64
|
+
raise InterfaceError, "Error :: Could not get stock symbol \n\n#{trace}"
|
65
|
+
else
|
66
|
+
raise InterfaceError, "Error connecting to #{@base_url} \n\n#{trace}"
|
191
67
|
end
|
192
68
|
end
|
193
69
|
|
194
|
-
# Add parameters to the url.
|
195
|
-
def add_parameters(*parameters)
|
196
|
-
parameters.each do |parameter|
|
197
|
-
unless allowed_parameters.include?(parameter)
|
198
|
-
raise InterfaceError, "Interface parameter #{parameter} is not a valid parameter."
|
199
|
-
end
|
200
|
-
unless yahoo_url_parameters.include?(parameter)
|
201
|
-
yahoo_url_parameters << parameter
|
202
|
-
end
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
# Remove parameters from the url.
|
207
|
-
def remove_parameters(*parameters)
|
208
|
-
parameters.each do |parameter|
|
209
|
-
unless yahoo_url_parameters.include?(parameter)
|
210
|
-
raise InterfaceError, "Parameter #{parameter} is not present in current list"
|
211
|
-
end
|
212
|
-
yahoo_url_parameters.reject!{|parameter_key| parameter_key == parameter}
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
# Returns an array of parameters that can be passed to yahoo.
|
217
|
-
def allowed_parameters
|
218
|
-
parameters.keys
|
219
|
-
end
|
220
|
-
|
221
|
-
# Add standard parameters
|
222
|
-
def add_standard_params
|
223
|
-
STD_PARAMETERS.keys.each { |parameter| add_parameters(parameter) }
|
224
|
-
end
|
225
|
-
|
226
|
-
# Add extended parameters
|
227
|
-
def add_extended_params
|
228
|
-
EXTENDED_PARAMETERS.keys.each { |parameter| add_parameters(parameter) }
|
229
|
-
end
|
230
|
-
|
231
|
-
# Add realtime parameters
|
232
|
-
def add_realtime_params
|
233
|
-
REALTIME_PARAMETERS.keys.each { |parameter| add_parameters(parameter) }
|
234
|
-
end
|
235
|
-
|
236
|
-
private
|
237
|
-
|
238
|
-
def parameters
|
239
|
-
STD_PARAMETERS.merge!(EXTENDED_PARAMETERS).merge!(REALTIME_PARAMETERS)
|
240
|
-
end
|
241
|
-
|
242
70
|
end
|
243
71
|
end
|