class Yafa::StockQuotes

Constants

BATCHLIMIT_QUOTES
READ_TIMEOUT
YAHOO_API_END
YAHOO_API_QUERY
YAHOO_API_START

Public Class Methods

new(tickers) click to toggle source
# File lib/yafa/stock_quotes.rb, line 16
def initialize(tickers)
  @tickers = tickers
end

Public Instance Methods

fetch() click to toggle source
# File lib/yafa/stock_quotes.rb, line 20
def fetch
  formatted_tickers = format_tickers @tickers
  quote_data        = call_api(formatted_tickers)

  format_quote_data(quote_data)
end

Private Instance Methods

build_yql_query_body(yahoo_tickers) click to toggle source
# File lib/yafa/stock_quotes.rb, line 48
def build_yql_query_body(yahoo_tickers)
  url = YAHOO_API_QUERY.gsub('yahoo_tickers', yahoo_tickers)
  URI.encode(url)
end
call_api(yahoo_tickers) click to toggle source
# File lib/yafa/stock_quotes.rb, line 29
def call_api(yahoo_tickers)
  url      = format_api_url yahoo_tickers
  response = open(
    url,
    ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE, read_timeout: READ_TIMEOUT
  )

  response.read
end
format_api_url(yahoo_tickers) click to toggle source
# File lib/yafa/stock_quotes.rb, line 43
def format_api_url(yahoo_tickers)
  yql_query_body = build_yql_query_body(yahoo_tickers)
  [YAHOO_API_START, yql_query_body, YAHOO_API_END].join('')
end
format_quote_data(quote_data) click to toggle source
# File lib/yafa/stock_quotes.rb, line 53
def format_quote_data(quote_data)
  response_data = parse_quote_json(quote_data)
  wrap(response_data)
end
format_tickers(tickers) click to toggle source
# File lib/yafa/stock_quotes.rb, line 39
def format_tickers(tickers)
  tickers.map { |x| "'" + x + "'" }.join(', ')
end
parse_quote_json(message) click to toggle source
# File lib/yafa/stock_quotes.rb, line 58
def parse_quote_json(message)
  response_data = JSON.parse(message)
  response_data['query']['results']['quote']
end
wrap(obj) click to toggle source
# File lib/yafa/stock_quotes.rb, line 63
def wrap(obj)
  return obj if obj.class == Array
  [obj]
end