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.
@@ -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
@@ -0,0 +1,120 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe YahooStock::Interface::ScripSymbol do
4
+ before(:each) do
5
+ @interface = YahooStock::Interface::ScripSymbol.new('company name')
6
+ end
7
+
8
+ describe ".new" do
9
+ it "should replace any space from company name with +" do
10
+ @interface.company.should eql('company+name')
11
+ end
12
+
13
+ end
14
+ describe "get" do
15
+ before(:each) do
16
+ @response = stub('Response')
17
+ Net::HTTP.stub!(:get_response).and_return(@response)
18
+ URI.stub!(:parse)
19
+ @response.stub!(:code).and_return('200')
20
+ @response.stub!(:body)
21
+ end
22
+
23
+ it "should find or generate the uri once in the get method and once in the get method of the super class" do
24
+ @interface.should_receive(:uri).twice
25
+ @interface.get
26
+ end
27
+
28
+ it "should get response for the uri" do
29
+ Net::HTTP.should_receive(:get_response).and_return(@response)
30
+ @interface.get
31
+ end
32
+
33
+ it "should parse the uri" do
34
+ URI.should_receive(:parse)
35
+ @interface.get
36
+ end
37
+
38
+ it "should check the response code" do
39
+ @response.should_receive(:code)
40
+ @interface.get
41
+ end
42
+
43
+ it "should get the body of the response if returned code is 200, ie success" do
44
+ @response.should_receive(:body)
45
+ @interface.get
46
+ end
47
+ end
48
+
49
+ describe "uri" do
50
+ it "should generate full url with all paramenters" do
51
+ @interface.base_url = 'http://download.finance.yaaaaahoo.com/d/quotes.csv'
52
+ @interface.uri.should eql('http://download.finance.yaaaaahoo.com/d/quotes.csv?s=company+name')
53
+ end
54
+ end
55
+
56
+ describe "values" do
57
+ before(:each) do
58
+ @text = "<tr><td>Company Name</td><td class ='dd'>symbol</td><td class='ss'>price</td></tr>"
59
+ @scrip_symbol = YahooStock::Interface::ScripSymbol.new('a company')
60
+ @scrip_symbol.stub!(:text_range).and_return(@text)
61
+ end
62
+
63
+ it "should use the get values pvt method to generate the values string" do
64
+ @scrip_symbol.should_receive(:get_values)
65
+ @scrip_symbol.values
66
+ end
67
+
68
+ it "should not call the get values pvt method if the value is already present" do
69
+ @scrip_symbol.stub!(:values).and_return('somthing')
70
+ @scrip_symbol.should_not_receive(:get_values)
71
+ @scrip_symbol.values
72
+ end
73
+
74
+ it "should return a comma separated string" do
75
+ @scrip_symbol.values.should eql('Company Name,symbol,price')
76
+ end
77
+
78
+ it "should return a comma separated string with line break chars if more than one row is present" do
79
+ @text += "<tr><td>Company Name</td><td class ='dd'>symbol</td><td class='ss'>price</td></tr>"
80
+ @scrip_symbol.stub!(:text_range).and_return(@text)
81
+ @scrip_symbol.values.should eql("Company Name,symbol,price\r\nCompany Name,symbol,price")
82
+ end
83
+
84
+ it "should remove any hyperlinks" do
85
+ @text += "<tr><td><a href='asd'>Company Name</a></td><td class ='dd'>symbol</td><td class='ss'>price</td></tr>"
86
+ @scrip_symbol.stub!(:text_range).and_return(@text)
87
+ @scrip_symbol.values.should eql("Company Name,symbol,price\r\nCompany Name,symbol,price")
88
+ end
89
+
90
+ it "should not get rid of any blank values" do
91
+ @text += "<tr><td></td><td class ='dd'>symbol</td><td class='ss'>price</td></tr>"
92
+ @scrip_symbol.stub!(:text_range).and_return(@text)
93
+ @scrip_symbol.values.should eql("Company Name,symbol,price\r\n,symbol,price")
94
+ end
95
+
96
+ end
97
+
98
+ describe "setting company" do
99
+ it "should replace any space from the company name with +" do
100
+ @interface.company = 'new company'
101
+ @interface.company.should eql('new+company')
102
+ end
103
+
104
+ it "should set changed to true if company has changed" do
105
+ @interface.should_receive(:changed)
106
+ @interface.company = 'new company'
107
+ end
108
+
109
+ it "should set changed to false if company has not changed" do
110
+ @interface.should_receive(:changed).with(false)
111
+ @interface.company = 'company name'
112
+ end
113
+
114
+ it "should notify any observers about the state of the object when company attribute is set" do
115
+ @interface.should_receive(:notify_observers)
116
+ @interface.company = 'company name'
117
+ end
118
+ end
119
+
120
+ end