nas-yahoo_stock 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -7,3 +7,12 @@
7
7
 
8
8
  * 1 minor change:
9
9
  * changed method name YahooStock::Quote#get_data to get
10
+
11
+ === 0.1.3 2009-09-05
12
+
13
+ * 1 addition:
14
+ * adds a new class called YahooStock::ScripSymbol that allows to
15
+ find out the stock symbol for a company
16
+ * adds specs for previous and new code
17
+ * adds rdocs
18
+
data/Manifest.txt CHANGED
@@ -5,5 +5,7 @@ Rakefile
5
5
  lib/yahoo_stock.rb
6
6
  lib/yahoo_stock/quote.rb
7
7
  lib/yahoo_stock/interface.rb
8
+ lib/yahoo_stock/scrip_symbol.rb
8
9
  spec/spec_helper.rb
9
10
  spec/yahoo_stock/quote_spec.rb
11
+ spec/yahoo_stock/scrip_symbol_spec.rb
data/README.rdoc CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
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
- You must know the stock symbol. For instance, YHOO for yahoo, GOOG for google, etc.
9
+ If you don't know the stock / scrip symbol of the company then you can find that out by using the YahooStock::ScripSymbol class. The methods are mentioned in the Usage section below. 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
11
  YahooStock::Quote object and passing valid_parameters message, example is given below in the USAGE section.
12
12
 
@@ -44,6 +44,13 @@ quote.remove_symbols('MSFT', 'AAPL')
44
44
 
45
45
  quote.get
46
46
 
47
+ * To find out the stock symbol for a company
48
+
49
+ YahooStock::ScripSymbol.print_options('Yahoo', 'Company1', 'Company3')
50
+
51
+ * To store the output in a file
52
+
53
+ YahooStock::ScripSymbol.save_options_to_file('path/to/filename','Yahoo', 'Company1', 'Company3')
47
54
 
48
55
  == INSTALL:
49
56
 
@@ -1,20 +1,19 @@
1
1
  =begin
2
2
 
3
+ Parameter source:
3
4
  http://www.gummy-stuff.org/Yahoo-data.htm
4
5
  http://finance.yahoo.com/d/quotes.csv?s=XOM+BBDb.TO+JNJ+MSFT&f=snd1l1yr
5
6
 
6
7
  =end
7
8
 
8
- =begin
9
-
10
- Class to generate the right url and interface with yahoo
11
-
12
- =end
13
-
14
9
  require 'net/http'
15
10
  module YahooStock
11
+ # ==DESCRIPTION:
12
+ #
13
+ # Class to generate the right url and interface with yahoo
14
+ #
16
15
  class Interface
17
-
16
+
18
17
  class InterfaceError < RuntimeError ; end
19
18
 
