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.
@@ -3,265 +3,102 @@ require File.dirname(__FILE__) + '/../spec_helper'
3
3
  describe YahooStock::Interface do
4
4
 
5
5
  before(:each) do
6
- @interface = YahooStock::Interface.new(:stock_symbols => ['MSFT'], :read_parameters => [:last_trade_price_only])
6
+ @interface = YahooStock::Interface.new
7
+ @interface.base_url = 'http://download.finance.yaaaaahoo.com/d/quotes.csv'
7
8
  end
8
-
9
- describe ".new" do
10
-
11
- it "should raise InterfaceError when stock params hash is nil" do
12
- lambda { YahooStock::Interface.new(nil)
13
- }.should raise_error(YahooStock::Interface::InterfaceError, 'You must pass a hash of stock symbols and the data you would like to see')
14
- end
15
-
16
- it "should raise ArgumentError when no parameter is passed" do
17
- lambda { YahooStock::Interface.new }.should raise_error(ArgumentError)
18
- end
19
-
20
- it "should raise InterfaceError when stock symbols hash is nil" do
21
- lambda { YahooStock::Interface.new(:stock_symbols => nil) }.should raise_error(YahooStock::Interface::InterfaceError, 'No stocks passed')
22
- end
23
-
24
- it "should raise InterfaceError when stock symbols parameter hash is an array of zero elements" do
25
- lambda { YahooStock::Interface.new(:stock_symbols => []) }.should raise_error(YahooStock::Interface::InterfaceError, 'No stocks passed')
26
- end
27
-
28
- it "should raise InterfaceError when read parameters hash is nil" do
29
- lambda { YahooStock::Interface.new(:stock_symbols => 'sym', :read_parameters => nil) }.should raise_error(YahooStock::Interface::InterfaceError,'Dont know what data to get')
30
- end
31
-
32
- it "should raise InterfaceError when read parameters hash is a zero element array" do
33
- lambda { YahooStock::Interface.new(:stock_symbols => 'sym', :read_parameters => []) }.should raise_error(YahooStock::Interface::InterfaceError,'Dont know what data to get')
34
- end
35
-
36
- it "should assign appropriate values to the stock symbols accessor" do
37
- interface = YahooStock::Interface.new(:stock_symbols => ['sym'], :read_parameters => ['param1'])
38
- interface.stock_symbols.should eql(['sym'])
39
- end
40
-
41
- it "should assign appropriate values to the yahoo url parameters accessor" do
42
- interface = YahooStock::Interface.new(:stock_symbols => ['sym'], :read_parameters => ['param1'])
43
- interface.yahoo_url_parameters.should eql(['param1'])
44
- end
45
-
46
- end
47
-
48
- describe "full_url" do
49
-
50
- it "should raise InterfaceError if the passed parameter is not present in the parameter list" do
51
- interface = YahooStock::Interface.new(:stock_symbols => ['sym'], :read_parameters => [:param1, :param2])
52
- lambda { interface.full_url }.should raise_error(YahooStock::Interface::InterfaceError, "The parameters 'param1, param2' are not valid. Please check using YahooStock::Interface#allowed_parameters or YahooStock::Quote#valid_parameters")
53
- end
54
-
55
- it "should not raise any error if parameters passed are correct" do
56
- lambda { @interface.full_url }.should_not raise_error
57
- end
58
-
59
- it "should raise error if stock symbols accessor is cleared and empty" do
60
- @interface.stock_symbols.clear
61
- lambda { @interface.full_url }.should raise_error(YahooStock::Interface::InterfaceError, "You must add atleast one stock symbol to get stock data")
62
- end
63
-
64
- it "should raise error if url parameters are cleared and empty" do
65
- @interface.yahoo_url_parameters.clear
66
- lambda { @interface.full_url }.should raise_error(YahooStock::Interface::InterfaceError, "You must add atleast one parameter to get stock data")
67
- end
68
-
69
- it "should create the full url with one symbol and one parameter" do
70
- @interface.full_url.should eql("http://download.finance.yahoo.com/d/quotes.csv?s=MSFT&f=l1")
71
- end
72
-
73
- it "should create the full url with 3 symbols and 3 parameters" do
74
- @interface.add_symbols 'AAP', 'GOOG'
75
- @interface.add_parameters :change_from_200_day_moving_average, :percent_change_from_200_day_moving_average
76
- @interface.full_url.should eql("http://download.finance.yahoo.com/d/quotes.csv?s=MSFT+AAP+GOOG&f=l1m5m6")
77
- end
78
-
79
- end
80
-
81
- describe "get_values" do
82
-
83
- it "should call the class method get of HTTP class" do
84
- Net::HTTP.should_receive(:get).and_return('some string')
85
- @interface.get_values
86
- end
87
-
88
- it "should parse the URI" do
89
- Net::HTTP.stub!(:get).and_return('some string')
90
- URI.should_receive(:parse)
91
- @interface.get_values
92
- end
93
-
94
- it "should generate the full url by combining the stock symbols and other parameters" do
95
- Net::HTTP.stub!(:get).and_return('some string')
96
- @interface.should_receive(:full_url).and_return('http://localhost')
97
- @interface.get_values
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')
98
12
  end
