they-yahoo_stock 1.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +98 -0
- data/Manifest.txt +37 -0
- data/README.rdoc +151 -0
- data/Rakefile +12 -0
- data/features/history.feature +23 -0
- data/features/quotes.feature +23 -0
- data/features/script_symbol.feature +23 -0
- data/features/step_definitions/history_steps.rb +44 -0
- data/features/step_definitions/quotes_steps.rb +45 -0
- data/features/step_definitions/script_symbol_steps.rb +40 -0
- data/lib/yahoo_stock.rb +21 -0
- data/lib/yahoo_stock/base.rb +37 -0
- data/lib/yahoo_stock/history.rb +32 -0
- data/lib/yahoo_stock/interface.rb +71 -0
- data/lib/yahoo_stock/interface/history.rb +187 -0
- data/lib/yahoo_stock/interface/quote.rb +287 -0
- data/lib/yahoo_stock/interface/scrip_symbol.rb +74 -0
- data/lib/yahoo_stock/quote.rb +158 -0
- data/lib/yahoo_stock/result.rb +21 -0
- data/lib/yahoo_stock/result/array_format.rb +23 -0
- data/lib/yahoo_stock/result/hash_format.rb +37 -0
- data/lib/yahoo_stock/result/xml_format.rb +37 -0
- data/lib/yahoo_stock/scrip_symbol.rb +61 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/yahoo_stock/base_spec.rb +48 -0
- data/spec/yahoo_stock/history_spec.rb +75 -0
- data/spec/yahoo_stock/interface/history_spec.rb +317 -0
- data/spec/yahoo_stock/interface/quote_spec.rb +414 -0
- data/spec/yahoo_stock/interface/scrip_symbol_spec.rb +120 -0
- data/spec/yahoo_stock/interface_spec.rb +112 -0
- data/spec/yahoo_stock/quote_spec.rb +258 -0
- data/spec/yahoo_stock/result/array_format_spec.rb +14 -0
- data/spec/yahoo_stock/result/hash_format_spec.rb +65 -0
- data/spec/yahoo_stock/result/xml_format_spec.rb +54 -0
- data/spec/yahoo_stock/result_spec.rb +33 -0
- data/spec/yahoo_stock/scrip_symbol_spec.rb +46 -0
- metadata +114 -0
@@ -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
|
@@ -0,0 +1,112 @@
|
|
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
|
+
|
28
|
+
it "should encode carrot (^) symbols for market indices" do
|
29
|
+
@interface.uri_parameters = {:s => '^boom', :f => 'toom', :k => 'zoom'}
|
30
|
+
@interface.uri.should =~ /http:\/\/download.finance.yaaaaahoo.com\/d\/quotes.csv?/
|
31
|
+
@interface.uri.should =~ /s=%5Eboom/
|
32
|
+
@interface.uri.should =~ /f=toom/
|
33
|
+
@interface.uri.should =~ /k=zoom/
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "get" do
|
38
|
+
before(:each) do
|
39
|
+
@response = stub('HTTP Response')
|
40
|
+
@response.stub!(:code).and_return('200')
|
41
|
+
@response.stub!(:body)
|
42
|
+
URI.stub!(:parse)
|
43
|
+
Net::HTTP.stub!(:get_response).and_return(@response)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should get response for the uri" do
|
47
|
+
Net::HTTP.should_receive(:get_response).and_return(@response)
|
48
|
+
@interface.get
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should parse the uri" do
|
52
|
+
URI.should_receive(:parse)
|
53
|
+
@interface.get
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should check the response code" do
|
57
|
+
@response.should_receive(:code)
|
58
|
+
@interface.get
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should get the body of the response if returned code is 200, ie success" do
|
62
|
+
@response.stub!(:code).and_return('200')
|
63
|
+
@response.should_receive(:body)
|
64
|
+
@interface.get
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "when response code is not 200" do
|
68
|
+
before(:each) do
|
69
|
+
@response.stub!(:code).and_return('301')
|
70
|
+
@response.stub!(:body).and_return('something')
|
71
|
+
@response.stub!(:message).and_return('eerrred')
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should return the response error when returned code is not 200" do
|
75
|
+
@interface.should_receive(:response_error)
|
76
|
+
@interface.get
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should not get the body of the response when returned code is not 200" do
|
80
|
+
@interface.should_receive(:response_error)
|
81
|
+
@response.should_not_receive(:body)
|
82
|
+
@interface.get
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should raise error when returned code is not 200" do
|
86
|
+
@interface.base_url = YahooStock::Interface::BASE_URLS[:quote]
|
87
|
+
lambda { @interface.get }.should raise_error
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
describe "values" do
|
94
|
+
it "should call get to receive the values" do
|
95
|
+
@interface.should_receive(:get)
|
96
|
+
@interface.values
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should not call get when values are already set" do
|
100
|
+
@interface.stub!(:values).and_return('some string')
|
101
|
+
@interface.should_not_receive(:get)
|
102
|
+
@interface.values
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "update" do
|
107
|
+
it "should set the values to nil" do
|
108
|
+
@interface.update.should be_nil
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
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
|