require 'ostruct'
require 'csv'
require 'open-uri'
-require 'rest-client'
+require 'uri'
+
+require 'yahoo_finanza/parser'
module YahooFinanza
- # Finance Utils For Yahoo Finanza
+ class MarketDoesntExistError < Exception; end
+
module FinanceUtils
def self.included(base)
base.extend(self)
end
- def method_missing(method_sym, *args, &block)
- if !!(method_sym =~ /'_url'/)
- "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=#{method_sym}&render=download"
- end
- end
-
- %w(nasdaq nyse amex).each do |market_symbol|
- define_method "#{market_symbol}_url".to_sym do
- "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=#{market_symbol}&render=download"
- end
- end
-
- %w(nnm nyse).each do |market_symbol|
- define_method "active_#{market_symbol}_url".to_sym do
- "http://online.wsj.com/mdc/public/page/2_3021-activ#{market_symbol}-actives.html"
- end
- end
-
- def search_endpoint(key)
- "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=#{key}&callback=YAHOO.Finance.SymbolSuggest.ssCallback"
- end
-
def symbols_by_market(market)
- symbols = []
- return symbols if market.nil?
- if respond_to?("#{market}_url".to_sym)
- url = send("#{market}_url".to_sym)
- market_file = open(url)
-
- CSV.foreach(market_file) do |row|
- next if row.first == 'Symbol'
- symbols.push(row.first.gsub(' ', ''))
- end
+ if [:nyse, :nasdaq, :amex].include? market.to_sym
- end
- end
-
- %w(nasdaq nyse amex).each do |market_symbol|
- define_method "#{market_symbol}_url".to_sym do
- "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=#{market_symbol}&render=download"
- end
- end
-
- %w(nnm nyse).each do |market_symbol|
- define_method "active_#{market_symbol}_url".to_sym do
- "http://online.wsj.com/mdc/public/page/2_3021-activ#{market_symbol}-actives.html"
- end
- end
-
- def search_endpoint(key)
- "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=#{key}&callback=YAHOO.Finance.SymbolSuggest.ssCallback"
- end
-
def symbols_by_market(market)
- symbols = []
- return symbols if market.nil?
- if respond_to?("#{market}_url".to_sym)
- url = send("#{market}_url".to_sym)
- market_file = open(url)
-
- CSV.foreach(market_file) do |row|
- next if row.first == 'Symbol'
- symbols.push(row.first.gsub(' ', ''))
- end
+ if [:nyse, :nasdaq, :amex].include? market.to_sym
+ YahooFinanza::Constants.send(market.to_sym)
+ else
+ raise YahooFinanza::MarketDoesntExistError
end
- symbols
end
- def sp_symbols(limit)
- response = JSON.parse(RestClient.get("http://46.101.6.197/standard_poor/#{limit}"))
- if response['error']
- Array.new
- else
- response['result']
- end
+ def sp_symbols
+ YahooFinanza::Constants.sp_500
end
def search_symbols(key = '')
- response = RestClient.get search_endpoint(key)
- result = response.gsub(/YAHOO.Finance.SymbolSuggest.ssCallback\(|\)/, '')
- json = JSON.parse(result)
- json['ResultSet']['Result'].map{|quote| quote['symbol']}
+ params = { callback: "YAHOO.Finance.SymbolSuggest.ssCallback", query: key }
+ response = RestClient.get("#{YahooFinanza::Constants.search_url}" +
+ "#{hash_to_query(params)}")
+ YahooFinanza::Parser.parse(response)
end
def active_symbols
- mecha_yahoo = Mechanize.new
- symbols = Array.new
- page1 = mecha_yahoo.get(active_nnm_url)
- page1.links.each do |link|
- if link.text.split(" (").count > 1
- symbols.push(link.text.split(" (")[1].gsub(')', '').strip)
- end
- break unless symbols.count < 8
- end
- page2 = mecha_yahoo.get(active_nyse_url)
- page2.links.each do |link|
- if link.text.split(" (").count > 1
- symbols.push(link.text.split(" (")[1].gsub(')', '').strip)
- end
- break unless symbols.count < 16
- end
- symbols
+ YahooFinanza::Worker.new.get_active_stocks
+ end
+
+ def hash_to_query(hash)
+ URI.encode(hash.map{|k,v| "#{k}=#{v}"}.join("&"))
end
end
end
Last active
September 28, 2015 21:38
-
-
Save ybur-yug/b2f654b7639576b657f8 to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment