Class: SearchResponse
- Inherits:
-
Object
- Object
- SearchResponse
- Defined in:
- lib/googlesearch/search_response.rb
Instance Attribute Summary collapse
-
#index_of_first_result ⇒ Object
readonly
Returns the value of attribute index_of_first_result.
-
#index_of_last_result ⇒ Object
readonly
Returns the value of attribute index_of_last_result.
-
#requested_number_of_search_results ⇒ Object
readonly
Returns the value of attribute requested_number_of_search_results.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
-
#start_index ⇒ Object
readonly
Returns the value of attribute start_index.
-
#total_number_of_results ⇒ Object
readonly
Returns the value of attribute total_number_of_results.
-
#total_server_time ⇒ Object
readonly
Returns the value of attribute total_server_time.
Instance Method Summary collapse
- #current_page ⇒ Object
-
#initialize(xml, requested_number_of_search_results = nil, start_index = nil) ⇒ SearchResponse
constructor
A new instance of SearchResponse.
- #next_page ⇒ Object
- #number_of_pages ⇒ Object
- #pages ⇒ Object
- #previous_page ⇒ Object
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_result ⇒ Object (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_result ⇒ Object (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_results ⇒ Object (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 |
#results ⇒ Object (readonly)
Returns the value of attribute results.
2 3 4 |
# File 'lib/googlesearch/search_response.rb', line 2 def results @results end |
#start_index ⇒ Object (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_results ⇒ Object (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_time ⇒ Object (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_page ⇒ Object
24 25 26 |
# File 'lib/googlesearch/search_response.rb', line 24 def current_page pages.find { |p| p.current_page? } end |
#next_page ⇒ Object
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_pages ⇒ Object
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 |
#pages ⇒ Object
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_page ⇒ Object
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 |