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.
@@ -50,7 +50,7 @@ module YahooStock
50
50
  #
51
51
  # quote.remove_symbols('MSFT', 'AAPL')
52
52
  #
53
- class Quote
53
+ class Quote < Base
54
54
  class QuoteException < RuntimeError; end
55
55
 
56
56
  # The options parameter expects a hash with two key value pairs
@@ -73,14 +73,7 @@ module YahooStock
73
73
  options[:read_parameters] = [:last_trade_price_only, :last_trade_date]
74
74
  end
75
75
  options[:stock_symbols] = Array.new << options[:stock_symbols] unless options[:stock_symbols].is_a?(Array)
76
- @interface = YahooStock::Interface.new(options)
77
- end
78
-
79
- # Returns results for the stock symbols as a hash.
80
- # The hash keys are the stock symbols and the values are a hash of the keys and
81
- # values asked for that stock.
82
- def get
83
- @interface.results
76
+ @interface = YahooStock::Interface::Quote.new(options)
84
77
  end
85
78
 
86
79
  # Create methods:
@@ -92,27 +85,26 @@ module YahooStock
92
85
  # - To get standard values for stocks
93
86
  %w{realtime extended standard}.each do |quote_type|
94
87
  self.send(:define_method, "#{quote_type}".to_sym) do
95
- clear_parameters
96
88
  @interface.send("add_#{quote_type}_params".to_sym)
97
- get
89
+ return self
98
90
  end
99
91
  end
100
92
 
101
93
  # Adds more stock symbols to the existing instance.
102
94
  # One or more stock symbols can be passed as parameter.
103
95
  def add_symbols(*symbols)
104
- symbols.each { |symbol| @interface.add_symbols(symbol) }
96
+ @interface.add_symbols(*symbols)
105
97
  end
106
98
 
107
99
  # Removes stock symbols from the existing instance.
108
100
  # One of more stock symbols can be passed to remove.
109
101
  def remove_symbols(*symbols)
110
- symbols.each { |symbol| @interface.remove_symbols(symbol) }
102
+ @interface.remove_symbols(*symbols)
111
103
  end
112
104
 
113
105
  # Clear all existing stock symbols from the current instance.
114
106
  def clear_symbols
115
- @interface.stock_symbols.clear
107
+ @interface.clear_symbols
116
108
  end
117
109
 
118
110
  # Show all stock symbols in the current instance that will be used to get results.
@@ -123,20 +115,22 @@ module YahooStock
123
115
  # Adds more parameters for the stock symbols to the existing instance for getting data.
124
116
  # One or more parameters can be passed as argument.
125
117
  def add_parameters(*parameters)
126
- parameters.each { |parameter| @interface.add_parameters(parameter) }
118
+ @interface.add_parameters(*parameters)
127
119
  end
128
120
 
129
121
  # Removes parameters for the stock symbols to get values for from the existing instance.
130
122
  # One of more parameters can be passed to remove.
131
123
  def remove_parameters(*parameters)
132
- parameters.each { |parameter| @interface.remove_parameters(parameter) }
124
+ @interface.remove_parameters(*parameters)
133
125
  end
134
126
 
135
127
  # Shows all parameters in the current instance that will be used to get results.
136
128
  def current_parameters
137
- sort_symbols(@interface.yahoo_url_parameters)
129
+ @interface.yahoo_url_parameters
138
130
  end
139
131
 
132
+ alias :data_attributes :current_parameters
133
+
140
134
  # Set current instance to use all parameters to fetch values for current symbols.
141
135
  def use_all_parameters
142
136
  params = valid_parameters.each {|parameter| add_parameters(parameter)}
@@ -145,7 +139,8 @@ module YahooStock
145
139
 
146
140
  # Clear all existing parameters from the current instance.
147
141
  def clear_parameters
148
- @interface.yahoo_url_parameters.clear
142
+ @interface.clear_parameters
143
+ current_parameters
149
144
  end
