Class: Oldskool::GCSE

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/oldskool/gcse.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#querytimeObject (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

Returns:

  • (Boolean)


56
57
58
# File 'lib/oldskool/gcse.rb', line 56

def has_next?
  !!@lastresult["queries"]["nextPage"]
end

#has_previous?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/oldskool/gcse.rb', line 52

def has_previous?
  !!@lastresult["queries"]["previousPage"]
end

#itemsObject

Array of search results



34
35
36
# File 'lib/oldskool/gcse.rb', line 34

def items
  @lastresult["items"]
end

#nextObject

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_startObject



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

#previousObject

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_startObject



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

#titleObject

Custom Search Engine title



43
44
45
# File 'lib/oldskool/gcse.rb', line 43

def title
  @lastresult["context"]["title"]
end

#totalObject



38
39
40
# File 'lib/oldskool/gcse.rb', line 38

def total
  Integer(@lastresult["queries"]["request"].first["totalResults"])
end

#total_resultsObject

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