Class: RubyWebSearch::Google::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-web-search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(google_raw_response = {}) ⇒ Response

Returns a new instance of Response.



164
165
166
# File 'lib/ruby-web-search.rb', line 164

def initialize(google_raw_response={})
  process(google_raw_response) unless google_raw_response.empty?
end

Instance Attribute Details

#estimated_result_countObject (readonly)

Returns the value of attribute estimated_result_count.



163
164
165
# File 'lib/ruby-web-search.rb', line 163

def estimated_result_count
  @estimated_result_count
end

#queryObject (readonly)

Returns the value of attribute query.



163
164
165
# File 'lib/ruby-web-search.rb', line 163

def query
  @query
end

#resultsObject (readonly)

Returns the value of attribute results.



163
164
165
# File 'lib/ruby-web-search.rb', line 163

def results
  @results
end

#sizeObject (readonly)

Returns the value of attribute size.



163
164
165
# File 'lib/ruby-web-search.rb', line 163

def size
  @size
end

#statusObject (readonly)

Returns the value of attribute status.



163
164
165
# File 'lib/ruby-web-search.rb', line 163

def status
  @status
end

Instance Method Details

#limit(req_size) ⇒ Object



186
187
188
189
# File 'lib/ruby-web-search.rb', line 186

def limit(req_size)
  @results = @results[0...req_size]
  self
end

#process(google_raw_response = {}) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/ruby-web-search.rb', line 168

def process(google_raw_response={})
  @query    ||= google_raw_response[:query]
  @size     ||= google_raw_response[:size]
  @results  ||= []
  @status     = google_raw_response["responseStatus"]
  if google_raw_response["responseData"] && status && status == 200
    estimated_result_count ||= google_raw_response["cursor"]["estimatedResultCount"] if google_raw_response["cursor"]
    @results  +=  google_raw_response["responseData"]["results"].map do |r| 
                  { 
                    :title      => r["titleNoFormatting"], 
                    :url        => r["unescapedUrl"],
                    :cache_url  => r["cacheUrl"],
                    :content    => r["content"],
                    :domain     => r["visibleUrl"]
                  }
                end
  end
  
  def limit(req_size)
    @results = @results[0...req_size]
    self
  end
  
end