150
145
 
151
146
  # Returns an array of all allowed parameters that can be used.
@@ -0,0 +1,27 @@
1
+ module YahooStock
2
+
3
+ # == DESCRIPTION:
4
+ # Parse results to show in an array form
5
+ #
6
+ # == USAGE
7
+ # YahooStock::Result::ArrayFormat.new("data as commma separated values").output
8
+ #
9
+ # Mostly will be used as a separate strategy for formatting results
10
+ class Result::ArrayFormat < Result
11
+
12
+ def initialize(data)
13
+ @data = data
14
+ super(self)
15
+ end
16
+
17
+ def output
18
+ # Some inconsistency happens hence remove quotes
19
+ val = @data.gsub(/\"/,'').split(/\r\n|\n/)
20
+ new_val = []
21
+ val.each {|v| new_val << v.split(',')}
22
+ return new_val
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,37 @@
1
+ module YahooStock
2
+ # == DESCRIPTION:
3
+ # Convert results as an array of key value pairs
4
+ #
5
+ # == USAGE
6
+ # YahooStock::Result::HashFormat.new("data as string"){['array', 'of', 'keys']}.output
7
+ # The number of keys in the block array must be equal to the values expected to be returned
8
+ #
9
+ # Mostly will be used as a separate strategy for formatting results
10
+ class Result::HashFormat < Result
11
+
12
+ def initialize(data, &block)
13
+ @data = YahooStock::Result::ArrayFormat.new(data).output
14
+ @keys = yield
15
+ super(self)
16
+ end
17
+
18
+ def output
19
+ data = []
20
+ @data.each do |datum|
21
+ row_values = {}
22
+ datum.each_with_index do |item, i|
23
+ row_values[keys[i]] = item
24
+ end
25
+ data << row_values
26
+ end
27
+ data
28
+ end
29
+
30
+ def keys
31
+ @keys.collect{|key| key.to_s.gsub(/\s/,'_').downcase.to_sym}
32
+ end
33
+
34
+ end
35
+
36
+
37
+ end
@@ -0,0 +1,21 @@
1
+ module YahooStock
2
+ class Result
3
+
4
+ def initialize(context)
5
+ @context = context
6
+ end
7
+
8
+ def output
9
+ return @context if @context.is_a?(String)
10
+ @context.output
11
+ end
12
+
13
+ def store(filename)
14
+ File.open(filename, 'a') do |f|
15
+ f.write(output)
16
+ f.write("\n")
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -1,5 +1,3 @@
1
- require 'net/http'
2
-
3
1
  module YahooStock
4
2
  # == DESCRIPTION:
5
3
  #
@@ -13,7 +11,9 @@ module YahooStock
13
11
  #
14
12
  # symbol = YahooStock::ScripSymbol.new('company name')
15
13
  #
16
- # symbol.find
14
+ # symbol.find #will return a string
15
+ #
16
+ # symbol.results :to_array, #will return an array of values instead of a string
17
17
  #
18
18
  # will give you an array of arrays where each outer array is the different option for the company name
19
19
  # you provided and the inner array includes stock symbol, full company name, stock price, exchange symbol
@@ -29,71 +29,33 @@ module YahooStock
29
29
  #
30
30
  # YahooStock::ScripSymbol.save_options_to_file('path/to/filename','company1', 'company2')
31
31
  #
32
- class ScripSymbol
32
+ class ScripSymbol < Base
33
33
  # Initialize with the name of the company as parameter for which stock symbol is needed
34
34
  #
35
35
  # symbol = YahooStock::ScripSymbol.new('company name')
36
36
  #
37
37
  # symbol.find
38
- def initialize(company)
39
- company = company.gsub(/\s/,'+')
40
- @base_url = "http://finance.yahoo.com/lookup/all?s=#{company}"
41
- @before_element = 'yfi_sym_results'
42
- @after_element = 'yfi_fp_left_bottom'
43
- end
44
38
 
45
- # Returns an array of arrays where each outer array is the different option for the company name
46
- # you provided and the inner array includes stock symbol, full company name, stock price, exchange symbol.
47
- def find
48
- data = []
49
- rows = get_results.to_s.split(/\<\/tr>/)
50
- rows.each_with_index do |row, row_i|
51
- cells = row.split(/\<\/td>/)
52
- row_data = []
53
- cells.each_with_index do |cell, cell_i|
54
- datum = cell.sub('</a>','').gsub(/\<.*\>/,'')
55
- row_data << datum if !datum.nil? || datum.empty?
56
- row_data.reject!{|rd| rd.empty?}
57
- end
58
- data << row_data if row_data.length > 1
59
- end
60
- data
61
- end
39
+ attr_reader :company
62
40
 
63
- # This is just a convenience method to print all results on your console screen
64
- # and to return nil at the end. It uses find method to print symbols on the screen.
65
- def self.print_options(*companies)
66
- companies.each do |name|
67
- scrip_symbol = self.new(name)
68
- scrip_symbol.find.each {|scrip| p scrip}
69
- end
70
- nil
41
+ def initialize(company_name)
42
+ @company = company_name
43
+ @interface = YahooStock::Interface::ScripSymbol.new(@company)
71
44
  end
72
45
 
73
- # Another convenience method to store all returned results into a file
74
- def self.save_options_to_file(file_name, *companies)
75
- File.open(file_name, 'a') do |f|
76
- companies.each do |name|
77
- scrip_symbol = self.new(name)
78
- scrip_symbol.find.each do |scrip|
79
- f.write(scrip.join(', '))
80
- f.puts('')
81
- end
82
- end
83
- end
46
+ def company=(company)
47
+ @company = @interface.company = company
84
48
  end
85
49
 
86
- private
87
-
88
- # Makes an http request to the given url and returns only the text among two points
89
- def get_results
90
- @body ||= Net::HTTP.get(URI.parse(@base_url)).gsub!(/\s*/,'')
91
- pattern = /#{@before_element}.*#{@after_element}/
92
- results = pattern.match(@body)
93
- if results
94
- return results[0]
95
- end
50
+ def data_attributes
51
+ ['Symbol', 'Name', 'Last Trade', 'Type', 'Industry Category', 'Exchange']
96
52
  end
97
53
 
54
+ # get stock symbols for multilple companies
55
+ def self.results(*companies)
56
+ res = companies.inject('') { |options, company| options + new(company).results.output + "\n" }
57
+ YahooStock::Result.new(res)
58
+ end
59
+
98
60
  end
99
61
  end
data/lib/yahoo_stock.rb CHANGED
@@ -2,11 +2,18 @@ $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
4
  require 'date'
5
- require 'yahoo_stock/scrip_symbol.rb'
5
+ require 'yahoo_stock/base.rb'
6
6
  require 'yahoo_stock/interface.rb'
7
- require 'yahoo_stock/quote.rb'
8
7
  require 'yahoo_stock/history.rb'
8
+ require 'yahoo_stock/scrip_symbol.rb'
9
+ require 'yahoo_stock/quote.rb'
10
+ require 'yahoo_stock/interface/quote.rb'
11
+ require 'yahoo_stock/interface/history.rb'
12
+ require 'yahoo_stock/interface/scrip_symbol.rb'
13
+ require 'yahoo_stock/result.rb'
14
+ require 'yahoo_stock/result/array_format.rb'
15
+ require 'yahoo_stock/result/hash_format.rb'
9
16
 
10
17
  module YahooStock
11
- VERSION = '0.1.3'
18
+ VERSION = '1.0.2'
12
19
  end
@@ -0,0 +1,48 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe YahooStock::Base do
4
+ before(:each) do
5
+ @interface = stub!('Base')
6
+ @base = YahooStock::Base.new(@interface)
7
+ end
8
+
9
+ describe "find" do
10
+
11
+ it "should get the values from interface" do
12
+ @interface.should_receive(:values)
13
+ @base.find
14
+ end
15
+ end
16
+
17
+ describe "results" do
18
+
19
+ it "should return the results using find" do
20
+ @base.should_receive(:find)
21
+ @base.results
22
+ end
23
+
24
+ it "should yield the block if a block is given" do
25
+ @base.results {"Block to pass, probably, a class with result output method"
26
+ }.should eql('Block to pass, probably, a class with result output method')
27
+ end
28
+
29
+ it "should use the ArrayFormat class for results when :to_array is passed to the results param" do
30
+ @base.stub!(:find)
31
+ YahooStock::Result::ArrayFormat.should_receive(:new)
32
+ @base.results(:to_array)
33
+ end
34
+
35
+ it "should create the YahooStock::Result object when no formatting option is provided" do
36
+ find = stub('find')
37
+ @base.stub!(:find).and_return(find)
38
+ YahooStock::Result.should_receive(:new).with(find)
39
+ @base.results
40
+ end
41
+
42
+ end
43
+ describe "data_attributes" do
44
+ it "should raise an error if the data_attributes is called as this is an abstract method" do
45
+ lambda { @base.data_attributes }.should raise_error(RuntimeError, 'Abstract method called. Use the subclass data_attributes method')
46
+ end
47
+ end
48
+ end
@@ -14,177 +14,62 @@ describe YahooStock::History do
14
14
  }.should raise_error(YahooStock::History::HistoryError, 'A hash of start_date, end_date and stock_symbol is expected as parameters')
