Class: Google::Search::Response
- Inherits:
-
Object
- Object
- Google::Search::Response
- Includes:
- Enumerable
- Defined in:
- lib/google-search/response.rb
Instance Attribute Summary collapse
-
#details ⇒ Object
readonly
Response details.
-
#estimated_count ⇒ Object
readonly
Estimated number of results.
-
#hash ⇒ Object
readonly
Hash parsed from raw JSON string.
-
#items ⇒ Object
readonly
Items populated by the JSON hash.
-
#page ⇒ Object
readonly
Current page index.
-
#raw ⇒ Object
Raw JSON string.
-
#size ⇒ Object
readonly
Size of response.
-
#status ⇒ Object
readonly
Response status code.
Instance Method Summary collapse
-
#each_item(&block) ⇒ Object
(also: #each)
Iterate each item with block.
-
#initialize(hash) ⇒ Response
constructor
Initialize with hash.
-
#valid? ⇒ Boolean
Check if the response is valid.
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
#details ⇒ Object (readonly)
Response details.
20 21 22 |
# File 'lib/google-search/response.rb', line 20 def details @details end |
#estimated_count ⇒ Object (readonly)
Estimated number of results.
40 41 42 |
# File 'lib/google-search/response.rb', line 40 def estimated_count @estimated_count end |
#hash ⇒ Object (readonly)
Hash parsed from raw JSON string.
30 31 32 |
# File 'lib/google-search/response.rb', line 30 def hash @hash end |
#items ⇒ Object (readonly)
Items populated by the JSON hash.
35 36 37 |
# File 'lib/google-search/response.rb', line 35 def items @items end |
#page ⇒ Object (readonly)
Current page index.
45 46 47 |
# File 'lib/google-search/response.rb', line 45 def page @page end |
#raw ⇒ Object
Raw JSON string.
25 26 27 |
# File 'lib/google-search/response.rb', line 25 def raw @raw end |
#size ⇒ Object (readonly)
Size of response.
50 51 52 |
# File 'lib/google-search/response.rb', line 50 def size @size end |
#status ⇒ Object (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.
86 87 88 |
# File 'lib/google-search/response.rb', line 86 def valid? hash['responseStatus'] == 200 end |