Class: Ytterb::YQLClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ytterb/yql_client.rb

Instance Method Summary collapse

Constructor Details

#initializeYQLClient

Returns a new instance of YQLClient.



9
10
11
12
# File 'lib/ytterb/yql_client.rb', line 9

def initialize
  @endpoint = "http://query.yahooapis.com/v1/public/yql"
  puts "YQL Client is using endpoint @ #{@endpoint}"
end

Instance Method Details

#get_symbol_current(my_symbol) ⇒ Object



25
26
27
# File 'lib/ytterb/yql_client.rb', line 25

def get_symbol_current(my_symbol)
  run_select_query("select * from yahoo.finance.quotes where symbol in (\"#{my_symbol}\")")["query"]["results"]
end

#get_symbol_historical(my_symbol, start_date = nil, end_date = nil) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/ytterb/yql_client.rb', line 29

def get_symbol_historical(my_symbol, start_date = nil, end_date = nil)
  requested_end = Date.parse(end_date) if end_date
  requested_end = Date.today unless requested_end

  requested_start = Date.parse(start_date) if start_date
  requested_start = requested_end - 30 unless requested_start
  run_select_query("select * from yahoo.finance.historicaldata where symbol in (\"#{my_symbol}\") and startDate='#{requested_start}' and endDate='#{requested_end}'")["query"]["results"]
end

#get_yql_endpointObject



14
15
16
# File 'lib/ytterb/yql_client.rb', line 14

def get_yql_endpoint
  @endpoint
end

#run_select_query(query) ⇒ Object



18
19
20
21
22
23
# File 'lib/ytterb/yql_client.rb', line 18

def run_select_query(query)
  RestClient.get("#{get_yql_endpoint}?q=#{CGI::escape(query)}&format=json&env=store://datatables.org/alltableswithkeys") do |response, request, result|
    raise "Failed performing YQLQuery\n Request: #{request.inspect}\n Response #{response.inspect}" unless response.code == 200
    return JSON.parse(response)
  end
end