99
13
 
100
- end
101
-
102
- describe "remove_parameters" do
103
-
104
- it "should remove the parameters if they are present" do
105
- params = [:test1, :test2, :test3]
106
- @interface.stub!(:yahoo_url_parameters).and_return(params)
107
- @interface.remove_parameters(:test1, :test2)
108
- @interface.yahoo_url_parameters.should eql([:test3])
109
- end
110
-
111
- it "should raise an error when removing a parameter that is not present" do
112
- lambda { @interface.remove_parameters(:test1) }.should raise_error(YahooStock::Interface::InterfaceError, "Parameter test1 is not present in current list")
113
- end
114
- end
115
-
116
- describe "add_parameters" do
117
-
118
- it "should raise an error when the parameter is not a valid parameter" do
119
- lambda { @interface.add_parameters('test1') }.should raise_error(YahooStock::Interface::InterfaceError, "Interface parameter test1 is not a valid parameter.")
120
- end
121
-
122
- it "should add the parameter if its a valid parameter" do
123
- params = [:test1, :test2, :test3]
124
- @interface.stub!(:allowed_parameters).and_return(params)
125
- @interface.add_parameters(:test1, :test2)
126
- @interface.yahoo_url_parameters.should include(:test1, :test2)
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.')
127
18
  end
128
19
 
129
- it "should not add the parameter more than once in the parameter list and silently ignore it" do
130
- params = [:test1, :test2, :test3]
131
- @interface.stub!(:allowed_parameters).and_return(params)
132
- @interface.add_parameters(:test1, :test2)
133
- @interface.yahoo_url_parameters.should include(:test1, :test2)
134
- @interface.yahoo_url_parameters.size.should eql(3)
135
- @interface.add_parameters(:test1, :test2)
136
- @interface.yahoo_url_parameters.should include(:test1, :test2)
137
- @interface.yahoo_url_parameters.size.should eql(3)
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/
138
26
  end
139
27
  end
140
28
 
141
- describe "remove_symbols" do
142
-
143
- it "should remove the symbols if they are present" do
144
- symbols = ['test1', 'test2', 'test3']
145
- @interface.stub!(:stock_symbols).and_return(symbols)
146
- @interface.remove_symbols('test1', 'test2')
147
- @interface.stock_symbols.should eql(['test3'])
148
- end
149
-
150
- it "should raise an error when removing a symbol that is not present" do
151
- lambda { @interface.remove_symbols('test1') }.should raise_error(YahooStock::Interface::InterfaceError, "Cannot remove stock symbol test1 as it is currently not present.")
152
- end
153
- end
154
-
155
- describe "add_symbols" do
156
-
157
- it "should add the symbol" do
158
- symbols = ['test1', 'test2', 'test3']
159
- @interface.stub!(:stock_symbols).and_return(symbols)
160
- @interface.add_symbols('test1', 'test2')
161
- @interface.stock_symbols.should include('test1', 'test2')
162
- end
163
-
164
- it "should not add the symbol more than once in the symbol list and silently ignore it" do
165
- symbols = ['test']
166
- @interface.stub!(:stock_symbols).and_return(symbols)
167
- @interface.add_symbols('test1', 'test2')
168
- @interface.stock_symbols.should include('test1', 'test2')
169
- @interface.stock_symbols.size.should eql(3)
170
- @interface.add_symbols('test1', 'test2')
171
- @interface.stock_symbols.should include('test1', 'test2')
172
- @interface.stock_symbols.size.should eql(3)
173
- end
174
- end
175
-
176
- describe "results" do
177
-
29
+ describe "get" do
178
30
  before(:each) do
