Class: GoogleCSE::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/google_cse/query.rb

Constant Summary collapse

@@endpoint =
'https://www.googleapis.com/customsearch/v1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Query

Returns a new instance of Query.



10
11
12
# File 'lib/google_cse/query.rb', line 10

def initialize opts = {}
  opts.map {|k,v| send(:"#{k}=",v)}
end

Instance Attribute Details

#current_indexObject

Returns the value of attribute current_index.



8
9
10
# File 'lib/google_cse/query.rb', line 8

def current_index
  @current_index
end

#infoObject

Returns the value of attribute info.



8
9
10
# File 'lib/google_cse/query.rb', line 8

def info
  @info
end

#paramsObject

Returns the value of attribute params.



8
9
10
# File 'lib/google_cse/query.rb', line 8

def params
  @params
end

#per_pageObject

Returns the value of attribute per_page.



8
9
10
# File 'lib/google_cse/query.rb', line 8

def per_page
  @per_page
end

#responseObject

Returns the value of attribute response.



8
9
10
# File 'lib/google_cse/query.rb', line 8

def response
  @response
end

#resultsObject

Returns the value of attribute results.



8
9
10
# File 'lib/google_cse/query.rb', line 8

def results
  @results
end

#timeObject

Returns the value of attribute time.



8
9
10
# File 'lib/google_cse/query.rb', line 8

def time
  @time
end

#totalObject

Returns the value of attribute total.



8
9
10
# File 'lib/google_cse/query.rb', line 8

def total
  @total
end

Instance Method Details

#fetchObject



18
19
20
21
22
# File 'lib/google_cse/query.rb', line 18

def fetch
  @response = JSON.parse(open(search_uri).read)
  parse_response!
  self
end

#fetch!Object



24
25
26
27
# File 'lib/google_cse/query.rb', line 24

def fetch!
  fetch
  nil
end

#nextObject



29
30
31
32
33
34
# File 'lib/google_cse/query.rb', line 29

def next
  if next?
    @params.merge!({:start => @next_page_params.first['startIndex']})
    fetch        
  end
end

#next?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/google_cse/query.rb', line 43

def next?
  !(@next_page_params.nil? || @next_page_params.empty?)
end

#pageObject



51
52
53
# File 'lib/google_cse/query.rb', line 51

def page
  (current_index / per_page.to_f).ceil
end

#parse_response!Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/google_cse/query.rb', line 55

def parse_response!
  @results = @response['items'].map { |i| Result.new(i) }
  @next_page_params = @response['queries']['nextPage']
  @prev_page_params = @response['queries']['previousPage']
  @info = @response['searchInformation']
  @total = @info['totalResults'].to_i
  @time = @info['searchTime'].to_f
  @per_page = @response['queries']['request'].first['count'].to_i
  @current_index = @response['queries']['request'].first['startIndex'].to_i
  nil
end

#previousObject



36
37
38
39
40
41
# File 'lib/google_cse/query.rb', line 36

def previous
  if previous?
    @params.merge!({:start => @prev_page_params.first['startIndex']})
    fetch
  end
end

#previous?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/google_cse/query.rb', line 47

def previous?
  !(@prev_page_params.nil? || @prev_page_params.empty?)
end

#search_uriObject



14
15
16
# File 'lib/google_cse/query.rb', line 14

def search_uri
  "#{@@endpoint}?#{params.map {|k,v|"#{k}=#{CGI.escape(v.to_s)}"}.join('&')}"
end