Class: SearchResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/googlesearch/search_response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml, requested_number_of_search_results = nil, start_index = nil) ⇒ SearchResponse

Returns a new instance of SearchResponse.



5
6
7
8
9
10
11
12
13
14
# File 'lib/googlesearch/search_response.rb', line 5

def initialize(xml, requested_number_of_search_results=nil, start_index=nil)
  doc = Nokogiri::XML(xml)
  @total_server_time = doc.root.xpath('TM').text.to_f 
  @index_of_first_result = doc.root.xpath('RES/@SN').text.to_i
  @index_of_last_result = doc.root.xpath('RES/@EN').text.to_i
  @total_number_of_results = doc.root.xpath('RES/M').text.to_i
  @results = doc.root.xpath('RES//R').map { |res_doc| SearchResult.new(res_doc) }
  @requested_number_of_search_results = requested_number_of_search_results ||= 10
  @start_index = start_index ||= 0
end

Instance Attribute Details

#index_of_first_resultObject (readonly)

Returns the value of attribute index_of_first_result.



2
3
4
# File 'lib/googlesearch/search_response.rb', line 2

def index_of_first_result
  @index_of_first_result
end

#index_of_last_resultObject (readonly)

Returns the value of attribute index_of_last_result.



2
3
4
# File 'lib/googlesearch/search_response.rb', line 2

def index_of_last_result
  @index_of_last_result
end

#requested_number_of_search_resultsObject (readonly)

Returns the value of attribute requested_number_of_search_results.



2
3
4
# File 'lib/googlesearch/search_response.rb', line 2

def requested_number_of_search_results
  @requested_number_of_search_results
end

#resultsObject (readonly)

Returns the value of attribute results.



2
3
4
# File 'lib/googlesearch/search_response.rb', line 2

def results
  @results
end

#start_indexObject (readonly)

Returns the value of attribute start_index.



2
3
4
# File 'lib/googlesearch/search_response.rb', line 2

def start_index
  @start_index
end

#total_number_of_resultsObject (readonly)

Returns the value of attribute total_number_of_results.



2
3
4
# File 'lib/googlesearch/search_response.rb', line 2

def total_number_of_results
  @total_number_of_results
end

#total_server_timeObject (readonly)

Returns the value of attribute total_server_time.



2
3
4
# File 'lib/googlesearch/search_response.rb', line 2

def total_server_time
  @total_server_time
end

Instance Method Details

#current_pageObject



24
25
26
# File 'lib/googlesearch/search_response.rb', line 24

def current_page
  pages.find { |p| p.current_page? }
end

#next_pageObject



32
33
34
35
# File 'lib/googlesearch/search_response.rb', line 32

def next_page
  return nil if pages.empty?
  pages[pages.index(current_page)+1]
end

#number_of_pagesObject



16
17
18
# File 'lib/googlesearch/search_response.rb', line 16

def number_of_pages
  (total_number_of_results/requested_number_of_search_results.to_f).round
end

#pagesObject



20
21
22
# File 'lib/googlesearch/search_response.rb', line 20

def pages
  @pages ||= number_of_pages.enum_for(:times).collect { |page_index| SearchPage.new(page_index, index_of_last_result, requested_number_of_search_results) }
end

#previous_pageObject



28
29
30
# File 'lib/googlesearch/search_response.rb', line 28

def previous_page
  start_index == 0 ? nil : pages[pages.index(current_page)-1]
end