nas-yahoo_stock 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -26,3 +26,9 @@
26
26
 
27
27
  * 1 changes related with ruby 1.9:
28
28
  * modifies ScripSymbol class to be compatible with ruby 1.9
29
+
30
+ === 0.1.6 2009-09-07
31
+
32
+ * 1 addition:
33
+ * adds convenience methods Quote#realtime, standard and extended to
34
+ fetch relevant stock values
@@ -15,89 +15,97 @@ module YahooStock
15
15
  class Interface
16
16
 
17
17
  class InterfaceError < RuntimeError ; end
18
-
19
- PARAMETERS = {
18
+
19
+ STD_PARAMETERS = {
20
+ :symbol => 's',
21
+ :name => 'n',
22
+ :last_trade_price_only => 'l1',
23
+ :last_trade_date => 'd1',
24
+ :last_trade_time => 't1',
25
+ :change_with_percent_change => 'c',
26
+ :change => 'c1',
27
+ :previous_close => 'p',
28
+ :change_in_percent => 'p2',
29
+ :open => 'o',
30
+ :day_low => 'g',
31
+ :day_high => 'h',
32
+ :volume => 'v',
33
+ :last_trade_with_time => 'l',
34
+ :day_range => 'm',
35
+ :ticker_trend => 't7',
20
36
  :ask => 'a',
21
37
  :average_daily_volume => 'a2',
22
- :ask_size => 'a5',
23
38
  :bid => 'b',
24
- :ask_real_time => 'b2',
25
- :bid_real_time => 'b3',
26
- :book_value => 'b4',
27
- :bid_size => 'b4',
28
- :change_with_percent_change => 'c',
29
- :change => 'c1',
30
- :commission => 'c3',
31
- :change_real_time => 'c6',
32
- :after_hours_change_real_time => 'c8',
33
- :dividend_per_share => 'd',
34
- :last_trade_date => 'd1',
35
- :trade_date => 'd2',
39
+ :bid_size => 'b4'
40
+ }
41
+
42
+ EXTENDED_PARAMETERS = {
43
+ :symbol => 's',
44
+ :name => 'n',
45
+ :fifty_two_week_range => 'w',
46
+ :change_from_52_week_low => 'j5',
47
+ :percent_change_from_52_week_low => 'j6',
48
+ :change_from_52_week_high => 'k4',
49
+ :percent_change_from_52_week_high => 'k5',
36
50
  :earnings_per_share => 'e',
37
- :error_indication => 'e1',
51
+ :short_ratio => 's7',
52
+ :p_e_ratio => 'r',
53
+ :dividend_pay_date => 'r1',
54
+ :ex_dividend_date => 'q',
55
+ :dividend_per_share => 'd',
56
+ :dividend_yield => 'y',
57
+ :one_yr_target_price => 't8',
58
+ :market_capitalization => 'j1',
38
59
  :eps_estimate_current_year => 'e7',
39
60
  :eps_estimate_next_year => 'e8',
40
61
  :eps_estimate_next_quarter => 'e9',
41
- :day_low => 'g',
42
- :day_high => 'h',
43
- :fifty_two_week_low => 'j',
44
- :fifty_two_week_high => 'k',
62
+ :peg_ratio => 'r5',
63
+ :price_eps_estimate_current_year => 'r6',
64
+ :price_eps_estimate_next_year => 'r7',
65
+ :book_value => 'b4',
66
+ :ebitda => 'j4',
67
+ :fifty_day_moving_average => 'm3',
68
+ :two_hundred_day_moving_average => 'm4',
69
+ :change_from_200_day_moving_average => 'm5',
70
+ :percent_change_from_200_day_moving_average => 'm6',
71
+ :change_from_50_day_moving_average => 'm7',
72
+ :percent_change_from_50_day_moving_average => 'm8',
73
+ :shares_owned => 's1',
74
+ :price_paid => 'p1',
75
+ :commission => 'c3',
76
+ :holdings_value => 'v1',
77
+ :day_value_change => 'w1',
78
+ :trade_date => 'd2',
45
79
  :holdings_gain_percent => 'g1',
46
80
  :annualized_gain => 'g3',
47
81
  :holdings_gain => 'g4',
82
+ :stock_exchange => 'x',
83
+ :high_limit => 'l2',
84
+ :low_limit => 'l3',
85
+ :notes => 'n4',
86
+ :fifty_two_week_low => 'j',
87
+ :fifty_two_week_high => 'k',
88
+ :more_info => 'i',
89
+ }
90
+
91
+ REALTIME_PARAMETERS = {
92
+ :symbol => 's',
93
+ :name => 'n',
94
+ :ask_real_time => 'b2',
95
+ :bid_real_time => 'b3',
96
+ :change => 'c1',
97
+ :change_real_time => 'c6',
98
+ :after_hours_change_real_time => 'c8',
48
99
  :holdings_gain_percent_real_time => 'g5',
49
100
  :holdings_gain_real_time => 'g6',
50
- :more_info => 'i',
51
101
  :order_book_real_time => 'i5',
52
- :market_capitalization => 'j1',
53
102
  :market_cap_real_time => 'j3',
54
- :ebitda => 'j4',
55
- :change_from_52_week_low => 'j5',
56
- :percent_change_from_52_week_low => 'j6',
57
103
  :last_trade_real_time_with_time => 'k1',
58
104
  :change_percent_real_time => 'k2',
59
- :change_from_52_week_high => 'k4',
60
- :percent_change_from_52_week_high => 'k5',
61
- :last_trade_with_time => 'l',
62
- :last_trade_price_only => 'l1',
63
- :high_limit => 'l2',
64
- :low_limit => 'l3',
65
- :day_range => 'm',
66
105
  :day_range_real_time => 'm2',
67
- :fifty_day_moving_average => 'm3',
68
- :two_hundred_day_moving_average => 'm4',
69
- :change_from_200_day_moving_average => 'm5',
70
- :percent_change_from_200_day_moving_average => 'm6',
71
- :change_from_50_day_moving_average => 'm7',
72
- :percent_change_from_50_day_moving_average => 'm8',
73
- :name => 'n',
74
- :notes => 'n4',
75
- :open => 'o',
76
- :previous_close => 'p',
77
- :price_paid => 'p1',
78
- :change_in_percent => 'p2',
79
- :ex_dividend_date => 'q',
80
- :p_e_ratio => 'r',
81
- :dividend_pay_date => 'r1',
82
106
  :p_e_ratio_real_time => 'r2',
83
- :peg_ratio => 'r5',
84
- :price_eps_estimate_current_year => 'r6',
85
- :price_eps_estimate_next_year => 'r7',
86
- :symbol => 's',
87
- :shares_owned => 's1',
88
- :short_ratio => 's7',
89
- :last_trade_time => 't1',
90
- :trade_links => 't6',
91
- :ticker_trend => 't7',
92
- :one_yr_target_price => 't8',
93
- :volume => 'v',
94
- :holdings_value => 'v1',
95
107
  :holdings_value_real_time => 'v7',
96
- :fifty_two_week_range => 'w',
97
- :day_value_change => 'w1',
98
108
  :day_value_change_real_time => 'w4',
99
- :stock_exchange => 'x',
100
- :dividend_yield => 'y',
101
109
  }
102
110
 
103
111
  attr_accessor :stock_symbols, :yahoo_url_parameters
@@ -210,10 +218,25 @@ module YahooStock
210
218
  parameters.keys
211
219
  end
212
220
 
221
+ # Add standard parameters
222
+ def add_standard_params
223
+ STD_PARAMETERS.keys.each { |parameter| add_parameters(parameter) }
224
+ end
225
+
226
+ # Add extended parameters
227
+ def add_extended_params
228
+ EXTENDED_PARAMETERS.keys.each { |parameter| add_parameters(parameter) }
229
+ end
230
+
231
+ # Add realtime parameters
232
+ def add_realtime_params
233
+ REALTIME_PARAMETERS.keys.each { |parameter| add_parameters(parameter) }
234
+ end
235
+
213
236
  private
214
237
 
215
238
  def parameters
216
- PARAMETERS
239
+ STD_PARAMETERS.merge!(EXTENDED_PARAMETERS).merge!(REALTIME_PARAMETERS)
217
240
  end
218
241
 
219
242
  end
@@ -18,6 +18,18 @@ module YahooStock
18
18
  #
19
19
  # quote.get
20
20
  #
21
+ # * To get data with real time values
22
+ #
23
+ # quote.realtime
24
+ #
25
+ # * To get data with standard values
26
+ #
27
+ # quote.standard
28
+ #
29
+ # * To get data with extra parameter values
30
+ #
31
+ # quote.extended
32
+ #
21
33
  # * To view the valid parameters that can be passed
22
34
  #
23
35
  # quote.valid_parameters
@@ -51,7 +63,7 @@ module YahooStock
51
63
  #
52
64
  # e.g. :read_parameters => [:last_trade_price_only, :last_trade_date]
53
65
  def initialize(options)
54
- if options.nil? || !options[:stock_symbols]
66
+ if options.nil? || !options.is_a?(Hash)
55
67
  raise QuoteException, "You must provide a hash of stock symbols to fetch data"
56
68
  end
57
69
  if options[:stock_symbols].nil? || options[:stock_symbols].empty?
@@ -60,7 +72,7 @@ module YahooStock
60
72
  if !(options[:read_parameters] && options[:read_parameters].any?)
61
73
  options[:read_parameters] = [:last_trade_price_only, :last_trade_date]
62
74
  end
63
- options[:stock_symbols] = options[:stock_symbols].to_a unless options[:stock_symbols].kind_of?(Array)
75
+ options[:stock_symbols] = Array.new << options[:stock_symbols] unless options[:stock_symbols].is_a?(Array)
64
76
  @interface = YahooStock::Interface.new(options)
65
77
  end
66
78
 
@@ -71,6 +83,21 @@ module YahooStock
71
83
  @interface.results
72
84
  end
73
85
 
86
+ # Create methods:
87
+ # def realtime
88
+ # - To get realtime stock values
89
+ # def extended
90
+ # - To get values for extra parameters
91
+ # def standard
92
+ # - To get standard values for stocks
93
+ %w{realtime extended standard}.each do |quote_type|
94
+ self.send(:define_method, "#{quote_type}".to_sym) do
95
+ clear_parameters
96
+ @interface.send("add_#{quote_type}_params".to_sym)
97
+ get
98
+ end
99
+ end
100
+
74
101
  # Adds more stock symbols to the existing instance.
75
102
  # One or more stock symbols can be passed as parameter.
76
103
  def add_symbols(*symbols)
@@ -213,4 +213,55 @@ describe YahooStock::Interface do
213
213
 
214
214
  end
215
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
221
+ end
222
+
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
+ end
232
+
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
238
+ end
239
+
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
246
+ end
247
+
248
+ end
249
+
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
263
+ end
264
+
265
+ end
266
+
216
267
  end
@@ -10,11 +10,15 @@ module YahooStock
10
10
  end
11
11
 
12
12
  it "should raise QuoteException error when parameter passed is nil" do
13
- lambda { YahooStock::Quote.new(nil)}.should raise_error Quote::QuoteException
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')
14
18
  end
15
19
 
16
20
  it "should raise QuoteException when a hash of stock_symbols are not passed" do
17
- lambda { YahooStock::Quote.new(:misspelled => 'YHOO')}.should raise_error(Quote::QuoteException, 'You must provide a hash of stock symbols to fetch data')
21
+ lambda { YahooStock::Quote.new(:misspelled => 'YHOO')}.should raise_error(Quote::QuoteException, 'You must provide atleast one stock symbol to fetch data')
18
22
  end
19
23
 
20
24
  it "should raise QuoteException when stock symbols hash key value is an emtpy array" do
@@ -227,6 +231,84 @@ module YahooStock
227
231
  end
228
232
 
229
233
  end
234
+
235
+ describe "realtime" do
236
+
237
+ before(:each) do
238
+ @quote.stub!(:get)
239
+ @interface.stub!(:add_realtime_params)
240
+ end
241
+
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
+ it "should add the realtime parameters" do
248
+ @quote.stub!(:clear_parameters)
249
+ @interface.should_receive(:add_realtime_params)
250
+ @quote.realtime
251
+ end
252
+
253
+ it "should get the results" do
254
+ @quote.stub!(:clear_parameters)
255
+ @quote.should_receive(:get)
256
+ @quote.realtime
257
+ end
258
+
259
+ end
260
+
261
+ describe "standard" do
262
+
263
+ before(:each) do
264
+ @quote.stub!(:get)
265
+ @interface.stub!(:add_standard_params)
266
+ end
267
+
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)
275
+ @interface.should_receive(:add_standard_params)
276
+ @quote.standard
277
+ end
278
+
279
+ it "should get the results" do
280
+ @quote.stub!(:clear_parameters)
281
+ @quote.should_receive(:get)
282
+ @quote.standard
283
+ end
284
+
285
+ end
286
+
287
+ describe "extended" do
288
+
289
+ before(:each) do
290
+ @quote.stub!(:get)
291
+ @interface.stub!(:add_extended_params)
292
+ end
293
+
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)
301
+ @interface.should_receive(:add_extended_params)
302
+ @quote.extended
303
+ end
304
+
305
+ it "should get the results" do
306
+ @quote.stub!(:clear_parameters)
307
+ @quote.should_receive(:get)
308
+ @quote.extended
309
+ end
310
+
311
+ end
230
312
 
231
313
  end
232
314
  end
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.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nasir Jamal
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-06 00:00:00 -07:00
12
+ date: 2009-09-07 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency