Class: DNZ::Search
- Inherits:
-
Object
- Object
- DNZ::Search
- Defined in:
- lib/dnz/search.rb
Overview
This class represents a digitalnz search API call. It should not be created directly. Instead use the Client.search
method.
Example
search = client.search('text')
puts "%d results found on %d pages" % [search.result_count, search.pages]
Instance Attribute Summary collapse
-
#result_count ⇒ Object
readonly
Returns the value of attribute result_count.
Instance Method Summary collapse
-
#facets ⇒ Object
An array of facets.
-
#initialize(client, search_options) ⇒ Search
constructor
Constructor for Search class.
-
#num_results_requested ⇒ Object
The number of results requested via the :num_results option (see
Client.search
). -
#options ⇒ Object
The search options passed to the digitalnz API.
-
#page ⇒ Object
The current page of results, based on the number of requested results and the start value (see
Client.search
). -
#page=(new_page) ⇒ Object
Set the page.
-
#pages ⇒ Object
The number of pages available for the current search.
-
#results ⇒ Object
An array of results.
-
#text ⇒ Object
The text used for searching.
- #to_s ⇒ Object
Constructor Details
#initialize(client, search_options) ⇒ Search
Constructor for Search class. Do not call this directly, instead use the Client.search
method.
22 23 24 25 26 27 |
# File 'lib/dnz/search.rb', line 22 def initialize(client, ) @client = client @search_options = execute end |
Instance Attribute Details
#result_count ⇒ Object (readonly)
Returns the value of attribute result_count.
19 20 21 |
# File 'lib/dnz/search.rb', line 19 def result_count @result_count end |
Instance Method Details
#facets ⇒ Object
An array of facets.
Example
search = client.search('text', :facets => 'category')
categories = search.facets['category']
categories.each do |category|
puts '%d results in category %s' % [category.count, category.name]
end
57 58 59 60 |
# File 'lib/dnz/search.rb', line 57 def facets parse_facets if @facets.nil? @facets end |
#num_results_requested ⇒ Object
The number of results requested via the :num_results option (see Client.search
).
81 82 83 |
# File 'lib/dnz/search.rb', line 81 def num_results_requested @num_results_requested || 20 end |
#options ⇒ Object
The search options passed to the digitalnz API
35 36 37 |
# File 'lib/dnz/search.rb', line 35 def @search_options end |
#page ⇒ Object
The current page of results, based on the number of requested results and the start value (see Client.search
).
64 65 66 |
# File 'lib/dnz/search.rb', line 64 def page (((@start || 0) / num_results_requested) + 1) rescue 1 end |
#page=(new_page) ⇒ Object
Set the page. This will update the search :start option and call the API again. The results array will be replaced with the new page of results.
70 71 72 73 |
# File 'lib/dnz/search.rb', line 70 def page=(new_page) @search_options['start'] = (new_page-1) * num_results_requested execute end |
#pages ⇒ Object
The number of pages available for the current search.
76 77 78 |
# File 'lib/dnz/search.rb', line 76 def pages num_results_requested < result_count ? (result_count.to_f / num_results_requested).ceil : 0 end |
#results ⇒ Object
An array of results. If the mislav-will_paginate gem is installed this will return a paginated array.
40 41 42 43 44 45 46 47 |
# File 'lib/dnz/search.rb', line 40 def results if @results.nil? parse_results paginate_results if defined? WillPaginate::Collection end @results end |
#text ⇒ Object
The text used for searching
30 31 32 |
# File 'lib/dnz/search.rb', line 30 def text @search_options[:search_text] end |
#to_s ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/dnz/search.rb', line 85 def to_s { :results => self.results.length, :facets => self.facets.length, :page => self.page, :pages => self.pages }.inspect end |