they-yahoo_stock 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/History.txt +98 -0
  2. data/Manifest.txt +37 -0
  3. data/README.rdoc +151 -0
  4. data/Rakefile +12 -0
  5. data/features/history.feature +23 -0
  6. data/features/quotes.feature +23 -0
  7. data/features/script_symbol.feature +23 -0
  8. data/features/step_definitions/history_steps.rb +44 -0
  9. data/features/step_definitions/quotes_steps.rb +45 -0
  10. data/features/step_definitions/script_symbol_steps.rb +40 -0
  11. data/lib/yahoo_stock.rb +21 -0
  12. data/lib/yahoo_stock/base.rb +37 -0
  13. data/lib/yahoo_stock/history.rb +32 -0
  14. data/lib/yahoo_stock/interface.rb +71 -0
  15. data/lib/yahoo_stock/interface/history.rb +187 -0
  16. data/lib/yahoo_stock/interface/quote.rb +287 -0
  17. data/lib/yahoo_stock/interface/scrip_symbol.rb +74 -0
  18. data/lib/yahoo_stock/quote.rb +158 -0
  19. data/lib/yahoo_stock/result.rb +21 -0
  20. data/lib/yahoo_stock/result/array_format.rb +23 -0
  21. data/lib/yahoo_stock/result/hash_format.rb +37 -0
  22. data/lib/yahoo_stock/result/xml_format.rb +37 -0
  23. data/lib/yahoo_stock/scrip_symbol.rb +61 -0
  24. data/spec/spec_helper.rb +4 -0
  25. data/spec/yahoo_stock/base_spec.rb +48 -0
  26. data/spec/yahoo_stock/history_spec.rb +75 -0
  27. data/spec/yahoo_stock/interface/history_spec.rb +317 -0
  28. data/spec/yahoo_stock/interface/quote_spec.rb +414 -0
  29. data/spec/yahoo_stock/interface/scrip_symbol_spec.rb +120 -0
  30. data/spec/yahoo_stock/interface_spec.rb +112 -0
  31. data/spec/yahoo_stock/quote_spec.rb +258 -0
  32. data/spec/yahoo_stock/result/array_format_spec.rb +14 -0
  33. data/spec/yahoo_stock/result/hash_format_spec.rb +65 -0
  34. data/spec/yahoo_stock/result/xml_format_spec.rb +54 -0
  35. data/spec/yahoo_stock/result_spec.rb +33 -0
  36. data/spec/yahoo_stock/scrip_symbol_spec.rb +46 -0
  37. metadata +114 -0
