class Gugl::Search::RemoteSearcher
Constants
- ERROR_RESPONSE_NODE
- ITEMS_RESPONSE_NODE
Attributes
credentials[RW]
Public Instance Methods
search(query, limit) { |results| ... }
click to toggle source
# File lib/search/remote_searcher.rb, line 13 def search(query, limit) data = fetch_and_parse(query, limit) if results = data[ITEMS_RESPONSE_NODE] yield results else raise "Error: #{data[ERROR_RESPONSE_NODE]}" end end
Private Instance Methods
base_url()
click to toggle source
# File lib/search/remote_searcher.rb, line 41 def base_url "https://www.googleapis.com/customsearch/v1" end
fetch(query, limit)
click to toggle source
# File lib/search/remote_searcher.rb, line 34 def fetch(query, limit) HTTParty.get(base_url, query: { num: limit, q: query }.merge(credentials.to_h)) end
fetch_and_parse(query, limit)
click to toggle source
# File lib/search/remote_searcher.rb, line 29 def fetch_and_parse(query, limit) response = fetch(query, limit) parse_response(response) end
parse_response(response)
click to toggle source
# File lib/search/remote_searcher.rb, line 45 def parse_response(response) JSON.parse(response.body) end