Class: DNZ::Search

Inherits:
Object
  • Object
show all
Extended by:
Memoizable
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 Method Summary collapse

Methods included from Memoizable

memoize, memoized_ivar_for

Constructor Details

#initialize(client, search_options) ⇒ Search

Constructor for Search class. Do not call this directly, instead use the Client.search method.



24
25
26
27
28
29
# File 'lib/dnz/search.rb', line 24

def initialize(client, search_options)
  @client = client
  @search_options = search_options

  execute
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/dnz/search.rb', line 67

def method_missing(method, * args, & block)
  if @results
    @results.send(method, * args, & block)
  else
    super
  end
end

Instance Method Details

#custom_search?Boolean

Return true if this search is using a custom search engine

Returns:

  • (Boolean)


63
64
65
# File 'lib/dnz/search.rb', line 63

def custom_search?
  !@search_options.has_key?(:custom_search)
end

#inspectObject



48
49
50
# File 'lib/dnz/search.rb', line 48

def inspect
  self.to_s
end

#optionsObject

The search options passed to the digitalnz API



37
38
39
# File 'lib/dnz/search.rb', line 37

def options
  @search_options
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.



43
44
45
46
# File 'lib/dnz/search.rb', line 43

def page=(new_page)
  @search_options['start'] = (new_page-1) * num_results_requested
  execute
end

#textObject

The text used for searching



32
33
34
# File 'lib/dnz/search.rb', line 32

def text
  @search_options[:search_text]
end

#to_sObject



52
53
54
55
56
57
58
59
60
# File 'lib/dnz/search.rb', line 52

def to_s
  {
          :results => self.result_count,
          :facets => self.facets.length,
          :page => self.page,
          :pages => self.pages,
          :per_page => self.num_results_requested
  }.inspect
end