Class: DNZ::Search

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, search_options)
  @client = client
  @search_options = search_options

  execute
end

Instance Attribute Details

#result_countObject (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

#facetsObject

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_requestedObject

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

#optionsObject

The search options passed to the digitalnz API



35
36
37
# File 'lib/dnz/search.rb', line 35

def options
  @search_options
end

#pageObject

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

#pagesObject

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

#resultsObject

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

#textObject

The text used for searching



30
31
32
# File 'lib/dnz/search.rb', line 30

def text
  @search_options[:search_text]
end

#to_sObject



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