nas-yahoo_stock 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/README.rdoc +9 -5
- data/lib/yahoo_stock/interface.rb +4 -0
- data/lib/yahoo_stock/quote.rb +1 -1
- data/spec/yahoo_stock/interface_spec.rb +216 -0
- data/spec/yahoo_stock/quote_spec.rb +2 -2
- metadata +4 -2
data/History.txt
CHANGED
data/README.rdoc
CHANGED
@@ -4,13 +4,17 @@
|
|
4
4
|
|
5
5
|
== DESCRIPTION:
|
6
6
|
|
7
|
-
Provides an interface to yahoo finance to get stock related data. For instance, latest trade related data, volume, 50 day moving average, market cap, etc.
|
7
|
+
Provides an interface to yahoo finance to get stock related data. For instance, latest trade related data, volume, 50 day moving average, market cap, etc, virtually that yahoo finance provides.
|
8
8
|
|
9
9
|
You must know the stock symbol. For instance, YHOO for yahoo, GOOG for google, etc.
|
10
10
|
The kind of parameters can be passed can be found after initializing the
|
11
|
-
YahooStock::Quote object and passing valid_parameters message.
|
11
|
+
YahooStock::Quote object and passing valid_parameters message, example is given below in the USAGE section.
|
12
12
|
|
13
|
-
==
|
13
|
+
== USAGE:
|
14
|
+
|
15
|
+
require 'rubygems'
|
16
|
+
|
17
|
+
require 'yahoo_stock'
|
14
18
|
|
15
19
|
* Initialize quote object
|
16
20
|
|
@@ -38,14 +42,14 @@ quote.remove_symbols('MSFT', 'AAPL')
|
|
38
42
|
|
39
43
|
* To get data for all stocks
|
40
44
|
|
41
|
-
quote.
|
45
|
+
quote.get
|
42
46
|
|
43
47
|
|
44
48
|
== INSTALL:
|
45
49
|
|
46
50
|
gem sources -a http://gems.github.com
|
47
51
|
|
48
|
-
sudo gem install yahoo_stock
|
52
|
+
sudo gem install nas-yahoo_stock
|
49
53
|
|
50
54
|
== TESTS:
|
51
55
|
|
@@ -120,6 +120,10 @@ module YahooStock
|
|
120
120
|
|
121
121
|
def full_url
|
122
122
|
all_stock_symbols = stock_symbols.join('+')
|
123
|
+
params = yahoo_url_parameters-allowed_parameters
|
124
|
+
unless params.length.zero?
|
125
|
+
raise InterfaceError, "The parameters '#{params.join(', ')}' are not valid. Please check using YahooStock::Interface#allowed_parameters or YahooStock::Quote#valid_parameters"
|
126
|
+
end
|
123
127
|
parameter_values = yahoo_url_parameters.collect {|v| parameters[v]}.join('')
|
124
128
|
if !all_stock_symbols.any?
|
125
129
|
raise InterfaceError, "You must add atleast one stock symbol to get stock data"
|
data/lib/yahoo_stock/quote.rb
CHANGED
@@ -0,0 +1,216 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe YahooStock::Interface do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@interface = YahooStock::Interface.new(:stock_symbols => ['MSFT'], :read_parameters => [:last_trade_price_only])
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "initialize" 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
|
98
|
+
end
|
99
|
+
|
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)
|
127
|
+
end
|
128
|
+
|
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)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
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
|
+
|
178
|
+
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
|
205
|
+
end
|
206
|
+
|
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
|
212
|
+
end
|
213
|
+
|
214
|
+
end
|
215
|
+
|
216
|
+
end
|
@@ -65,11 +65,11 @@ module YahooStock
|
|
65
65
|
@quote = YahooStock::Quote.new(:stock_symbols => 'MSFT')
|
66
66
|
end
|
67
67
|
|
68
|
-
describe "
|
68
|
+
describe "get" do
|
69
69
|
|
70
70
|
it "should get the results from the interface" do
|
71
71
|
@interface.should_receive(:results)
|
72
|
-
@quote.
|
72
|
+
@quote.get
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nas-yahoo_stock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nasir Jamal
|
@@ -50,8 +50,10 @@ files:
|
|
50
50
|
- lib/yahoo_stock/interface.rb
|
51
51
|
- spec/spec_helper.rb
|
52
52
|
- spec/yahoo_stock/quote_spec.rb
|
53
|
+
- spec/yahoo_stock/interface_spec.rb
|
53
54
|
has_rdoc: true
|
54
55
|
homepage: http://github.com/nas/yahoo_stock
|
56
|
+
licenses:
|
55
57
|
post_install_message:
|
56
58
|
rdoc_options:
|
57
59
|
- --inline-source
|
@@ -73,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
75
|
requirements: []
|
74
76
|
|
75
77
|
rubyforge_project:
|
76
|
-
rubygems_version: 1.
|
78
|
+
rubygems_version: 1.3.5
|
77
79
|
signing_key:
|
78
80
|
specification_version: 2
|
79
81
|
summary: Yahoo Stock is a Ruby library for extracting information about stocks from yahoo finance.
|