179
- symbols = ['sym1', 'sym2', 'sym3']
180
- @interface.stub!(:stock_symbols).and_return(symbols)
181
- params = [:test1, :test2, :test3]
182
- @interface.stub!(:yahoo_url_parameters).and_return(params)
183
- end
184
-
185
- it "should get all values" do
186
- @interface.should_receive(:get_values).and_return([])
187
- @interface.results
188
- end
189
-
190
- it "should have a hash of symbols and each parameter value" do
191
- @values = ["23,787,09","43,45,65", "6,56,8"]
192
- @interface.stub!(:get_values).and_return(@values)
193
- @interface.results.should include({'sym1' => {:test1 => "23", :test2 => "787", :test3 => "09"}})
194
- @interface.results.should include({'sym2' => {:test1 => "43", :test2 => "45", :test3 => "65"}})
195
- @interface.results.should include({'sym3' => {:test1 => "6", :test2 => "56", :test3 => "8"}})
196
- end
197
-
198
- end
199
-
200
- describe "allowed_parameters" do
201
-
202
- it "should find all parameters" do
203
- @interface.should_receive(:parameters).and_return({})
204
- @interface.allowed_parameters
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)
205
36
  end
206
37
 
207
- it "should return the keys of all parameters" do
208
- parameter_hash = {}
209
- @interface.stub!(:parameters).and_return(parameter_hash)
210
- parameter_hash.should_receive(:keys)
211
- @interface.allowed_parameters
38
+ it "should get response for the uri" do
39
+ Net::HTTP.should_receive(:get_response).and_return(@response)
40
+ @interface.get
212
41
  end
213
42
 
214
- end
215
-
216
- describe "add_standard_params" do
217
-
218
- it "should get the keys for standard parameters" do
219
- YahooStock::Interface::STD_PARAMETERS.should_receive(:keys).and_return([])
220
- @interface.add_standard_params
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
221
81
  end
222
82
 
223
- it "should add each parameter" do
224
- keys = [:a_key, :b_key]
225
- YahooStock::Interface::STD_PARAMETERS.stub!(:keys).and_return(keys)
226
- @interface.should_receive(:add_parameters).with(:a_key)
227
- @interface.should_receive(:add_parameters).with(:b_key)
228
- @interface.add_standard_params
229
- end
230
-
231
83
  end
232
84
 
233
- describe "add_extended_params" do
234
-
235
- it "should get the keys for extended parameters" do
236
- YahooStock::Interface::EXTENDED_PARAMETERS.should_receive(:keys).and_return([])
237
- @interface.add_extended_params
85
+ describe "values" do
86
+ it "should call get to receive the values" do
87
+ @interface.should_receive(:get)
88
+ @interface.values
238
89
  end
239
90
 
240
- it "should add each parameter" do
241
- keys = [:a_key, :b_key]
242
- YahooStock::Interface::EXTENDED_PARAMETERS.stub!(:keys).and_return(keys)
243
- @interface.should_receive(:add_parameters).with(:a_key)
244
- @interface.should_receive(:add_parameters).with(:b_key)
245
- @interface.add_extended_params
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
246
95
  end
247
-
248
96
  end
249
97
 
250
- describe "add_realtime_params" do
251
-
252
- it "should get the keys for realtime parameters" do
253
- YahooStock::Interface::REALTIME_PARAMETERS.should_receive(:keys).and_return([])
254
- @interface.add_realtime_params
255
- end
256
-
257
- it "should add each parameter" do
258
- keys = [:a_key, :b_key]
259
- YahooStock::Interface::REALTIME_PARAMETERS.stub!(:keys).and_return(keys)
260
- @interface.should_receive(:add_parameters).with(:a_key)
261
- @interface.should_receive(:add_parameters).with(:b_key)
262
- @interface.add_realtime_params
98
+ describe "update" do
99
+ it "should set the values to nil" do
100
+ @interface.update.should be_nil
263
101
  end