@@ -0,0 +1,75 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe YahooStock::History do
4
+
5
+ describe ".new" do
6
+
7
+ it "should raise error when no parameter is passed during object initialization" do
8
+ lambda { YahooStock::History.new
9
+ }.should raise_error(ArgumentError)
10
+ end
11
+
12
+ it "should raise error when the parameter is not a hash" do
13
+ lambda { YahooStock::History.new(nil)
14
+ }.should raise_error(YahooStock::History::HistoryError, 'A hash of start_date, end_date and stock_symbol is expected as parameters')
15
+ end
16
+
17
+ it "should inititalize the history interface" do
18
+ options = {}
19
+ YahooStock::Interface::History.should_receive(:new).with(options)
20
+ YahooStock::History.new({})
21
+ end
22
+
23
+ end
24
+
25
+ describe "find" do
26
+ before(:each) do
27
+ @interface = stub('YahooStock::Interface::History')
28
+ YahooStock::Interface::History.stub!(:new).and_return(@interface)
29
+ @interface.stub!(:values)
30
+ @history = YahooStock::History.new(:stock_symbol => 'a symbol', :start_date => Date.today-3, :end_date => Date.today-1)
31
+ end
32
+
33
+ it "should find values using interface" do
34
+ @interface.should_receive(:values).and_return('data')
35
+ @history.find
36
+ end
37
+
38
+ it "should remove the first line from the data as it is just the headings" do
39
+ data = 'data returned'
40
+ @interface.stub!(:values).and_return(data)
41
+ data.should_receive(:sub).with(/Date.*\s/,'')
42
+ @history.find
43
+ end
44
+ end
45
+
46
+ describe "data_attributes" do
47
+ before(:each) do
48
+ @history = YahooStock::History.new(:stock_symbol => 'a symbol', :start_date => Date.today-3, :end_date => Date.today-1)
49
+ @data = "Date, price, etc \n23, 34, 44\n\n222,234,2"
50
+ @history.stub!(:values_with_header).and_return(@data)
51
+ end
52
+
53
+ it "should return nil if history values are not present" do
54
+ @history.stub!(:values_with_header)
55
+ @history.data_attributes.should eql(nil)
56
+ end
57
+
58
+ it "should return only header values if history values are present" do
59
+ @history.data_attributes.should eql(["Date","price","etc"])
60
+ end
61
+
62
+ end
63
+
64
+ describe "values_with_header" do
65
+
66
+ it "should get values from the interface" do
67
+ @interface = stub('YahooStock::Interface::History')
68
+ YahooStock::Interface::History.stub!(:new).and_return(@interface)
69
+ @history = YahooStock::History.new(:stock_symbol => 'a symbol', :start_date => Date.today-3, :end_date => Date.today-1)
70
+ @interface.should_receive(:values)
71
+ @history.values_with_header
72
+ end
73
+ end
74
+
75
+ end
@@ -0,0 +1,317 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe YahooStock::Interface::History do
4
+
5
+ describe ".new" do
6
+
7
+ it "should raise error when no parameter is passed during object initialization" do
8
+ lambda { YahooStock::Interface::History.new
9
+ }.should raise_error(ArgumentError)
10
+ end
11
+
12
+ it "should raise an error if stock symbol key is not present in parameter hash" do
13
+ lambda { YahooStock::Interface::History.new(:start_date => Date.today-7)
14
+ }.should raise_error(YahooStock::Interface::History::HistoryError, ':stock_symbol key is not present in the parameter hash')
15
+ end
16
+
17
+ it "should raise error when stock_symbol value in hash is nil" do
18
+ lambda { YahooStock::Interface::History.new(:stock_symbol => nil)
19
+ }.should raise_error(YahooStock::Interface::History::HistoryError, ':stock_symbol value cannot be nil or blank')
20
+ end
21
+
22
+ it "should raise error when stock_symbol value in hash is blank" do
23
+ lambda { YahooStock::Interface::History.new(:stock_symbol => '')
24
+ }.should raise_error(YahooStock::Interface::History::HistoryError, ':stock_symbol value cannot be nil or blank')
25
+ end
26
+
27
+ it "should raise error when start date key is not present in parameter hash" do
28
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd') }.should raise_error(YahooStock::Interface::History::HistoryError, ':start_date key is not present in the parameter hash')
29
+ end
30
+
31
+ it "should raise error when start_date value is nil" do
32
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd', :start_date => nil) }.should raise_error(YahooStock::Interface::History::HistoryError, ':start_date value cannot be blank')
33
+ end
34
+
35
+ it "should raise error when start_date value is blank" do
36
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd', :start_date => '') }.should raise_error(YahooStock::Interface::History::HistoryError, ':start_date value cannot be blank')
37
+ end
38
+
39
+ it "should raise error when start date value is not of type date" do
40
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd', :start_date => '23/3/2009') }.should raise_error(YahooStock::Interface::History::HistoryError, 'Start date must be of type Date')
41
+ end
42
+
43
+ it "should raise error when end date key is not present in parameter hash" do
44
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd', :start_date => Date.today-1) }.should raise_error(YahooStock::Interface::History::HistoryError, ':end_date key is not present in the parameter hash')
45
+ end
46
+
47
+ it "should raise error when end_date value is nil" do
48
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd',
49
+ :start_date => Date.today-1,
50
+ :end_date => nil) }.should raise_error(YahooStock::Interface::History::HistoryError, ':end_date value cannot be blank')
51
+ end
52
+
53
+ it "should raise error when end_date value is blank" do
54
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd',
55
+ :start_date => Date.today-1,
56
+ :end_date => '') }.should raise_error(YahooStock::Interface::History::HistoryError, ':end_date value cannot be blank')
57
+ end
58
+
59
+ it "should raise error when end date value is not of type date" do
60
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd',
61
+ :start_date => Date.today-1,
62
+ :end_date => 'sds') }.should raise_error(YahooStock::Interface::History::HistoryError, 'End date must be of type Date')
63
+ end
64
+
65
+ it "should raise error when start date is not less than today" do
66
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd',
67
+ :start_date => Date.today,
68
+ :end_date => Date.today)
69
+ }.should raise_error(YahooStock::Interface::History::HistoryError, 'Start date must be in the past')
70
+ end
71
+
72
+ it "should raise error when start date is greater than end date" do
73
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd',
74
+ :start_date => Date.today-7,
75
+ :end_date => Date.today-8)
76
+ }.should raise_error(YahooStock::Interface::History::HistoryError, 'End date must be greater than the start date')
77
+ end
78
+
79
+ it "should not raise error when start date is in the past, end date is greater than start date and all relevant keys are present with right type" do
80
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd',
81
+ :start_date => Date.today-7,
82
+ :end_date => Date.today-1)
83
+ }.should_not raise_error
84
+
85
+ end
86
+
87
+ it "should by default set type to daily if no value provided for it" do
88
+ @history = YahooStock::Interface::History.new(:stock_symbol => 'd',
89
+ :start_date => Date.today-7,
90
+ :end_date => Date.today-1)
91
+ @history.type.should eql(:daily)
92
+ end
93
+
94
+ it "should raise invalid keys error when an invalid key is passed in the parameter" do
95
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd',
96
+ :start_date => Date.today-7,
97
+ :end_date => Date.today-1, :boom => 1) }.should raise_error(YahooStock::Interface::History::HistoryError, "An invalid key 'boom' is passed in the parameters. Allowed keys are stock_symbol, start_date, end_date, type")
98
+ end
99
+
100
+ it "should raise error when type is neither :daily, :weekly, :monthly or :dividend" do
101
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd',
102
+ :start_date => Date.today-7,
103
+ :end_date => Date.today-1,
104
+ :type => :yearly)
105
+ }.should raise_error(YahooStock::Interface::History::HistoryError, "Allowed values for type are daily, weekly, monthly, dividend")
106
+
107
+ end
108
+
109
+ it "should not raise error when type is :daily" do
110
+ lambda { YahooStock::Interface::History.new(:stock_symbol => 'd',
111
+ :start_date => Date.today-7,
112
+ :end_date => Date.today-1,
113
+ :type => :daily)
114
+ }.should_not raise_error
115
+
116
+ end
117
+ end
118
+
119
+ describe "setters" do
120
+ before(:each) do
121
+ @history = YahooStock::Interface::History.new(:stock_symbol => 'd',
122
+ :start_date => Date.today-7,
123
+ :end_date => Date.today-1)
124
+ end
125
+
126
+ it "should raise error when stock symbol is being set to nil" do
127
+ lambda { @history.stock_symbol = nil
128
+ }.should raise_error(YahooStock::Interface::History::HistoryError, ':stock_symbol value cannot be nil or blank')
129
+ end
130
+
131
+ it "should raise error when stock symbol is being set to an empty string" do
132
+ lambda { @history.stock_symbol = ''
133
+ }.should raise_error(YahooStock::Interface::History::HistoryError, ':stock_symbol value cannot be nil or blank')
134
+ end
135
+
136
+ it "should set the new stock symbol value when it is valid" do
137
+ @history.stock_symbol = 'new_val'
138
+ @history.stock_symbol.should eql('new_val')
139
+ end
140
+
141
+ it "should raise error when start date type is not a date" do
142
+ lambda { @history.start_date = '21/3/2009'
143
+ }.should raise_error(YahooStock::Interface::History::HistoryError, 'Start date must be of type Date')
144
+ end
145
+
146
+ it "should raise error when start date is not in the past" do
147
+ lambda { @history.start_date = Date.today
148
+ }.should raise_error(YahooStock::Interface::History::HistoryError, 'Start date must be in the past')
149
+ end
150
+
151
+ it "should set the start date when start date is valid" do
152
+ s_date = Date.today-1
153
+ @history.start_date = s_date
154
+ @history.start_date.should eql(s_date)
155
+ end
156
+
157
+ it "should raise error when end date type is not a date" do
158
+ lambda { @history.end_date = '21/3/2009'
159
+ }.should raise_error(YahooStock::Interface::History::HistoryError, 'End date must be of type Date')
160
+ end
161
+
162
+ it "should raise error when end date type is not in the past" do
163
+ lambda { @history.end_date = Date.today
164
+ }.should raise_error(YahooStock::Interface::History::HistoryError, 'End date must be in the past')
165
+ end
166
+
167
+ it "should raise error when end date type is not in the past" do
168
+ e_date = Date.today-1
169
+ @history.end_date = e_date
170
+ @history.end_date.should eql(e_date)
171
+ end
172
+
173
+ it "should raise error when settin type other than :daily, :monthly or :weekly" do
174
+ lambda { @history.type = :yearly }.should raise_error(YahooStock::Interface::History::HistoryError)
175
+ end
176
+
177
+ it "should set the type" do
178
+ @history.type = :daily
179
+ @history.type.should eql(:daily)
180
+ end
181
+
182
+ it "should set the type default to daily if not set" do
183
+ @history.type.should eql(:daily)
184
+ end
185
+ end
186
+
187
+ describe "uri" do
188
+ before(:each) do
189
+ @history = YahooStock::Interface::History.new(:stock_symbol => 'symbol',
190
+ :start_date => Date.today-7,
191
+ :end_date => Date.today-1)
192
+ end
193
+
194
+ it "should get the type to generate url" do
195
+ @history.should_receive(:type)
196
+ @history.uri
197
+ end
198
+
199
+ it "should get the day of the start date" do
200
+ @history.start_date.should_receive(:day)
201
+ @history.uri
202
+ end
203
+
204
+ it "should get the day of the end date" do
205
+ @history.end_date.should_receive(:day)
206
+ @history.uri
207
+ end
208
+
209
+ it "should get the year of the start date" do
210
+ @history.start_date.should_receive(:year)
211
+ @history.uri
212
+ end
213
+
214
+ it "should get the day of the end date" do
215
+ @history.start_date.should_receive(:year)
216
+ @history.uri
217
+ end
218
+
219
+ it "should get the month of the start and end date" do
220
+ @history.should_receive(:sprintf).exactly(2)
221
+ @history.uri
222
+ end
223
+
224
+ it "should have the base url" do
225
+ @history.uri.should =~ /http:\/\/ichart.finance.yahoo.com\/table.csv?/
226
+ end
227
+
228
+ it "should have parameter 'a' with month -1 " do
229
+ month = sprintf("%02d", @history.start_date.month-1)
230
+ @history.uri.should =~ /a=#{month}/
231
+ end
232
+
233
+ it "should have parameter 'b' with start date day " do
234
+ @history.uri.should =~ /b=#{@history.start_date.day}/
235
+ end
236
+
237
+ it "should have parameter 'c' with start date year " do
238
+ @history.uri.should =~ /c=#{@history.start_date.year}/
239
+ end
240
+
241
+ it "should have parameter 'd' with end date month -1 " do
242
+ month = sprintf("%02d", @history.end_date.month-1)
243
+ @history.uri.should =~ /d=#{month}/
244
+ end
245
+
246
+ it "should have parameter 'e' with end date day " do
247
+ @history.uri.should =~ /e=#{@history.end_date.day}/
248
+ end
249
+
250
+ it "should have parameter 'f' with end date year " do
251
+ @history.uri.should =~ /c=#{@history.end_date.year}/
252
+ end
253
+
254
+ it "should have parameter 'g' with type" do
255
+ @history.uri.should =~ /g=d/
256
+ end
257
+
258
+ it "should have the parameter 's' with stock symbol" do
259
+ @history.uri.should =~ /s=symbol/
260
+ end
261
+
262
+ end
263
+
264
+ describe "get" do
265
+ before(:each) do
266
+ @history = YahooStock::Interface::History.new(:stock_symbol => 'd',
267
+ :start_date => Date.today-7,
268
+ :end_date => Date.today-1)
269
+ response = stub('HTTP')
270
+ Net::HTTP.stub!(:get_response).and_return(response)
271
+ response.stub!(:code).and_return('200')
272
+ response.stub!(:body)
273
+ end
274
+
275
+ it "should check the stock symbol" do
276
+ @history.should_receive(:stock_symbol).times.at_least(2).and_return('symbol')
277
+ @history.get
278
+ end
279
+
280
+ it "should check the end date" do
281
+ @history.should_receive(:end_date).times.at_least(1).and_return(Date.today-1)
282
+ @history.get
283
+ end
284
+
285
+ it "should check the start date" do
286
+ @history.should_receive(:start_date).times.at_least(1).and_return(Date.today-7)
287
+ @history.get
288
+ end
289
+
290
+ it "should raise error if start date is not present" do
291
+ @history.stub!(:start_date)
292
+ lambda { @history.get }.should raise_error('Cannot send request unless all parameters, ie stock_symbol, start and end date are present')
293
+ end
294
+
295
+ it "should raise error if end date is not present" do
296
+ @history.stub!(:end_date)
297
+ lambda { @history.get }.should raise_error('Cannot send request unless all parameters, ie stock_symbol, start and end date are present')
298
+ end
299
+
300
+ it "should raise error if stock symbol is not present" do
301
+ @history.stub!(:stock_symbol)
302
+ lambda { @history.get }.should raise_error('Cannot send request unless all parameters, ie stock_symbol, start and end date are present')
303
+ end
304
+
305
+ it "should send the http request to yahoo" do
306
+ Net::HTTP.should_receive(:get_response)
307
+ @history.get
308
+ end
309
+
310
+ it "should parse the url" do
311
+ URI.should_receive(:parse)
312
+ @history.get
313
+ end
314
+
315
+ end
316
+
317
+ end
@@ -0,0 +1,414 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe YahooStock::Interface::Quote::Quote do
4
+
5
+ before(:each) do
6
+ @interface = YahooStock::Interface::Quote.new(:stock_symbols => ['sym1'], :read_parameters => [:param1])
7
+ @allowed_parameters = [:param1, :param2, :param3, :zxy, :xyz, :abc, :def]
8
+ end
9
+
10
+ describe ".new" do
11
+
12
+ it "should add new parameters after current parametr and sort the url parameters" do
13
+ @interface.stub!(:allowed_parameters).and_return(@allowed_parameters)
14
+ @interface.add_parameters :zxy, :xyz
15
+ @interface.yahoo_url_parameters.should eql([:param1, :xyz, :zxy])
16
+ end
17
+
18
+ it "should sort the url parameters" do
19
+ @interface.stub!(:allowed_parameters).and_return(@allowed_parameters)
20
+ @interface.add_parameters :def, :abc
21
+ @interface.yahoo_url_parameters.should eql([:abc, :def, :param1])
22
+ end
23
+
24
+ it "should raise InterfaceError when stock params hash is nil" do
25
+ lambda { YahooStock::Interface::Quote.new(nil)
26
+ }.should raise_error(YahooStock::Interface::Quote::QuoteError, 'You must pass a hash of stock symbols and the data you would like to see')
27
+ end
28
+
29
+ it "should raise ArgumentError when no parameter is passed" do
30
+ lambda { YahooStock::Interface::Quote.new }.should raise_error(ArgumentError)
31
+ end
32
+
33
+ it "should raise InterfaceError when stock symbols hash is nil" do
34
+ lambda { YahooStock::Interface::Quote.new(:stock_symbols => nil) }.should raise_error(YahooStock::Interface::Quote::QuoteError, 'No stocks passed')
35
+ end
36
+
37
+ it "should raise InterfaceError when stock symbols parameter hash is an array of zero elements" do
38
+ lambda { YahooStock::Interface::Quote.new(:stock_symbols => []) }.should raise_error(YahooStock::Interface::Quote::QuoteError, 'No stocks passed')
39
+ end
40
+
41
+ it "should raise InterfaceError when read parameters hash is nil" do
42
+ lambda { YahooStock::Interface::Quote.new(:stock_symbols => 'sym', :read_parameters => nil) }.should raise_error(YahooStock::Interface::Quote::QuoteError,'Read parameters are not provided')
43
+ end
44
+
45
+ it "should raise InterfaceError when read parameters hash is a zero element array" do
46
+ lambda { YahooStock::Interface::Quote.new(:stock_symbols => 'sym', :read_parameters => []) }.should raise_error(YahooStock::Interface::Quote::QuoteError,'Read parameters are not provided')
47
+ end
48
+
49
+ it "should assign appropriate values to the stock symbols accessor" do
50
+ interface = YahooStock::Interface::Quote.new(:stock_symbols => ['sym'], :read_parameters => [:param1])
51
+ interface.stock_symbols.should eql(['sym'])
52
+ end
53
+
54
+ it "should assign appropriate values to the yahoo url parameters accessor" do
55
+ interface = YahooStock::Interface::Quote.new(:stock_symbols => ['sym'], :read_parameters => [:param1])
56
+ interface.yahoo_url_parameters.should eql([:param1])
57
+ end
58
+
59
+ it "should have the base url" do
60
+ interface = YahooStock::Interface::Quote.new(:stock_symbols => ['sym'], :read_parameters => [:param1])
61
+ interface.base_url = 'http://download.finance.yahoo.com/d/quotes.csv'
62
+ end
63
+
64
+ it "should add the self as an observer to reset the values if any symbol or parameter is modified" do
65
+ interface = YahooStock::Interface::Quote.new(:stock_symbols => ['sym'], :read_parameters => [:param1])
66
+ interface.count_observers.should eql(1)
67
+ end
68
+
69
+ it "should not have zero observer" do
70
+ interface = YahooStock::Interface::Quote.new(:stock_symbols => ['sym'], :read_parameters => [:param1])
71
+ interface.count_observers.should_not eql(0)
72
+ end
73
+ end
74
+
75
+ describe "scrip_symbols=" do
76
+
77
+ it "should add the new symbol to the existing stock symbol array" do
78
+ @interface.stock_symbols = 'sym2'
79
+ @interface.stock_symbols.should include('sym2','sym1')
80
+ end
81
+
82
+ it "should not add duplicate symbols more than once" do
83
+ @interface.stock_symbols = 'sym2'
84
+ @interface.stock_symbols = 'sym2'
85
+ @interface.stock_symbols.size.should eql(2)
86
+ end
87
+
88
+ it "should not run observers if symbols havent change" do
89
+ @interface.stock_symbols = 'sym1'
90
+ @interface.should_not_receive(:run_observers)
91
+ end
92
+
93
+ it "should run observers if stock symbols have changed" do
94
+ @interface.stock_symbols.should eql(['sym1'])
95
+ @interface.should_receive(:run_observers)
96
+ @interface.stock_symbols = 'sym2'
97
+ @interface.stock_symbols.should include('sym2','sym1')
98
+ end
99
+ end
100
+
101
+ describe "yahoo_url_parameters=" do
102
+ before(:each) do
103
+ @interface.stub!(:allowed_parameters).and_return(@allowed_parameters)
104
+ end
105
+ it "should raise error when an invalid parameter is passed" do
106
+ lambda {
107
+ @interface.yahoo_url_parameters = :random_param
108
+ }.should raise_error(YahooStock::Interface::Quote::QuoteError, "Interface parameter random_param is not a valid parameter.")
109
+ end
110
+
111
+ it "should add the new param to the existing params array" do
112
+ @interface.yahoo_url_parameters = :param2
113
+ @interface.yahoo_url_parameters.should include(:param1, :param2)
114
+ end
115
+
116
+ it "should not add duplicate params more than once" do
117
+ @interface.yahoo_url_parameters = :param1
118
+ @interface.yahoo_url_parameters = :param1
119
+ @interface.yahoo_url_parameters.size.should eql(1)
120
+ end
121
+
122
+ it "should not run observers if parameters havent change" do
123
+ @interface.yahoo_url_parameters = :param1
124
+ @interface.should_not_receive(:run_observers)
125
+ end
126
+
127
+ it "should run observers if stock symbols have changed" do
128
+ @interface.yahoo_url_parameters.should eql([:param1])
129
+ @interface.should_receive(:run_observers)
130
+ @interface.yahoo_url_parameters = :param2
131
+ @interface.yahoo_url_parameters.should include(:param1, :param2)
132
+ end
133
+
134
+ end
135
+
136
+ describe "uri" do
137
+ before(:each) do
138
+ @interface.stub!(:yahoo_url_parameters).and_return([:param1])
139
+ @interface.stub!(:parameters).and_return({:param1 => 's', :param2 => 'w', :param3 => 't'})
140
+ end
141
+
142
+ it "should raise InterfaceError if the passed parameter is not present in the parameter list" do
143
+ interface = YahooStock::Interface::Quote.new(:stock_symbols => ['sym'], :read_parameters => [:param4, :param5])
144
+ lambda { interface.uri }.should raise_error(YahooStock::Interface::Quote::QuoteError, "The parameters 'param4, param5' are not valid. Please check using YahooStock::Interface::Quote#allowed_parameters or YahooStock::Quote#valid_parameters")
145
+ end
146
+
147
+ it "should not raise any error if parameters passed are correct" do
148
+ lambda { @interface.uri }.should_not raise_error
149
+ end
150
+
151
+ it "should raise error if stock symbols accessor is cleared and empty" do
152
+ @interface.stock_symbols.clear
153
+ lambda { @interface.uri }.should raise_error(YahooStock::Interface::Quote::QuoteError, "You must add atleast one stock symbol to get stock data")
154
+ end
155
+
156
+ it "should raise error if url parameters are cleared and empty" do
157
+ @interface.yahoo_url_parameters.clear
158
+ lambda { @interface.uri }.should raise_error(YahooStock::Interface::Quote::QuoteError, "You must add atleast one parameter to get stock data")
159
+ end
160
+
161
+ it "should create the uri with base url" do
162
+ @interface.uri.should =~ /http:\/\/download.finance.yahoo.com\/d\/quotes.csv?/
163
+ end
164
+
165
+ it "should create the uri with stock symbol parameter" do
166
+ @interface.uri.should =~ /s=sym1/
167
+ end
168
+
169
+ it "should create the uri with parameter code" do
170
+ @interface.uri.should =~ /f=s/
171
+ end
172
+
173
+ it "should have an & to join symbols and data parameter" do
174
+ @interface.uri.should =~ /&/
175
+ end
176
+
177
+ it "should create the uri with 3 symbols" do
178
+ @interface.stub!(:stock_symbols).and_return(['sym1','sym2', 'sym3'])
179
+ @interface.uri.should =~ /s=sym1\+sym2\+sym3/
180
+ end
181
+
182
+ it "should create the uri with 3 parameters" do
183
+ @interface.stub!(:yahoo_url_parameters).and_return([:param1,:param2, :param3])
184
+ @interface.uri.should =~ /f=swt/
185
+ end
186
+
187
+ end
188
+
189
+ describe "get" do
190
+ before(:each) do
191
+ @response = stub('HTTP Response')
192
+ @response.stub!(:code).and_return('200')
193
+ @response.stub!(:body)
194
+ URI.stub!(:parse)
195
+ Net::HTTP.stub!(:get_response).and_return(@response)
196
+ @interface.stub!(:uri)
197
+ end
198
+
199
+ it "should use uri to get the content" do
200
+ @interface.should_receive(:uri).at_least(2)
201
+ @interface.get
202
+ end
203
+
204
+ it "should get response for the uri" do
205
+ Net::HTTP.should_receive(:get_response).and_return(@response)
206
+ @interface.get
207
+ end
208
+
209
+ it "should parse the uri" do
210
+ URI.should_receive(:parse)
211
+ @interface.get
212
+ end
213
+
214
+ it "should check the response code" do
215
+ @response.should_receive(:code)
216
+ @interface.get
217
+ end
218
+
219
+ it "should get the body of the response if returned code is 200, ie success" do
220
+ @response.stub!(:code).and_return('200')
221
+ @response.should_receive(:body)
222
+ @interface.get
223
+ end
224
+
225
+ end
226
+
227
+ describe "remove_parameters" do
228
+
229
+ it "should remove the parameters if they are present" do
230
+ params = [:test1, :test2, :test3]
231
+ @interface = YahooStock::Interface::Quote.new(:stock_symbols => ['sym1'], :read_parameters => params)
232
+ @interface.remove_parameters(:test1, :test2)
233
+ @interface.yahoo_url_parameters.should eql([:test3])
234
+ end
235
+
236
+ it "should raise an error when removing a parameter that is not present" do
237
+ lambda { @interface.remove_parameters(:test1) }.should raise_error(YahooStock::Interface::Quote::QuoteError, "Parameter test1 is not present in current list")
238
+ end
239
+ end
240
+
241
+ describe "add_parameters" do
242
+
243
+ it "should raise an error when the parameter is not a valid parameter" do
244
+ lambda { @interface.add_parameters('test1') }.should raise_error(YahooStock::Interface::Quote::QuoteError, "Interface parameter test1 is not a valid parameter.")
245
+ end
246
+
247
+ it "should add the parameter if its a valid parameter" do
248
+ params = [:test1, :test2, :test3]
249
+ @interface.stub!(:allowed_parameters).and_return(params)
250
+ @interface.add_parameters(:test1, :test2)
251
+ @interface.yahoo_url_parameters.should include(:test1, :test2)
252
+ end
253
+
254
+ it "should not add the parameter more than once in the parameter list and silently ignore it" do
255
+ params = [:test1, :test2, :test3]
256
+ @interface.stub!(:allowed_parameters).and_return(params)
257
+ @interface.add_parameters(:test1, :test2)
258
+ @interface.yahoo_url_parameters.should include(:test1, :test2)
259
+ @interface.yahoo_url_parameters.size.should eql(3)
260
+ @interface.add_parameters(:test1, :test2)
261
+ @interface.yahoo_url_parameters.should include(:test1, :test2)
262
+ @interface.yahoo_url_parameters.size.should eql(3)
263
+ end
264
+ end
265
+
266
+ describe "remove_symbols" do
267
+
268
+ it "should remove the symbols if they are present" do
269
+ symbols = ['test1', 'test2', 'test3']
270
+ @interface.stub!(:stock_symbols).and_return(symbols)
271
+ @interface.remove_symbols('test1', 'test2')
272
+ @interface.stock_symbols.should eql(['test3'])
273
+ end
274
+
275
+ it "should raise an error when removing a symbol that is not present" do
276
+ lambda { @interface.remove_symbols('test1') }.should raise_error(YahooStock::Interface::Quote::QuoteError, "Cannot remove stock symbol test1 as it is currently not present.")
277
+ end
278
+ end
279
+
280
+ describe "add_symbols" do
281
+
282
+ it "should add the symbol" do
283
+ symbols = ['test1', 'test2', 'test3']
284
+ @interface.stub!(:stock_symbols).and_return(symbols)
285
+ @interface.add_symbols('test1', 'test2')
286
+ @interface.stock_symbols.should include('test1', 'test2')
287
+ end
288
+
289
+ it "should not add the symbol more than once in the symbol list and silently ignore the duplicate ones" do
290
+ @interface.add_symbols('test1', 'test2')
291
+ @interface.stock_symbols.should include('test1', 'test2')
292
+ @interface.stock_symbols.size.should eql(3)
293
+ @interface.add_symbols('test1', 'test2')
294
+ @interface.stock_symbols.should include('test1', 'test2')
295
+ @interface.stock_symbols.size.should eql(3)
296
+ end
297
+ end
298
+
299
+ describe "allowed_parameters" do
300
+
301
+ it "should find all parameters" do
302
+ @interface.should_receive(:parameters).and_return({})
303
+ @interface.allowed_parameters
304
+ end
305
+
306
+ it "should return the keys of all parameters" do
307
+ parameter_hash = {}
308
+ @interface.stub!(:parameters).and_return(parameter_hash)
309
+ parameter_hash.should_receive(:keys)
310
+ @interface.allowed_parameters
311
+ end
312
+
313
+ end
314
+
315
+ describe "add_standard_params" do
316
+
317
+ it "should get the keys for standard parameters" do
318
+ YahooStock::Interface::Quote::STD_PARAMETERS.should_receive(:keys).and_return([])
319
+ @interface.add_standard_params
320
+ end
321
+
322
+ it "should add each parameter" do
323
+ keys = [:a_key, :b_key]
324
+ YahooStock::Interface::Quote::STD_PARAMETERS.stub!(:keys).and_return(keys)
325
+ @interface.should_receive(:add_parameters).with(:a_key)
326
+ @interface.should_receive(:add_parameters).with(:b_key)
327
+ @interface.add_standard_params
328
+ end
329
+
330
+ it "should add grouped parameters" do
331
+ @interface.should_receive(:add_grouped_parameters)
332
+ @interface.add_extended_params
333
+ end
334
+
335
+ end
336
+
337
+ describe "add_extended_params" do
338
+
339
+ it "should get the keys for extended parameters" do
340
+ YahooStock::Interface::Quote::EXTENDED_PARAMETERS.should_receive(:keys).and_return([])
341
+ @interface.add_extended_params
342
+ end
343
+
344
+ it "should add each parameter" do
345
+ keys = [:a_key, :b_key]
346
+ YahooStock::Interface::Quote::EXTENDED_PARAMETERS.stub!(:keys).and_return(keys)
347
+ @interface.should_receive(:add_parameters).with(:a_key)
348
+ @interface.should_receive(:add_parameters).with(:b_key)
349
+ @interface.add_extended_params
350
+ end
351
+
352
+ it "should add grouped parameters" do
353
+ @interface.should_receive(:add_grouped_parameters)
354
+ @interface.add_extended_params
355
+ end
356
+
357
+ end
358
+
359
+ describe "add_realtime_params" do
360
+
361
+ it "should get the keys for realtime parameters" do
362
+ YahooStock::Interface::Quote::REALTIME_PARAMETERS.should_receive(:keys).and_return([])
363
+ @interface.add_realtime_params
364
+ end
365
+
366
+ it "should add each parameter" do
367
+ keys = [:a_key, :b_key]
368
+ YahooStock::Interface::Quote::REALTIME_PARAMETERS.stub!(:keys).and_return(keys)
369
+ @interface.should_receive(:add_parameters).with(:a_key)
370
+ @interface.should_receive(:add_parameters).with(:b_key)
371
+ @interface.add_realtime_params
372
+ end
373
+
374
+ it "should add grouped parameters" do
375
+ @interface.should_receive(:add_grouped_parameters)
376
+ @interface.add_extended_params
377
+ end
378
+
379
+ end
380
+
381
+ describe "clear_symbols" do
382
+ it "should get all stock symbols" do
383
+ @interface.should_receive(:stock_symbols).and_return([])
384
+ @interface.clear_symbols
385
+ end
386
+
387
+ it "should clear all stock symbols" do
388
+ sym = []
389
+ @interface.stub!(:stock_symbols).and_return(sym)
390
+ sym.should_receive(:clear)
391
+ @interface.clear_symbols
392
+ end
393
+
394
+ it "should run the observers" do
395
+ @interface.should_receive(:run_observers)
396
+ @interface.clear_symbols
397
+ end
398
+ end
399
+
400
+ describe "clear_parameters" do
401
+
402
+ it "should clear all yahoo url parameters" do
403
+ @interface.yahoo_url_parameters.should_not eql([])
404
+ @interface.clear_parameters
405
+ @interface.yahoo_url_parameters.should eql([])
406
+ end
407
+
408
+ it "should run the observers" do
409
+ @interface.should_receive(:run_observers)
410
+ @interface.clear_parameters
411
+ end
412
+ end
413
+
414
+ end