Class: Google::Search::Response

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/google-search/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Response

Initialize with hash.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/google-search/response.rb', line 55

def initialize hash
  @page = 0
  @hash = hash
  @size = (hash['responseSize'] || :large).to_sym
  @items = []
  @status = hash['responseStatus']
  @details = hash['responseDetails']
  if valid?
    if hash['responseData'].include? 'cursor'
      @estimated_count = hash['responseData']['cursor']['estimatedResultCount'].to_i
      @page = hash['responseData']['cursor']['currentPageIndex'].to_i
    end
    @hash['responseData']['results'].each_with_index do |result, i|
      item_class = Google::Search::Item.class_for result['GsearchResultClass']
      result['index'] = i + Google::Search.size_for(size) * page
      items << item_class.new(result)
    end
  end
end

Instance Attribute Details

#detailsObject (readonly)

Response details.



20
21
22
# File 'lib/google-search/response.rb', line 20

def details
  @details
end

#estimated_countObject (readonly)

Estimated number of results.



40
41
42
# File 'lib/google-search/response.rb', line 40

def estimated_count
  @estimated_count
end

#hashObject (readonly)

Hash parsed from raw JSON string.



30
31
32
# File 'lib/google-search/response.rb', line 30

def hash
  @hash
end

#itemsObject (readonly)

Items populated by the JSON hash.



35
36
37
# File 'lib/google-search/response.rb', line 35

def items
  @items
end

#pageObject (readonly)

Current page index.



45
46
47
# File 'lib/google-search/response.rb', line 45

def page
  @page
end

#rawObject

Raw JSON string.



25
26
27
# File 'lib/google-search/response.rb', line 25

def raw
  @raw
end

#sizeObject (readonly)

Size of response.



50
51
52
# File 'lib/google-search/response.rb', line 50

def size
  @size
end

#statusObject (readonly)

Response status code.



15
16
17
# File 'lib/google-search/response.rb', line 15

def status
  @status
end

Instance Method Details

#each_item(&block) ⇒ Object Also known as: each

Iterate each item with block.



78
79
80
# File 'lib/google-search/response.rb', line 78

def each_item &block
  items.each { |item| yield item }
end

#valid?Boolean

Check if the response is valid.

Returns:

  • (Boolean)


86
87
88
# File 'lib/google-search/response.rb', line 86

def valid?
  hash['responseStatus'] == 200
end