264
-
265
102
  end
266
103
 
267
104
  end
@@ -55,7 +55,7 @@ module YahooStock
55
55
  end
56
56
 
57
57
  it "should create a new instance of the Stock Interface class" do
58
- YahooStock::Interface.should_receive(:new)
58
+ YahooStock::Interface::Quote.should_receive(:new)
59
59
  YahooStock::Quote.new(:stock_symbols => 'test')
60
60
  end
61
61
 
@@ -64,18 +64,10 @@ module YahooStock
64
64
  describe "subject" do
65
65
 
66
66
  before(:each) do
67
- @interface = mock(YahooStock::Interface)
68
- YahooStock::Interface.stub!(:new).and_return(@interface)
67
+ @interface = mock(YahooStock::Interface::Quote)
68
+ YahooStock::Interface::Quote.stub!(:new).and_return(@interface)
69
69
  @quote = YahooStock::Quote.new(:stock_symbols => 'MSFT')
70
70
  end
71
-
72
- describe "get" do
73
-
74
- it "should get the results from the interface" do
75
- @interface.should_receive(:results)
76
- @quote.get
77
- end
78
- end
79
71
 
80
72
  describe "add_symbols" do
81
73
 
@@ -84,11 +76,6 @@ module YahooStock
84
76
  @quote.add_symbols('new_symbol')
85
77
  end
86
78
 
87
- it "should add the symbol to existing symbols by calling add symbols on interface for each sybmol" do
88
- @interface.should_receive(:add_symbols).exactly(2).times
89
- @quote.add_symbols('new_symbol', 'another_symbol')
90
- end
91
-
92
79
  end
93
80
 
94
81
  describe "remove_symbols" do
@@ -98,24 +85,12 @@ module YahooStock
98
85
  @quote.remove_symbols('remove_symbol')
99
86
  end
100
87
 
101
- it "should remove the symbol from existing symbols by calling remove symbols on interface for each symbol" do
102
- @interface.should_receive(:remove_symbols).exactly(2).times
103
- @quote.remove_symbols('new_symbol', 'another_symbol')
104
- end
105
-
106
88
  end
107
89
 
108
90
  describe "clear_symbols" do
109
91
 
110
- it "should get all stock symbols from the interface" do
111
- @interface.should_receive(:stock_symbols).and_return([])
112
- @quote.clear_symbols
113
- end
114
-
115
- it "should remove the symbol from existing symbols by calling remove symbols on interface for each symbol" do
116
- symbols = ['symbol1', 'symbol2']
117
- @interface.stub!(:stock_symbols).and_return(symbols)
118
- symbols.should_receive(:clear)
92
+ it "should remove the symbol from existing symbols by clearing symbols from interface" do
93
+ @interface.should_receive(:clear_symbols)
119
94
  @quote.clear_symbols
120
95
  end
121
96
 
@@ -137,11 +112,6 @@ module YahooStock
137
112
  @quote.add_parameters('param1')
138
113
  end
139
114
 
140
- it "should add the parameter to existing parameters by calling add parameters on interface for each parameter" do
141
- @interface.should_receive(:add_parameters).exactly(2).times
142
- @quote.add_parameters('param1', 'param2')
143
- end
144
-
145
115
  end
146
116
 
147
117
  describe "remove_parameters" do
@@ -151,11 +121,6 @@ module YahooStock
151
121
  @quote.remove_parameters('remove_parameter')
152
122
  end
153
123
 
154
- it "should remove the parameter from existing parameters by calling remove parameters on interface for each parameter" do
155
- @interface.should_receive(:remove_parameters).exactly(2).times
156
- @quote.remove_parameters('param1', 'param2')
157
- end
158
-
159
124
  end
160
125
 
161
126
  describe "valid_parameters" do
@@ -182,12 +147,6 @@ module YahooStock
182
147
  @quote.current_parameters