15
15
  end
16
16
 
17
- it "should raise an error if stock symbol key is not present in parameter hash" do
18
- lambda { YahooStock::History.new(:start_date => Date.today-7)
19
- }.should raise_error(YahooStock::History::HistoryError, ':stock_symbol key is not present in the parameter hash')
17
+ it "should inititalize the history interface" do
18
+ options = {}
19
+ YahooStock::Interface::History.should_receive(:new).with(options)
20
+ YahooStock::History.new({})
20
21
  end
21
22
 
22
- it "should raise error when stock_symbol value in hash is nil" do
23
- lambda { YahooStock::History.new(:stock_symbol => nil)
24
- }.should raise_error(YahooStock::History::HistoryError, ':stock_symbol value cannot be nil or blank')
25
- end
26
-
27
- it "should raise error when stock_symbol value in hash is blank" do
28
- lambda { YahooStock::History.new(:stock_symbol => '')
29
- }.should raise_error(YahooStock::History::HistoryError, ':stock_symbol value cannot be nil or blank')
30
- end
31
-
32
- it "should raise error when start date key is not present in parameter hash" do
33
- lambda { YahooStock::History.new(:stock_symbol => 'd') }.should raise_error(YahooStock::History::HistoryError, ':start_date key is not present in the parameter hash')
34
- end
35
-
36
- it "should raise error when start_date value is nil" do
37
- lambda { YahooStock::History.new(:stock_symbol => 'd', :start_date => nil) }.should raise_error(YahooStock::History::HistoryError, ':start_date value cannot be blank')
38
- end
39
-
40
- it "should raise error when start_date value is blank" do
41
- lambda { YahooStock::History.new(:stock_symbol => 'd', :start_date => '') }.should raise_error(YahooStock::History::HistoryError, ':start_date value cannot be blank')
42
- end
43
-
44
- it "should raise error when start date value is not of type date" do
45
- lambda { YahooStock::History.new(:stock_symbol => 'd', :start_date => '23/3/2009') }.should raise_error(YahooStock::History::HistoryError, 'Start date must be of type Date')
46
- end
47
-
48
- it "should raise error when end date key is not present in parameter hash" do
49
- lambda { YahooStock::History.new(:stock_symbol => 'd', :start_date => Date.today-1) }.should raise_error(YahooStock::History::HistoryError, ':end_date key is not present in the parameter hash')
50
- end
51
-
52
- it "should raise error when end_date value is nil" do
53
- lambda { YahooStock::History.new(:stock_symbol => 'd',
54
- :start_date => Date.today-1,
55
- :end_date => nil) }.should raise_error(YahooStock::History::HistoryError, ':end_date value cannot be blank')
56
- end
57
-
58
- it "should raise error when end_date value is blank" do
59
- lambda { YahooStock::History.new(:stock_symbol => 'd',
60
- :start_date => Date.today-1,
61
- :end_date => '') }.should raise_error(YahooStock::History::HistoryError, ':end_date value cannot be blank')
62
- end
63
-
64
- it "should raise error when end date value is not of type date" do
65
- lambda { YahooStock::History.new(:stock_symbol => 'd',
66
- :start_date => Date.today-1,
67
- :end_date => 'sds') }.should raise_error(YahooStock::History::HistoryError, 'End date must be of type Date')
68
- end
69
-
70
- it "should raise error when start date is not less than today" do
71
- lambda { YahooStock::History.new(:stock_symbol => 'd',
72
- :start_date => Date.today,
73
- :end_date => Date.today)
74
- }.should raise_error(YahooStock::History::HistoryError, 'Start date must be in the past')
75
- end
76
-
77
- it "should raise error when start date is greater than end date" do
78
- lambda { YahooStock::History.new(:stock_symbol => 'd',
79
- :start_date => Date.today-7,
80
- :end_date => Date.today-8)
81
- }.should raise_error(YahooStock::History::HistoryError, 'End date must be greater than the start date')
82
- end
83
-
84
- it "should not raise error when start date is in the past, end date is greater than start date and all relevant keys are present with right type" do
85
- lambda { YahooStock::History.new(:stock_symbol => 'd',
86
- :start_date => Date.today-7,
87
- :end_date => Date.today-1)
88
- }.should_not raise_error
89
-
90
- end
91
-
92
- it "should by default set interval to daily if no value provided for it" do
93
- @history = YahooStock::History.new(:stock_symbol => 'd',
94
- :start_date => Date.today-7,
95
- :end_date => Date.today-1)
96
- @history.interval.should eql(:daily)
97
- end
98
-
99
- it "should raise invalid keys error when an invalid key is passed in the parameter" do
100
- lambda { YahooStock::History.new(:stock_symbol => 'd',
101
- :start_date => Date.today-7,
102
- :end_date => Date.today-1, :boom => 1) }.should raise_error(YahooStock::History::HistoryError, "An invalid key 'boom' is passed in the parameters. Allowed keys are stock_symbol, start_date, end_date, interval")
103
- end
104
-
105
- it "should raise error when interval is neither :daily, :weekly or :monthly" do
106
- lambda { YahooStock::History.new(:stock_symbol => 'd',
107
- :start_date => Date.today-7,
108
- :end_date => Date.today-1,
109
- :interval => :yearly)
110
- }.should raise_error(YahooStock::History::HistoryError, "Allowed values for interval are daily, weekly, monthly")
111
-
112
- end
113
-
114
- it "should not raise error when interval is :daily" do
115
- lambda { YahooStock::History.new(:stock_symbol => 'd',
116
- :start_date => Date.today-7,
117
- :end_date => Date.today-1,
118
- :interval => :daily)
119
- }.should_not raise_error
120
-
121
- end
122
23
  end
