Class: YahooStock::ScripSymbol
- Inherits:
-
Object
- Object
- YahooStock::ScripSymbol
- Defined in:
- lib/yahoo_stock/scrip_symbol.rb
Overview
DESCRIPTION:
This class provides the ability to find out the stock /scrip symbols for a company used in stock exchanges.
It uses Yahoo finance.yahoo.com/lookup page to find, screen scrape / parse the returned results.
USAGE:
-
If you want to use the symbols in your existing code then:
symbol = YahooStock::ScripSymbol.new('company name') symbol.find
will give you an array of arrays where each outer array is the different option for the company name you provided and the inner array includes stock symbol, full company name, stock price, exchange symbol so that you can decide easily what symbol you can use.
-
If you just want to print the results on your console screen
YahooStock::ScripSymbol.('company name')
-
If you just want to store the results in file to use it later
You can pass in any number of companies in the parameter
YahooStock::ScripSymbol.('path/to/filename','company1', 'company2')
Class Method Summary collapse
-
.print_options(*companies) ⇒ Object
This is just a convenience method to print all results on your console screen and to return nil at the end.
-
.save_options_to_file(file_name, *companies) ⇒ Object
Another convenience method to store all returned results into a file.
Instance Method Summary collapse
-
#find ⇒ Object
Returns an array of arrays where each outer array is the different option for the company name you provided and the inner array includes stock symbol, full company name, stock price, exchange symbol.
-
#initialize(company) ⇒ ScripSymbol
constructor
Initialize with the name of the company as parameter for which stock symbol is needed.
Constructor Details
#initialize(company) ⇒ ScripSymbol
Initialize with the name of the company as parameter for which stock symbol is needed
symbol = YahooStock::ScripSymbol.new('company name')
symbol.find
38 39 40 41 42 43 |
# File 'lib/yahoo_stock/scrip_symbol.rb', line 38 def initialize(company) company = company.gsub(/\s/,'+') @base_url = "http://finance.yahoo.com/lookup/all?s=#{company}" @before_element = 'yfi_sym_results' @after_element = 'yfi_fp_left_bottom' end |
Class Method Details
.print_options(*companies) ⇒ Object
This is just a convenience method to print all results on your console screen and to return nil at the end. It uses find method to print symbols on the screen.
65 66 67 68 69 70 71 |
# File 'lib/yahoo_stock/scrip_symbol.rb', line 65 def self.(*companies) companies.each do |name| scrip_symbol = self.new(name) scrip_symbol.find.each {|scrip| p scrip} end nil end |
.save_options_to_file(file_name, *companies) ⇒ Object
Another convenience method to store all returned results into a file
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/yahoo_stock/scrip_symbol.rb', line 74 def self.(file_name, *companies) File.open(file_name, 'a') do |f| companies.each do |name| scrip_symbol = self.new(name) scrip_symbol.find.each do |scrip| f.write(scrip.join(', ')) f.puts('') end end end end |
Instance Method Details
#find ⇒ Object
Returns an array of arrays where each outer array is the different option for the company name you provided and the inner array includes stock symbol, full company name, stock price, exchange symbol.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/yahoo_stock/scrip_symbol.rb', line 47 def find data = [] rows = get_results.to_s.split(/\<\/tr>/) rows.each_with_index do |row, row_i| cells = row.split(/\<\/td>/) row_data = [] cells.each_with_index do |cell, cell_i| datum = cell.sub('</a>','').gsub(/\<.*\>/,'') row_data << datum if !datum.nil? || datum.empty? row_data.reject!{|rd| rd.empty?} end data << row_data if row_data.length > 1 end data end |