183
148
  end
184
149
 
185
- it "should sort all received current parameters" do
186
- @interface.stub!(:yahoo_url_parameters)
187
- @quote.should_receive(:sort_symbols)
188
- @quote.current_parameters
189
- end
190
-
191
150
  end
192
151
 
193
152
  describe "use_all_parameters" do
@@ -219,41 +178,39 @@ module YahooStock
219
178
  describe "clear_parameters" do
220
179
 
221
180
  it "should get all values for parameters from the interface" do
222
- @interface.should_receive(:yahoo_url_parameters).and_return([])
181
+ @quote.stub!(:current_parameters)
182
+ @interface.should_receive(:clear_parameters)
223
183
  @quote.clear_parameters
224
184
  end
225
185
 
226
- it "should remove the parameter from existing parameters by calling remove parameters on interface for each parameter" do
227
- params = ['param1', 'param2']
228
- @interface.stub!(:yahoo_url_parameters).and_return(params)
229
- params.should_receive(:clear)
186
+ it "should get current parameters after clearing them" do
187
+ @interface.stub!(:clear_parameters)
188
+ @quote.should_receive(:current_parameters)
230
189
  @quote.clear_parameters
231
190
  end
232
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
+
233
198
  end
234
199
 
235
200
  describe "realtime" do
236
201
 
237
202
  before(:each) do
238
- @quote.stub!(:get)
203
+ @quote.stub!(:find)
239
204
  @interface.stub!(:add_realtime_params)
240
205
  end
241
206
 
242
- it "should clear all existing parameters for the existing instance" do
243
- @quote.should_receive(:clear_parameters)
244
- @quote.realtime
245
- end
246
-
247
207
  it "should add the realtime parameters" do
248
- @quote.stub!(:clear_parameters)
249
208
  @interface.should_receive(:add_realtime_params)
250
209
  @quote.realtime
251
210
  end
252
211
 
253
- it "should get the results" do
254
- @quote.stub!(:clear_parameters)
255
- @quote.should_receive(:get)
256
- @quote.realtime
212
+ it "should return self" do
213
+ @quote.realtime.should eql(@quote)
257
214
  end
258
215
 
259
216
  end
@@ -261,25 +218,17 @@ module YahooStock
261
218
  describe "standard" do
262
219
 
263
220
  before(:each) do
264
- @quote.stub!(:get)
221
+ @quote.stub!(:find)
265
222
  @interface.stub!(:add_standard_params)
266
223
  end
267
224
 
268
- it "should clear all existing parameters for the existing instance" do
269
- @quote.should_receive(:clear_parameters)
270
- @quote.standard
271
- end
272
-
273
- it "should add the realtime parameters" do
274
- @quote.stub!(:clear_parameters)
225
+ it "should add the standard parameters" do
275
226
  @interface.should_receive(:add_standard_params)
276
227
  @quote.standard
277
228
  end
278
229
 
279
- it "should get the results" do
280
- @quote.stub!(:clear_parameters)
281
- @quote.should_receive(:get)
282
- @quote.standard
230
+ it "should return self" do
231
+ @quote.standard.should eql(@quote)
283
232
  end
284
233
 
285
234
  end
@@ -287,25 +236,17 @@ module YahooStock
287
236
  describe "extended" do
288
237
 
289
238
  before(:each) do
290
- @quote.stub!(:get)
239
+ @quote.stub!(:find)
291
240
  @interface.stub!(:add_extended_params)
292
241
  end
293
242
 
294
- it "should clear all existing parameters for the existing instance" do
295
- @quote.should_receive(:clear_parameters)
296
- @quote.extended
297
- end
298
-
299
- it "should add the realtime parameters" do
300
- @quote.stub!(:clear_parameters)
243
+ it "should add the extended parameters" do
301
244
  @interface.should_receive(:add_extended_params)
302
245
  @quote.extended
303
246
  end
304
247
 
305
- it "should get the results" do
306
- @quote.stub!(:clear_parameters)
307
- @quote.should_receive(:get)
308
- @quote.extended
248
+ it "should return self" do
249
+ @quote.extended.should eql(@quote)
309
250
  end
310
251
 
311
252
  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