Class: Ytterb::Yql::Response

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

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Response

Returns a new instance of Response.



8
9
10
11
12
13
14
15
# File 'lib/ytterb/yql/response.rb', line 8

def initialize(raw)
  begin
    @response = JSON.parse(raw)
  rescue
    raise InvalidYqlResponse, "failed parsing yql response"
  end
  build_response_lines
end

Instance Method Details

#build_response_linesObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ytterb/yql/response.rb', line 25

def build_response_lines
  @response_lines = []
  return unless @response.has_key?("query")
  return unless @response["query"].has_key?("count")
  return unless @response["query"]["count"].to_i > 0
  return unless @response["query"].has_key?("results")
  return unless @response["query"]["results"].has_key?("quote")
  if @response["query"]["results"]["quote"].kind_of?(Hash)
    @response_lines << validate_response_line(@response["query"]["results"]["quote"])
  elsif @response["query"]["results"]["quote"].kind_of?(Array)
    @response["query"]["results"]["quote"].each do |line|
      @response_lines << validate_response_line(line)
    end
  else
    raise InvalidYqlResponse, "response/query/results/quote needs to be an Array or a Hash. Found a #{@response["query"]["results"]["quote"].class}"
  end
end

#linesObject



43
44
45
# File 'lib/ytterb/yql/response.rb', line 43

def lines
  @response_lines
end

#validate_response_line(line) ⇒ Object

Raises:



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

def validate_response_line(line)
  raise InvalidYqlResponse, "response line needs to be a hash. found a #{line.class}" unless line.kind_of?(Hash)
  line.select! do |k,v|
    ["Symbol", "Date", "Open", "High", "Low", "Close", "Volume", "Adj_Close"].include?(k)
  end
  line
end