Class: YahooStock::Interface

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/yahoo_stock/interface.rb

Overview

DESCRIPTION:

Class to generate the right url and interface with yahoo

Direct Known Subclasses

History, Quote, ScripSymbol

Defined Under Namespace

Classes: History, InterfaceError, Quote, ScripSymbol

Constant Summary collapse

BASE_URLS =
{
  :quote        => "http://download.finance.yahoo.com/d/quotes.csv",
  :history      => "http://ichart.finance.yahoo.com/table.csv",
  :scrip_symbol => "http://finance.yahoo.com/lookup/all"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url


22
23
24
# File 'lib/yahoo_stock/interface.rb', line 22

def base_url
  @base_url
end

#uri_parametersObject

Returns the value of attribute uri_parameters


22
23
24
# File 'lib/yahoo_stock/interface.rb', line 22

def uri_parameters
  @uri_parameters
end

Instance Method Details

#getObject

Send request to the uri and get results


25
26
27
28
29
30
31
32
# File 'lib/yahoo_stock/interface.rb', line 25

def get
  begin
    response = Net::HTTP.get_response(URI.parse(uri))
  rescue => e
    raise InterfaceError, "#{e.message}\n\n#{e.backtrace}"
  end
  response.code == '200' ? response.body : response_error(response)
end

#updateObject


48
49
50
# File 'lib/yahoo_stock/interface.rb', line 48

def update
  @values = nil
end

#uriObject

Generates full url to connect to yahoo

Raises:


35
36
37
38
39
40
41
# File 'lib/yahoo_stock/interface.rb', line 35

def uri
  raise InterfaceError, 'Base url is require to generate full uri.' unless @base_url
  return @base_url if @uri_parameters.nil? || @uri_parameters.empty?
  params_with_values = []
  @uri_parameters.each {|k,v| params_with_values << "#{k}=#{v}"}
  URI.encode(@base_url+'?'+params_with_values.join('&'))
end

#valuesObject

Get result string


44
45
46
# File 'lib/yahoo_stock/interface.rb', line 44

def values
  @values ||= get
end