Class: Oldskool::GCSE
Instance Attribute Summary collapse
-
#querytime ⇒ Object
readonly
Returns the value of attribute querytime.
Instance Method Summary collapse
- #has_next? ⇒ Boolean
- #has_previous? ⇒ Boolean
-
#initialize(apikey, cx, apiurl = 'https://www.googleapis.com/customsearch/v1') ⇒ GCSE
constructor
A new instance of GCSE.
-
#items ⇒ Object
Array of search results.
-
#next ⇒ Object
do a search for the next results.
- #next_start ⇒ Object
-
#previous ⇒ Object
do search for the previous results.
- #previous_start ⇒ Object
- #search(q, args = {}) ⇒ Object
- #search_by_query_hash(query) ⇒ Object
-
#title ⇒ Object
Custom Search Engine title.
- #total ⇒ Object
-
#total_results ⇒ Object
number of total results found in the search.
Constructor Details
#initialize(apikey, cx, apiurl = 'https://www.googleapis.com/customsearch/v1') ⇒ GCSE
Returns a new instance of GCSE.
7 8 9 10 11 12 13 14 |
# File 'lib/oldskool/gcse.rb', line 7 def initialize(apikey, cx, apiurl='https://www.googleapis.com/customsearch/v1') @cx = cx @apikey = apikey @api = apiurl @lastresult = nil @querytime = 0 @error = nil end |
Instance Attribute Details
#querytime ⇒ Object (readonly)
Returns the value of attribute querytime.
3 4 5 |
# File 'lib/oldskool/gcse.rb', line 3 def querytime @querytime end |
Instance Method Details
#has_next? ⇒ Boolean
56 57 58 |
# File 'lib/oldskool/gcse.rb', line 56 def has_next? !!@lastresult["queries"]["nextPage"] end |
#has_previous? ⇒ Boolean
52 53 54 |
# File 'lib/oldskool/gcse.rb', line 52 def has_previous? !!@lastresult["queries"]["previousPage"] end |
#items ⇒ Object
Array of search results
34 35 36 |
# File 'lib/oldskool/gcse.rb', line 34 def items @lastresult["items"] end |
#next ⇒ Object
do a search for the next results
82 83 84 85 86 87 88 89 90 |
# File 'lib/oldskool/gcse.rb', line 82 def next if (r = @lastresult["queries"]["nextPage"]) @previous = @lastresult.clone return search_by_query_hash(r.first) else return nil end end |
#next_start ⇒ Object
60 61 62 63 64 |
# File 'lib/oldskool/gcse.rb', line 60 def next_start if has_next? return @lastresult["queries"]["nextPage"].first["startIndex"] end end |
#previous ⇒ Object
do search for the previous results
73 74 75 76 77 78 79 |
# File 'lib/oldskool/gcse.rb', line 73 def previous if (r = @lastresult["queries"]["previousPage"]) return search_by_query_hash(r.first) else return nil end end |
#previous_start ⇒ Object
66 67 68 69 70 |
# File 'lib/oldskool/gcse.rb', line 66 def previous_start if has_previous? return @lastresult["queries"]["previousPage"].first["startIndex"] end end |
#search(q, args = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/oldskool/gcse.rb', line 16 def search(q, args={}) args[:start] = (args.delete(:page) * 10 + 1) if args[:page] start = Time.now @lastresult = self.class.get(@api, :query => {:q => q, :key => @apikey, :cx => @cx}.merge(args)) @querytime = Time.now - start if @lastresult["error"] @error = @lastresult["error"]["message"] else @error = nil end self end |
#search_by_query_hash(query) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/oldskool/gcse.rb', line 92 def search_by_query_hash(query) if query["startIndex"] return search(query["searchTerms"], {:start => query["startIndex"]}) else return search(query["searchTerms"]) end end |
#title ⇒ Object
Custom Search Engine title
43 44 45 |
# File 'lib/oldskool/gcse.rb', line 43 def title @lastresult["context"]["title"] end |
#total ⇒ Object
38 39 40 |
# File 'lib/oldskool/gcse.rb', line 38 def total Integer(@lastresult["queries"]["request"].first["totalResults"]) end |
#total_results ⇒ Object
number of total results found in the search
48 49 50 |
# File 'lib/oldskool/gcse.rb', line 48 def total_results @lastresults["request"]["totalResults"] end |