yahoo_stock 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,104 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe YahooStock::Interface do
4
+
5
+ before(:each) do
6
+ @interface = YahooStock::Interface.new
7
+ @interface.base_url = 'http://download.finance.yaaaaahoo.com/d/quotes.csv'
8
+ end
9
+ describe "uri" do
10
+ it "should return base url when uri_parameters is empty" do
11
+ @interface.uri.should eql('http://download.finance.yaaaaahoo.com/d/quotes.csv')
12
+ end
13
+
14
+ it "should raise error when base url is not present" do
15
+ @interface.base_url = nil
16
+ lambda { @interface.uri }.should raise_error(YahooStock::Interface::InterfaceError,
17
+ 'Base url is require to generate full uri.')
18
+ end
19
+
20
+ it "should generate full url with all paramenters" do
21
+ @interface.uri_parameters = {:s => 'boom', :f => 'toom', :k => 'zoom'}
22
+ @interface.uri.should =~ /http:\/\/download.finance.yaaaaahoo.com\/d\/quotes.csv?/
23
+ @interface.uri.should =~ /s=boom/
24
+ @interface.uri.should =~ /f=toom/
25
+ @interface.uri.should =~ /k=zoom/
26
+ end
27
+ end
28
+
29
+ describe "get" do
30
+ before(:each) do
31
+ @response = stub('HTTP Response')
32
+ @response.stub!(:code).and_return('200')
33
+ @response.stub!(:body)
34
+ URI.stub!(:parse)
35
+ Net::HTTP.stub!(:get_response).and_return(@response)
36
+ end
37
+
38
+ it "should get response for the uri" do
39
+ Net::HTTP.should_receive(:get_response).and_return(@response)
40
+ @interface.get
41
+ end
42
+
43
+ it "should parse the uri" do
44
+ URI.should_receive(:parse)
45
+ @interface.get
46
+ end
47
+
48
+ it "should check the response code" do
49
+ @response.should_receive(:code)
50
+ @interface.get
51
+ end
52
+
53
+ it "should get the body of the response if returned code is 200, ie success" do
54
+ @response.stub!(:code).and_return('200')
55
+ @response.should_receive(:body)
56
+ @interface.get
57
+ end
58
+
59
+ describe "when response code is not 200" do
60
+ before(:each) do
61
+ @response.stub!(:code).and_return('301')
62
+ @response.stub!(:body).and_return('something')
63
+ @response.stub!(:message).and_return('eerrred')
64
+ end
65
+
66
+ it "should return the response error when returned code is not 200" do
67
+ @interface.should_receive(:response_error)
68
+ @interface.get
69
+ end
70
+
71
+ it "should not get the body of the response when returned code is not 200" do
72
+ @interface.should_receive(:response_error)
73
+ @response.should_not_receive(:body)
74
+ @interface.get
75
+ end
76
+
77
+ it "should raise error when returned code is not 200" do
78
+ @interface.base_url = YahooStock::Interface::BASE_URLS[:quote]
79
+ lambda { @interface.get }.should raise_error
80
+ end
81
+ end
82
+
83
+ end
84
+
85
+ describe "values" do
86
+ it "should call get to receive the values" do
87
+ @interface.should_receive(:get)
88
+ @interface.values
89
+ end
90
+
91
+ it "should not call get when values are already set" do
92
+ @interface.stub!(:values).and_return('some string')
93
+ @interface.should_not_receive(:get)
94
+ @interface.values
95
+ end
96
+ end
97
+
98
+ describe "update" do
99
+ it "should set the values to nil" do
100
+ @interface.update.should be_nil
101
+ end
102
+ end
103
+
104
+ end
@@ -0,0 +1,258 @@
1
+ require File.join(File.dirname(__FILE__), "/../spec_helper")
2
+
3
+ module YahooStock
4
+ describe Quote do
5
+
6
+ describe "initialize" do
7
+
8
+ it "should raise error when no parameter is passed" do
9
+ lambda { YahooStock::Quote.new }.should raise_error
10
+ end
11
+
12
+ it "should raise QuoteException error when parameter passed is nil" do
13
+ lambda { YahooStock::Quote.new(nil)}.should raise_error(Quote::QuoteException, 'You must provide a hash of stock symbols to fetch data')
14
+ end
15
+
16
+ it "should raise QuoteException error when parameter passed is not a hash" do
17
+ lambda { YahooStock::Quote.new('erred')}.should raise_error(Quote::QuoteException, 'You must provide a hash of stock symbols to fetch data')
18
+ end
19
+
20
+ it "should raise QuoteException when a hash of stock_symbols are not passed" do
21
+ lambda { YahooStock::Quote.new(:misspelled => 'YHOO')}.should raise_error(Quote::QuoteException, 'You must provide atleast one stock symbol to fetch data')
22
+ end
23
+
24
+ it "should raise QuoteException when stock symbols hash key value is an emtpy array" do
25
+ lambda { YahooStock::Quote.new(:stock_symbols => [])}.should raise_error(Quote::QuoteException, 'You must provide atleast one stock symbol to fetch data')
26
+ end
27
+
28
+ it "should raise QuoteException when stock symbols hash key value is an emtpy string" do
29
+ lambda { YahooStock::Quote.new(:stock_symbols => '')}.should raise_error(Quote::QuoteException, 'You must provide atleast one stock symbol to fetch data')
30
+ end
31
+
32
+ it "should create 2 read parameter when no parameters are passed" do
33
+ quote = YahooStock::Quote.new(:stock_symbols => 'test')
34
+ quote.current_parameters.length.should eql(2)
35
+ end
36
+
37
+ it "should have last_trade_price_only key in the read parameters when no keys are passed" do
38
+ quote = YahooStock::Quote.new(:stock_symbols => 'test')
39
+ quote.current_parameters.should include(:last_trade_price_only)
40
+ end
41
+
42
+ it "should have last_trade_date key in the read parameters when no keys are passed" do
43
+ quote = YahooStock::Quote.new(:stock_symbols => 'test')
44
+ quote.current_parameters.should include(:last_trade_date)
45
+ end
46
+
47
+ it "should convert the symbols passed to an array when passed as a single string" do
48
+ quote = YahooStock::Quote.new(:stock_symbols => 'test')
49
+ quote.current_symbols.should eql(['test'])
50
+ end
51
+
52
+ it "should not keep stock symbols as a string" do
53
+ quote = YahooStock::Quote.new(:stock_symbols => 'test')
54
+ quote.current_symbols.should_not eql('test')
55
+ end
56
+
57
+ it "should create a new instance of the Stock Interface class" do
58
+ YahooStock::Interface::Quote.should_receive(:new)
59
+ YahooStock::Quote.new(:stock_symbols => 'test')
60
+ end
61
+
62
+ end
63
+
64
+ describe "subject" do
65
+
66
+ before(:each) do
67
+ @interface = mock(YahooStock::Interface::Quote)
68
+ YahooStock::Interface::Quote.stub!(:new).and_return(@interface)
69
+ @quote = YahooStock::Quote.new(:stock_symbols => 'MSFT')
70
+ end
71
+
72
+ describe "add_symbols" do
73
+
74
+ it "should add the symbol to existing symbols by calling add symbols on interface" do
75
+ @interface.should_receive(:add_symbols)
76
+ @quote.add_symbols('new_symbol')
77
+ end
78
+
79
+ end
80
+
81
+ describe "remove_symbols" do
82
+
83
+ it "should remove the symbol from existing symbols by calling remove symbols on interface" do
84
+ @interface.should_receive(:remove_symbols)
85
+ @quote.remove_symbols('remove_symbol')
86
+ end
87
+
88
+ end
89
+
90
+ describe "clear_symbols" do
91
+
92
+ it "should remove the symbol from existing symbols by clearing symbols from interface" do
93
+ @interface.should_receive(:clear_symbols)
94
+ @quote.clear_symbols
95
+ end
96
+
97
+ end
98
+
99
+ describe "current_symbols" do
100
+
101
+ it "should get all stock symbols from the interface" do
102
+ @interface.should_receive(:stock_symbols)
103
+ @quote.current_symbols
104
+ end
105
+
106
+ end
107
+
108
+ describe "add_parameters" do
109
+
110
+ it "should add the parameter to existing parameter by calling add parameters on interface" do
111
+ @interface.should_receive(:add_parameters)
112
+ @quote.add_parameters('param1')
113
+ end
114
+
115
+ end
116
+
117
+ describe "remove_parameters" do
118
+
119
+ it "should remove the parameter from existing parameters by calling remove parameters on interface" do
120
+ @interface.should_receive(:remove_parameters)
121
+ @quote.remove_parameters('remove_parameter')
122
+ end
123
+
124
+ end
125
+
126
+ describe "valid_parameters" do
127
+
128
+ it "should get the valid parameters by sending allowed parameters message to the yahoo interface" do
129
+ @quote.stub!(:sort_symbols)
130
+ @interface.should_receive(:allowed_parameters)
131
+ @quote.valid_parameters
132
+ end
133
+
134
+ it "sort the parameters after fetching them" do
135
+ @interface.stub!(:allowed_parameters)
136
+ @quote.should_receive(:sort_symbols)
137
+ @quote.valid_parameters
138
+ end
139
+
140
+ end
141
+
142
+ describe "current_parameters" do
143
+
144
+ it "should get all current parameters from the interface" do
145
+ @quote.stub!(:sort_symbols)
146
+ @interface.should_receive(:yahoo_url_parameters)
147
+ @quote.current_parameters
148
+ end
149
+
150
+ end
151
+
152
+ describe "use_all_parameters" do
153
+
154
+ before(:each) do
155
+ @quote.stub!(:sort_symbols)
156
+ @interface.stub!(:allowed_parameters)
157
+ end
158
+
159
+ it "should get all valid parameters" do
160
+ @quote.should_receive(:valid_parameters).and_return([])
161
+ @quote.use_all_parameters
162
+ end
163
+
164
+ it "should sort all received current parameters" do
165
+ @quote.stub!(:valid_parameters).and_return([])
166
+ @quote.should_receive(:sort_symbols)
167
+ @quote.use_all_parameters
168
+ end
169
+
170
+ it "should add each parameter" do
171
+ @quote.stub!(:valid_parameters).and_return([:param1, :param2])
172
+ @quote.should_receive(:add_parameters).exactly(2).times
173
+ @quote.use_all_parameters
174
+ end
175
+
176
+ end
177
+
178
+ describe "clear_parameters" do
179
+
180
+ it "should get all values for parameters from the interface" do
181
+ @quote.stub!(:current_parameters)
182
+ @interface.should_receive(:clear_parameters)
183
+ @quote.clear_parameters
184
+ end
185
+
186
+ it "should get current parameters after clearing them" do
187
+ @interface.stub!(:clear_parameters)
188
+ @quote.should_receive(:current_parameters)
189
+ @quote.clear_parameters
190
+ end
191
+
192
+ it "clear all parameters and show an empty array when there are no yahoo url parameters" do
193
+ @interface.stub!(:clear_parameters)
194
+ @interface.stub!(:yahoo_url_parameters).and_return([])
195
+ @quote.clear_parameters.should eql([])
196
+ end
197
+
198
+ end
199
+
200
+ describe "realtime" do
201
+
202
+ before(:each) do
203
+ @quote.stub!(:find)
204
+ @interface.stub!(:add_realtime_params)
205
+ end
206
+
207
+ it "should add the realtime parameters" do
208
+ @interface.should_receive(:add_realtime_params)
209
+ @quote.realtime
210
+ end
211
+
212
+ it "should return self" do
213
+ @quote.realtime.should eql(@quote)
214
+ end
215
+
216
+ end
217
+
218
+ describe "standard" do
219
+
220
+ before(:each) do
221
+ @quote.stub!(:find)
222
+ @interface.stub!(:add_standard_params)
223
+ end
224
+
225
+ it "should add the standard parameters" do
226
+ @interface.should_receive(:add_standard_params)
227
+ @quote.standard
228
+ end
229
+
230
+ it "should return self" do
231
+ @quote.standard.should eql(@quote)
232
+ end
233
+
234
+ end
235
+
236
+ describe "extended" do
237
+
238
+ before(:each) do
239
+ @quote.stub!(:find)
240
+ @interface.stub!(:add_extended_params)
241
+ end
242
+
243
+ it "should add the extended parameters" do
244
+ @interface.should_receive(:add_extended_params)
245
+ @quote.extended
246
+ end
247
+
248
+ it "should return self" do
249
+ @quote.extended.should eql(@quote)
250
+ end
251
+
252
+ end
253
+
254
+ end
255
+ end
256
+
257
+
258
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe YahooStock::Result::ArrayFormat do
4
+
5
+ describe "output" do
6
+ before(:each) do
7
+ @data = "asdf\"sdf,as,f\" asf s"
8
+ @array_format = YahooStock::Result::ArrayFormat.new(@data)
9
+ end
10
+
11
+ it "should remove all double quotes from the data string" do
12
+ @data.should_receive(:gsub).with(/\"/,'').and_return('a string')
13
+ @array_format.output
14
+ end
15
+
16
+ it "should create array element for each line by splitting it for the line break" do
17
+ string = "asdfsdf,as,f asf s"
18
+ @data.stub!(:gsub).and_return(string)
19
+ string.should_receive(:split).with(/\r\n|\n/).and_return([])
20
+ @array_format.output
21
+ end
22
+
23
+ it "should create a sub array for each line by splitting them for each comma" do
24
+ string = "asdfsdf,as,f asf s"
25
+ @data.stub!(:gsub).and_return(string)
26
+ string.stub!(:split).with(/\r\n|\n/).and_return([string])
27
+ string.should_receive(:split).with(',').and_return([])
28
+ @array_format.output
29
+ end
30
+
31
+ it "should return an array for each line and each line with an array of csv" do
32
+ string = "asdfsdf,as,f asf s\r\n23,sdf,2332,sdf"
33
+ @data.stub!(:gsub).and_return(string)
34
+ @array_format.output.should eql([['asdfsdf','as','f asf s'],['23','sdf' ,'2332', 'sdf']])
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,68 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe YahooStock::Result::HashFormat do
4
+ before(:each) do
5
+ @data = 'da,ta'
6
+ @array_format = YahooStock::Result::ArrayFormat.new(@data)
7
+ end
8
+ describe ".new" do
9
+ it "should initialise the Array Format class with data" do
10
+ YahooStock::Result::ArrayFormat.should_receive(:new).and_return(@array_format)
11
+ YahooStock::Result::HashFormat.new(@data){[:key1, :key2]}
12
+ end
13
+
14
+ it "should get the output of the array format" do
15
+ YahooStock::Result::ArrayFormat.stub!(:new).and_return(@array_format)
16
+ @array_format.should_receive(:output)
17
+ YahooStock::Result::HashFormat.new(@data){[:key1, :key2]}
18
+ end
19
+
20
+ it "should yield the block" do
21
+ hash_format = YahooStock::Result::HashFormat.new(@data){['key1', 'key2']}
22
+ hash_format.keys.should eql([:key1, :key2])
23
+ end
24
+ end
25
+
26
+
27
+ describe "output" do
28
+ it "should have the data as a hash with keys key1 and key2" do
29
+ hash_format = YahooStock::Result::HashFormat.new(@data){['key1', 'key2']}
30
+ hash_format.output.should eql([{:key1 => 'da',:key2 => 'ta'}])
31
+ end
32
+
33
+ it "should have the data as an array of hashes with the same keys key1 and key2" do
34
+ data = "da,ta\nda,ta\r\nda,ta"
35
+ hash_format = YahooStock::Result::HashFormat.new(data){['key1', 'key2']}
36
+ hash_format.output.should eql([{:key1 => 'da',:key2 => 'ta'},
37
+ {:key1 => 'da',:key2 => 'ta'},
38
+ {:key1 => 'da',:key2 => 'ta'}
39
+ ])
40
+ end
41
+ end
42
+
43
+ def output
44
+ data = []
45
+ @data.each do |datum|
46
+ row_values = {}
47
+ datum.each_with_index do |item, i|
48
+ row_values[keys[i]] = item
49
+ end
50
+ data << row_values
51
+ end
52
+ data
53
+ end
54
+
55
+ describe "keys" do
56
+ it "should replace white space characters to underscore in the keys" do
57
+ hash_format = YahooStock::Result::HashFormat.new(@data){['ke y 1', 'k e y 2']}
58
+ hash_format.keys.should eql([:ke_y_1, :k_e_y_2])
59
+ end
60
+
61
+ it "should replace upper case characters to lower case in the keys" do
62
+ hash_format = YahooStock::Result::HashFormat.new(@data){['Key1', 'keY2']}
63
+ hash_format.keys.should eql([:key1, :key2])
64
+ end
65
+ end
66
+
67
+
68
+ end