Class: YahooStock::Interface
- Inherits:
-
Object
- Object
- YahooStock::Interface
- Defined in:
- lib/yahoo_stock/interface.rb
Overview
DESCRIPTION:
Class to generate the right url and interface with yahoo
Defined Under Namespace
Classes: InterfaceError
Constant Summary collapse
- PARAMETERS =
{ :ask => 'a', :average_daily_volume => 'a2', :ask_size => 'a5', :bid => 'b', :ask_real_time => 'b2', :bid_real_time => 'b3', :book_value => 'b4', :bid_size => 'b4', :change_with_percent_change => 'c', :change => 'c1', :commission => 'c3', :change_real_time => 'c6', :after_hours_change_real_time => 'c8', :dividend_per_share => 'd', :last_trade_date => 'd1', :trade_date => 'd2', :earnings_per_share => 'e', :error_indication => 'e1', :eps_estimate_current_year => 'e7', :eps_estimate_next_year => 'e8', :eps_estimate_next_quarter => 'e9', :day_low => 'g', :day_high => 'h', :fifty_two_week_low => 'j', :fifty_two_week_high => 'k', :holdings_gain_percent => 'g1', :annualized_gain => 'g3', :holdings_gain => 'g4', :holdings_gain_percent_real_time => 'g5', :holdings_gain_real_time => 'g6', :more_info => 'i', :order_book_real_time => 'i5', :market_capitalization => 'j1', :market_cap_real_time => 'j3', :ebitda => 'j4', :change_from_52_week_low => 'j5', :percent_change_from_52_week_low => 'j6', :last_trade_real_time_with_time => 'k1', :change_percent_real_time => 'k2', :change_from_52_week_high => 'k4', :percent_change_from_52_week_high => 'k5', :last_trade_with_time => 'l', :last_trade_price_only => 'l1', :high_limit => 'l2', :low_limit => 'l3', :day_range => 'm', :day_range_real_time => 'm2', :fifty_day_moving_average => 'm3', :two_hundred_day_moving_average => 'm4', :change_from_200_day_moving_average => 'm5', :percent_change_from_200_day_moving_average => 'm6', :change_from_50_day_moving_average => 'm7', :percent_change_from_50_day_moving_average => 'm8', :name => 'n', :notes => 'n4', :open => 'o', :previous_close => 'p', :price_paid => 'p1', :change_in_percent => 'p2', :ex_dividend_date => 'q', :p_e_ratio => 'r', :dividend_pay_date => 'r1', :p_e_ratio_real_time => 'r2', :peg_ratio => 'r5', :price_eps_estimate_current_year => 'r6', :price_eps_estimate_next_year => 'r7', :symbol => 's', :shares_owned => 's1', :short_ratio => 's7', :last_trade_time => 't1', :trade_links => 't6', :ticker_trend => 't7', :one_yr_target_price => 't8', :volume => 'v', :holdings_value => 'v1', :holdings_value_real_time => 'v7', :fifty_two_week_range => 'w', :day_value_change => 'w1', :day_value_change_real_time => 'w4', :stock_exchange => 'x', :dividend_yield => 'y', }
Instance Attribute Summary collapse
-
#stock_symbols ⇒ Object
Returns the value of attribute stock_symbols.
-
#yahoo_url_parameters ⇒ Object
Returns the value of attribute yahoo_url_parameters.
Instance Method Summary collapse
-
#add_parameters(*parameters) ⇒ Object
Add parameters to the url.
-
#add_symbols(*symbols) ⇒ Object
Add stock symbols to the url.
-
#allowed_parameters ⇒ Object
Returns an array of parameters that can be passed to yahoo.
-
#full_url ⇒ Object
Generate full url to be sent to yahoo.
-
#get_values ⇒ Object
Get the csv file and create an array of different stock symbols and values returned for the parameters passed based on line break.
-
#initialize(stock_params_hash) ⇒ Interface
constructor
The stock_params_hash parameter expects a hash with two key value pairs.
-
#remove_parameters(*parameters) ⇒ Object
Remove parameters from the url.
-
#remove_symbols(*symbols) ⇒ Object
Remove stock symbols from the url.
-
#results ⇒ Object
Returns results for the stock symbols as a hash.
Constructor Details
#initialize(stock_params_hash) ⇒ Interface
The stock_params_hash parameter expects a hash with two key value pairs
:stock_symbols => ‘Array of stock symbols’
e.g. :stock_symbols => [‘YHOO’]
another hash :read_parameters => ‘array of values’
e.g. :read_parameters => [:last_trade_price_only, :last_trade_date]
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/yahoo_stock/interface.rb', line 114 def initialize(stock_params_hash) unless stock_params_hash raise InterfaceError, 'You must pass a hash of stock symbols and the data you would like to see' end if !stock_params_hash[:stock_symbols] || stock_params_hash[:stock_symbols].length.zero? raise(InterfaceError, 'No stocks passed') end if !stock_params_hash[:read_parameters] || stock_params_hash[:read_parameters].length.zero? raise InterfaceError, 'Dont know what data to get' end @stock_symbols = stock_params_hash[:stock_symbols] @yahoo_url_parameters = stock_params_hash[:read_parameters] @base_url = "http://download.finance.yahoo.com/d/quotes.csv" end |
Instance Attribute Details
#stock_symbols ⇒ Object
Returns the value of attribute stock_symbols.
103 104 105 |
# File 'lib/yahoo_stock/interface.rb', line 103 def stock_symbols @stock_symbols end |
#yahoo_url_parameters ⇒ Object
Returns the value of attribute yahoo_url_parameters.
103 104 105 |
# File 'lib/yahoo_stock/interface.rb', line 103 def yahoo_url_parameters @yahoo_url_parameters end |
Instance Method Details
#add_parameters(*parameters) ⇒ Object
Add parameters to the url.
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/yahoo_stock/interface.rb', line 187 def add_parameters(*parameters) parameters.each do |parameter| unless allowed_parameters.include?(parameter) raise InterfaceError, "Interface parameter #{parameter} is not a valid parameter." end unless yahoo_url_parameters.include?(parameter) yahoo_url_parameters << parameter end end end |
#add_symbols(*symbols) ⇒ Object
Add stock symbols to the url.
168 169 170 171 172 173 174 |
# File 'lib/yahoo_stock/interface.rb', line 168 def add_symbols(*symbols) symbols.each do |symbol| unless stock_symbols.include?(symbol) stock_symbols << symbol end end end |
#allowed_parameters ⇒ Object
Returns an array of parameters that can be passed to yahoo.
209 210 211 |
# File 'lib/yahoo_stock/interface.rb', line 209 def allowed_parameters parameters.keys end |
#full_url ⇒ Object
Generate full url to be sent to yahoo
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/yahoo_stock/interface.rb', line 130 def full_url all_stock_symbols = stock_symbols.join('+') params = yahoo_url_parameters-allowed_parameters unless params.length.zero? raise InterfaceError, "The parameters '#{params.join(', ')}' are not valid. Please check using YahooStock::Interface#allowed_parameters or YahooStock::Quote#valid_parameters" end parameter_values = yahoo_url_parameters.collect {|v| parameters[v]}.join('') if all_stock_symbols.empty? raise InterfaceError, "You must add atleast one stock symbol to get stock data" end if parameter_values.empty? raise InterfaceError, "You must add atleast one parameter to get stock data" end "#{@base_url}?s=#{all_stock_symbols}&f=#{parameter_values}" end |
#get_values ⇒ Object
Get the csv file and create an array of different stock symbols and values returned for the parameters passed based on line break.
148 149 150 |
# File 'lib/yahoo_stock/interface.rb', line 148 def get_values Net::HTTP.get(URI.parse(full_url)).gsub(/\"/,'').split(/\r\n/) end |
#remove_parameters(*parameters) ⇒ Object
Remove parameters from the url.
199 200 201 202 203 204 205 206 |
# File 'lib/yahoo_stock/interface.rb', line 199 def remove_parameters(*parameters) parameters.each do |parameter| unless yahoo_url_parameters.include?(parameter) raise InterfaceError, "Parameter #{parameter} is not present in current list" end yahoo_url_parameters.reject!{|parameter_key| parameter_key == parameter} end end |
#remove_symbols(*symbols) ⇒ Object
Remove stock symbols from the url.
177 178 179 180 181 182 183 184 |
# File 'lib/yahoo_stock/interface.rb', line 177 def remove_symbols(*symbols) symbols.each do |symbol| unless stock_symbols.include?(symbol) raise InterfaceError, "Cannot remove stock symbol #{symbol} as it is currently not present." end stock_symbols.reject!{|stock_sym| stock_sym == symbol} end end |
#results ⇒ Object
Returns results for the stock symbols as a hash. The hash keys are the stock symbols and the values are a hash of the keys and values asked for that stock.
155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/yahoo_stock/interface.rb', line 155 def results stock = {} get_values.each_with_index do |values, index| parsed_values = values.split(',') stock[stock_symbols[index]] ||= {} parsed_values.each_with_index do |value, i| stock[stock_symbols[index]][yahoo_url_parameters[i]] = value end end stock end |