123
24
 
124
- describe "setters" do
25
+ describe "find" do
125
26
  before(:each) do
126
- @history = YahooStock::History.new(:stock_symbol => 'd',
127
- :start_date => Date.today-7,
128
- :end_date => Date.today-1)
129
- end
130
-
131
- it "should raise error when stock symbol is being set to nil" do
132
- lambda { @history.stock_symbol = nil
133
- }.should raise_error(YahooStock::History::HistoryError, ':stock_symbol value cannot be nil or blank')
27
+ @interface = stub('YahooStock::Interface::History')
28
+ YahooStock::Interface::History.stub!(:new).and_return(@interface)
29
+ @interface.stub!(:values)
30
+ @history = YahooStock::History.new(:stock_symbol => 'a symbol', :start_date => Date.today-3, :end_date => Date.today-1)
134
31
  end
135
32
 
136
- it "should raise error when stock symbol is being set to an empty string" do
137
- lambda { @history.stock_symbol = ''
138
- }.should raise_error(YahooStock::History::HistoryError, ':stock_symbol value cannot be nil or blank')
33
+ it "should find values using interface" do
34
+ @interface.should_receive(:values).and_return('data')
35
+ @history.find
139
36
  end
140
37
 
141
- it "should set the new stock symbol value when it is valid" do
142
- @history.stock_symbol = 'new_val'
143
- @history.stock_symbol.should eql('new_val')
38
+ it "should remove the first line from the data as it is just the headings" do
39
+ data = 'data returned'
40
+ @interface.stub!(:values).and_return(data)
41
+ data.should_receive(:sub).with(/Date.*\s/,'')
42
+ @history.find
144
43
  end
145
-
146
- it "should raise error when start date type is not a date" do
147
- lambda { @history.start_date = '21/3/2009'
148
- }.should raise_error(YahooStock::History::HistoryError, 'Start date must be of type Date')
149
- end
150
-
151
- it "should raise error when start date is not in the past" do
152
- lambda { @history.start_date = Date.today
153
- }.should raise_error(YahooStock::History::HistoryError, 'Start date must be in the past')
154
- end
155
-
156
- it "should set the start date when start date is valid" do
157
- s_date = Date.today-1
158
- @history.start_date = s_date
159
- @history.start_date.should eql(s_date)
160
- end
161
-
162
- it "should raise error when end date type is not a date" do
163
- lambda { @history.end_date = '21/3/2009'
164
- }.should raise_error(YahooStock::History::HistoryError, 'End date must be of type Date')
44
+ end
45
+
46
+ describe "data_attributes" do
47
+ before(:each) do
48
+ @history = YahooStock::History.new(:stock_symbol => 'a symbol', :start_date => Date.today-3, :end_date => Date.today-1)
49
+ @data = "Date, price, etc \n23, 34, 44\n\n222,234,2"
50
+ @history.stub!(:values_with_header).and_return(@data)
165
51
  end
166
52
 
167
- it "should raise error when end date type is not in the past" do
168
- lambda { @history.end_date = Date.today
169
- }.should raise_error(YahooStock::History::HistoryError, 'End date must be in the past')
53
+ it "should return nil if history values are not present" do
54
+ @history.stub!(:values_with_header)
55
+ @history.data_attributes.should eql(nil)
170
56
  end
171
57
 
172
- it "should raise error when end date type is not in the past" do
173
- e_date = Date.today-1
174
- @history.end_date = e_date
175
- @history.end_date.should eql(e_date)
58
+ it "should return only header values if history values are present" do
59
+ @history.data_attributes.should eql(["Date","price","etc"])
176
60
  end
177
61
 
178
- it "should raise error when settin interval other than :daily, :monthly or :weekly" do
179
- lambda { @history.interval = :yearly }.should raise_error(YahooStock::History::HistoryError)
180
- end
62
+ end
63
+
64
+ describe "values_with_header" do
181
65
 
182
- it "should set the interval" do
183
- @history.interval = :daily
184
- @history.interval.should eql(:daily)
66
+ it "should get values from the interface" do
67
+ @interface = stub('YahooStock::Interface::History')
68
+ YahooStock::Interface::History.stub!(:new).and_return(@interface)
69
+ @history = YahooStock::History.new(:stock_symbol => 'a symbol', :start_date => Date.today-3, :end_date => Date.today-1)
70
+ @interface.should_receive(:values)
71
+ @history.values_with_header
185
72
  end
186
-
187
73
  end
188
74
 
189
-
190
75
  end