20
19
  PARAMETERS = {
@@ -103,6 +102,15 @@ module YahooStock
103
102
 
104
103
  attr_accessor :stock_symbols, :yahoo_url_parameters
105
104
 
105
+ # The stock_params_hash parameter expects a hash with two key value pairs
106
+ #
107
+ # :stock_symbols => 'Array of stock symbols'
108
+ #
109
+ # e.g. :stock_symbols => ['YHOO']
110
+ #
111
+ # another hash :read_parameters => 'array of values'
112
+ #
113
+ # e.g. :read_parameters => [:last_trade_price_only, :last_trade_date]
106
114
  def initialize(stock_params_hash)
107
115
  unless stock_params_hash
108
116
  raise InterfaceError, 'You must pass a hash of stock symbols and the data you would like to see'
@@ -118,6 +126,7 @@ module YahooStock
118
126
  @base_url = "http://download.finance.yahoo.com/d/quotes.csv"
119
127
  end
120
128
 
129
+ # Generate full url to be sent to yahoo
121
130
  def full_url
122
131
  all_stock_symbols = stock_symbols.join('+')
123
132
  params = yahoo_url_parameters-allowed_parameters
@@ -134,10 +143,15 @@ module YahooStock
134
143
  "#{@base_url}?s=#{all_stock_symbols}&f=#{parameter_values}"
135
144
  end
136
145
 
146
+ # Get the csv file and create an array of different stock symbols and values returned
147
+ # for the parameters passed based on line break.
137
148
  def get_values
138
149
  Net::HTTP.get(URI.parse(full_url)).gsub(/\"/,'').split(/\r\n/)
139
150
  end
140
151
 
152
+ # Returns results for the stock symbols as a hash.
153
+ # The hash keys are the stock symbols and the values are a hash of the keys and
154
+ # values asked for that stock.
141
155
  def results
142
156
  stock = {}
143
157
  get_values.each_with_index do |values, index|
@@ -150,6 +164,7 @@ module YahooStock
150
164
  stock
151
165
  end
152
166
 
167
+ # Add stock symbols to the url.
153
168
  def add_symbols(*symbols)
154
169
  symbols.each do |symbol|
155
170
  unless stock_symbols.include?(symbol)
@@ -158,6 +173,7 @@ module YahooStock
158
173
  end
159
174
  end
160
175
 
176
+ # Remove stock symbols from the url.
161
177
  def remove_symbols(*symbols)
162
178
  symbols.each do |symbol|
163
179
  unless stock_symbols.include?(symbol)
@@ -167,6 +183,7 @@ module YahooStock
167
183
  end
168
184
  end
169
185
 
186
+ # Add parameters to the url.
170
187
  def add_parameters(*parameters)
171
188
  parameters.each do |parameter|
172
189
  unless allowed_parameters.include?(parameter)
@@ -178,6 +195,7 @@ module YahooStock
178
195
  end
179
196
  end
180
197
 
198
+ # Remove parameters from the url.
181
199
  def remove_parameters(*parameters)
182
200
  parameters.each do |parameter|
183
201
  unless yahoo_url_parameters.include?(parameter)
@@ -187,6 +205,7 @@ module YahooStock
187
205
  end
188
206
  end
189
207
 
208
+ # Returns an array of parameters that can be passed to yahoo.
190
209
  def allowed_parameters
191
210
  parameters.keys
192
211
  end
@@ -1,14 +1,55 @@
1
1
  module YahooStock
2
+ # == DESCRIPTION:
3
+ #
4
+ # Provides the stock related current data.
5
+ #
6
+ # Uses YahooStock::Interface to connect to yahoo and get relevant data.
7
+ #
8
+ # == USAGE:
9
+ #
10
+ # * Initialize quote object
11
+ #
12
+ # quote = YahooStock::Quote.new(:stock_symbols => ['YHOO', 'GOOG'],
13
+ # :read_parameters => [:last_trade_price_only, :last_trade_date])
14
+ #
15
+ # If read_parameters are not provided then by default the above two parameters are used.
16
+ #
17
+ # * To get data for all stocks
18
+ #
19
+ # quote.get
20
+ #
21
+ # * To view the valid parameters that can be passed
22
+ #
23
+ # quote.valid_parameters
24
+ #
25
+ # * To view the current parameters used
26
+ #
27
+ # quote.current_parameters
28
+ #
29
+ # * To view the current stock symbols used
30
+ #
31
+ # quote.current_symbols
32
+ #
33
+ # * To add more stocks to the list
34
+ #
35
+ # quote.add_symbols('MSFT', 'AAPL')
36
+ #
37
+ # * To remove stocks from list
38
+ #
39
+ # quote.remove_symbols('MSFT', 'AAPL')
40
+ #
2
41
  class Quote
3
-
4
42
  class QuoteException < RuntimeError; end
5
43
 
6
- # options expects a hash with two key value pairs
7
- # :stock_symbols => 'Array of stock symbols'
8
- # e.g. :stock_symbols => [:MSFT,:YHOO]
9
- # another :read_parameters => 'array of values'
44
+ # The options parameter expects a hash with two key value pairs
45
+ #
46
+ # :stock_symbols => 'Array of stock symbols' or a single symbol
47
+ #
48
+ # e.g. :stock_symbols => ['MSFT','YHOO'] or :stock_symbols => 'YHOO'
49
+ #
50
+ # another hash :read_parameters => 'array of values'
51
+ #
10
52
  # e.g. :read_parameters => [:last_trade_price_only, :last_trade_date]
11
- # usage YahooStock::Quote.new(:stock_symbols => ['MSFT','YHOO'], :read_parameters => [:last_trade_price_only, :last_trade_date])
12
53
  def initialize(options)
13
54
  if options.nil? || !options[:stock_symbols]
14
55
  raise QuoteException, "You must provide a hash of stock symbols to fetch data"
@@ -23,47 +64,64 @@ module YahooStock
23
64
  @interface = YahooStock::Interface.new(options)
24
65
  end
25
66
 
67
+ # Returns results for the stock symbols as a hash.
68
+ # The hash keys are the stock symbols and the values are a hash of the keys and
69
+ # values asked for that stock.
26
70
  def get
27
71
  @interface.results
28
72
  end
29
73
 
74
+ # Adds more stock symbols to the existing instance.
75
+ # One or more stock symbols can be passed as parameter.
30
76
  def add_symbols(*symbols)
31
77
  symbols.each { |symbol| @interface.add_symbols(symbol) }
32
78
  end
33
79
 
80
+ # Removes stock symbols from the existing instance.
81
+ # One of more stock symbols can be passed to remove.
34
82
  def remove_symbols(*symbols)
35
83
  symbols.each { |symbol| @interface.remove_symbols(symbol) }
36
84
  end
37
85
 
86
+ # Clear all existing stock symbols from the current instance.
38
87
  def clear_symbols
39
88
  @interface.stock_symbols.clear
40
89
  end
41
90
 
91
+ # Show all stock symbols in the current instance that will be used to get results.
42
92
  def current_symbols
43
93
  @interface.stock_symbols
44
94
  end
45
95
 
96
+ # Adds more parameters for the stock symbols to the existing instance for getting data.
97
+ # One or more parameters can be passed as argument.
46
98
  def add_parameters(*parameters)
47
99
  parameters.each { |parameter| @interface.add_parameters(parameter) }
48
100
  end
49
101
 
102
+ # Removes parameters for the stock symbols to get values for from the existing instance.
103
+ # One of more parameters can be passed to remove.
50
104
  def remove_parameters(*parameters)
51
105
  parameters.each { |parameter| @interface.remove_parameters(parameter) }
52
106
  end
53
107
 
108
+ # Shows all parameters in the current instance that will be used to get results.
54
109
  def current_parameters
55
110
  sort_symbols(@interface.yahoo_url_parameters)
56
111
  end
57
112
 
113
+ # Set current instance to use all parameters to fetch values for current symbols.
58
114
  def use_all_parameters
59
115
  params = valid_parameters.each {|parameter| add_parameters(parameter)}
60
116
  sort_symbols(params)
61
117
  end
62
118
 
119
+ # Clear all existing parameters from the current instance.
63
120
  def clear_parameters
64
121
  @interface.yahoo_url_parameters.clear
65
122
  end
66
123
 
124
+ # Returns an array of all allowed parameters that can be used.
67
125
  def valid_parameters
68
126
  sort_symbols(@interface.allowed_parameters)
69
127
  end
@@ -0,0 +1,99 @@
1
+ require 'net/http'
2
+
3
+ module YahooStock
4
+ # == DESCRIPTION:
5
+ #
6
+ # This class provides the ability to find out the stock /scrip symbols for a company used in stock exchanges.
7
+ #
8
+ # It uses Yahoo http://finance.yahoo.com/lookup page to find, screen scrape / parse the returned results.
9
+ #
10
+ # == USAGE:
11
+ #
12
+ # * If you want to use the symbols in your existing code then:
13
+ #
14
+ # symbol = YahooStock::ScripSymbol.new('company name')
15
+ #
16
+ # symbol.find
17
+ #
18
+ # will give you an array of arrays where each outer array is the different option for the company name
19
+ # you provided and the inner array includes stock symbol, full company name, stock price, exchange symbol
20
+ # so that you can decide easily what symbol you can use.
21
+ #
22
+ # * If you just want to print the results on your console screen
23
+ #
24
+ # YahooStock::ScripSymbol.print_options('company name')
25
+ #
26
+ # * If you just want to store the results in file to use it later
27
+ #
28
+ # You can pass in any number of companies in the parameter
29
+ #
30
+ # YahooStock::ScripSymbol.save_options_to_file('path/to/filename','company1', 'company2')
31
+ #
32
+ class ScripSymbol
33
+ # Initialize with the name of the company as parameter for which stock symbol is needed
34
+ #
35
+ # symbol = YahooStock::ScripSymbol.new('company name')
36
+ #
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
+
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.any?
56
+ row_data.reject!{|rd| !rd.any?}
57
+ end
58
+ data << row_data if row_data.length > 1
59
+ end
60
+ data
61
+ end
62
+
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
71
+ end
72
+
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
84
+ end
85
+
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
96
+ end
97
+
98
+ end
99
+ end
data/lib/yahoo_stock.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
+ require 'yahoo_stock/scrip_symbol.rb'
4
5
  require 'yahoo_stock/interface.rb'
5
6
  require 'yahoo_stock/quote.rb'
6
7
 
7
8
  module YahooStock
8
- VERSION = '0.0.1'
9
+ VERSION = '0.1.3'
9
10
  end
@@ -0,0 +1,111 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe YahooStock::ScripSymbol do
4
+
5
+ describe "find" do
6
+ before(:each) do
7
+ @script_symbol = YahooStock::ScripSymbol.new('company1')
8
+ end
9
+
10
+ it "should get results" do
11
+ @script_symbol.should_receive(:get_results)
12
+ @script_symbol.find
13
+ end
14
+
15
+ it "should convert the results to string and split them on the basis of tr tags" do
16
+ res = 'Some results returned'
17
+ @script_symbol.stub!(:get_results).and_return(res)
18
+ res.should_receive(:split).with(/\<\/tr>/).and_return([])
19
+ @script_symbol.find
20
+ end
21
+
22
+ it "should return an array of options" do
23
+ @script_symbol.stub!(:get_results).and_return('ss')
24
+ @script_symbol.find.class.should eql(Array)
25
+ end
26
+ end
27
+
28
+ describe ".print_options" do
29
+ before(:each) do
30
+ @symbol = stub(YahooStock::ScripSymbol)
31
+ @script_symbol = YahooStock::ScripSymbol.stub!(:new).and_return(@symbol)
32
+ end
33
+
34
+ it "should find scrip symbol for one company" do
35
+ @symbol.should_receive(:find).once.and_return([])
36
+ YahooStock::ScripSymbol.print_options('company1')
37
+ end
38
+
39
+ it "should find scrip symbols for two companies" do
40
+ @symbol.should_receive(:find).twice.and_return([])
41
+ YahooStock::ScripSymbol.print_options('company1', 'company2')
42
+ end
43
+
44
+ it "should return nil" do
45
+ @symbol.should_receive(:find).and_return([])
46
+ YahooStock::ScripSymbol.print_options('company1').should eql(nil)
47
+ end
48
+
49
+ it "should print the results for each scrip or company, i.e twice" do
50
+ scrip = ['name','stock']
51
+ @symbol.stub!(:find).and_return([scrip])
52
+ YahooStock::ScripSymbol.should_receive(:p).with(scrip).twice
53
+ YahooStock::ScripSymbol.print_options('company1','company2')
54
+ end
55
+
56
+ it "should print the results for each scrip or company, 4times" do
57
+ scrip = ['name','stock']
58
+ @symbol.stub!(:find).and_return([scrip,scrip])
59
+ YahooStock::ScripSymbol.should_receive(:p).with(scrip).exactly(4)
60
+ YahooStock::ScripSymbol.print_options('company1','company2')
61
+ end
62
+ end
63
+
64
+ describe ".save_options_to_file" do
65
+ before(:each) do
66
+ @symbol = stub(YahooStock::ScripSymbol)
67
+ @script_symbol = YahooStock::ScripSymbol.stub!(:new).and_return(@symbol)
68
+ @file = stub(File)
69
+ @file_name = 'file_name'
70
+ end
71
+
72
+ it "should create or open a file with file name parameter and appends to it" do
73
+ File.should_receive(:open).with(@file_name, 'a')
74
+ YahooStock::ScripSymbol.save_options_to_file('file_name', 'company1')
75
+ end
76
+
77
+ it "should find scrip symbol for one company" do
78
+ @symbol.should_receive(:find).once.and_return([])
79
+ YahooStock::ScripSymbol.save_options_to_file(@file_name,'company1')
80
+ end
81
+
82
+ it "should find scrip symbols for two companies" do
83
+ @symbol.should_receive(:find).twice.and_return([])
84
+ YahooStock::ScripSymbol.save_options_to_file(@file_name, 'company1','company2')
85
+ end
86
+
87
+ it "should have one option for each company in the file" do
88
+ scrip = ['name','stock']
89
+ @symbol.stub!(:find).and_return([scrip])
90
+ YahooStock::ScripSymbol.save_options_to_file(@file_name, 'company1','company2')
91
+ file_data.should eql(["name, stock\n", "name, stock\n"])
92
+ end
93
+
94
+ it "should have two option for each company in the file" do
95
+ scrip = ['name','stock']
96
+ @symbol.stub!(:find).and_return([scrip, scrip])
97
+ YahooStock::ScripSymbol.save_options_to_file(@file_name, 'company1','company2')
98
+ file_data.should eql(["name, stock\n", "name, stock\n", "name, stock\n", "name, stock\n"])
99
+ end
100
+
101
+ def file_data
102
+ File.open(@file_name, 'r') { |f| return f.readlines }
103
+ end
104
+
105
+ after(:each) do
106
+ File.delete(@file_name) if File.exists? @file_name
107
+ end
108
+
109
+ end
110
+
111
+ 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.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nasir Jamal
@@ -48,12 +48,13 @@ files:
48
48
  - lib/yahoo_stock.rb
49
49
  - lib/yahoo_stock/quote.rb
50
50
  - lib/yahoo_stock/interface.rb
51
+ - lib/yahoo_stock/scrip_symbol.rb
51
52
  - spec/spec_helper.rb
52
53
  - spec/yahoo_stock/quote_spec.rb
53
54
  - spec/yahoo_stock/interface_spec.rb
55
+ - spec/yahoo_stock/scrip_symbol_spec.rb
54
56
  has_rdoc: true
55
57
  homepage: http://github.com/nas/yahoo_stock
56
- licenses:
57
58
  post_install_message:
58
59
  rdoc_options:
59
60
  - --inline-source
@@ -75,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
76
  requirements: []
76
77
 
77
78
  rubyforge_project:
78
- rubygems_version: 1.3.5
79
+ rubygems_version: 1.2.0
79
80
  signing_key:
80
81
  specification_version: 2
81
82
  summary: Yahoo Stock is a Ruby library for extracting information about stocks from yahoo finance.