Class: GoogleCSE::Query
- Inherits:
-
Object
- Object
- GoogleCSE::Query
- Defined in:
- lib/google_cse/query.rb
Constant Summary collapse
- @@endpoint =
'https://www.googleapis.com/customsearch/v1'
Instance Attribute Summary collapse
-
#current_index ⇒ Object
Returns the value of attribute current_index.
-
#info ⇒ Object
Returns the value of attribute info.
-
#params ⇒ Object
Returns the value of attribute params.
-
#per_page ⇒ Object
Returns the value of attribute per_page.
-
#response ⇒ Object
Returns the value of attribute response.
-
#results ⇒ Object
Returns the value of attribute results.
-
#time ⇒ Object
Returns the value of attribute time.
-
#total ⇒ Object
Returns the value of attribute total.
Instance Method Summary collapse
- #fetch ⇒ Object
- #fetch! ⇒ Object
-
#initialize(opts = {}) ⇒ Query
constructor
A new instance of Query.
- #next ⇒ Object
- #next? ⇒ Boolean
- #page ⇒ Object
- #parse_response! ⇒ Object
- #previous ⇒ Object
- #previous? ⇒ Boolean
- #search_uri ⇒ Object
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_index ⇒ Object
Returns the value of attribute current_index.
8 9 10 |
# File 'lib/google_cse/query.rb', line 8 def current_index @current_index end |
#info ⇒ Object
Returns the value of attribute info.
8 9 10 |
# File 'lib/google_cse/query.rb', line 8 def info @info end |
#params ⇒ Object
Returns the value of attribute params.
8 9 10 |
# File 'lib/google_cse/query.rb', line 8 def params @params end |
#per_page ⇒ Object
Returns the value of attribute per_page.
8 9 10 |
# File 'lib/google_cse/query.rb', line 8 def per_page @per_page end |
#response ⇒ Object
Returns the value of attribute response.
8 9 10 |
# File 'lib/google_cse/query.rb', line 8 def response @response end |
#results ⇒ Object
Returns the value of attribute results.
8 9 10 |
# File 'lib/google_cse/query.rb', line 8 def results @results end |
#time ⇒ Object
Returns the value of attribute time.
8 9 10 |
# File 'lib/google_cse/query.rb', line 8 def time @time end |
#total ⇒ Object
Returns the value of attribute total.
8 9 10 |
# File 'lib/google_cse/query.rb', line 8 def total @total end |
Instance Method Details
#fetch ⇒ Object
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 |
#next ⇒ Object
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
43 44 45 |
# File 'lib/google_cse/query.rb', line 43 def next? !(@next_page_params.nil? || @next_page_params.empty?) end |
#page ⇒ Object
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 |
#previous ⇒ Object
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
47 48 49 |
# File 'lib/google_cse/query.rb', line 47 def previous? !(@prev_page_params.nil? || @prev_page_params.empty?) end |
#search_uri ⇒ Object
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 |