Class: Tire::Suggest::Suggest

Inherits:
Object
  • Object
show all
Defined in:
lib/tire/suggest.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indices = nil, options = {}, &block) ⇒ Suggest

Returns a new instance of Suggest.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tire/suggest.rb', line 9

def initialize(indices=nil, options={}, &block)
  if indices.is_a?(Hash)
    @indices = indices.keys
  else
    @indices = Array(indices)
  end

  #TODO no options for now
  @options = options

  @path    = ['/', @indices.join(','), '_suggest'].compact.join('/').squeeze('/')

  block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
end

Instance Attribute Details

#indicesObject (readonly)

Returns the value of attribute indices.



7
8
9
# File 'lib/tire/suggest.rb', line 7

def indices
  @indices
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/tire/suggest.rb', line 7

def options
  @options
end

#suggestion(name, &block) ⇒ Object (readonly)

Returns the value of attribute suggestion.



7
8
9
# File 'lib/tire/suggest.rb', line 7

def suggestion
  @suggestion
end

Instance Method Details

#jsonObject



42
43
44
# File 'lib/tire/suggest.rb', line 42

def json
  @json || (perform; @json)
end

#logged(endpoint = '_search') ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/tire/suggest.rb', line 84

def logged(endpoint='_search')
  if Configuration.logger

    Configuration.logger.log_request endpoint, indices, to_curl

    took = @json['took']  rescue nil
    code = @response.code rescue nil

    if Configuration.logger.level.to_s == 'debug'
      body = if @json
        MultiJson.encode( @json, :pretty => Configuration.pretty)
      else
        MultiJson.encode( MultiJson.load(@response.body), :pretty => Configuration.pretty) rescue ''
      end
    else
      body = ''
    end

    Configuration.logger.log_response code || 'N/A', took || 'N/A', body || 'N/A'
  end
end

#multi(&block) ⇒ Object



29
30
31
32
# File 'lib/tire/suggest.rb', line 29

def multi(&block)
  @suggestion = MultiSuggestion.new(&block)
  self
end

#paramsObject



50
51
52
53
# File 'lib/tire/suggest.rb', line 50

def params
  options = @options.except(:wrapper, :payload, :load)
  options.empty? ? '' : '?' + options.to_param
end

#performObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tire/suggest.rb', line 55

def perform
  @response = Configuration.client.get(self.url + self.params, self.to_json)
  if @response.failure?
    STDERR.puts "[REQUEST FAILED] #{self.to_curl}\n"
    raise Tire::Search::SearchRequestFailed, @response.to_s
  end
  @json = MultiJson.decode(@response.body)
  @results = Results::Suggestions.new(@json, @options)
  return self
ensure
  logged
end

#responseObject



38
39
40
# File 'lib/tire/suggest.rb', line 38

def response
  @response || (perform; @response)
end

#resultsObject



34
35
36
# File 'lib/tire/suggest.rb', line 34

def results
  @results  || (perform; @results)
end

#to_curlObject



68
69
70
71
# File 'lib/tire/suggest.rb', line 68

def to_curl
  to_json_escaped = to_json.gsub("'",'\u0027')
  %Q|curl -X GET '#{url}#{params.empty? ? '?' : params.to_s + '&'}pretty' -d '#{to_json_escaped}'|
end

#to_hashObject



73
74
75
76
77
# File 'lib/tire/suggest.rb', line 73

def to_hash
  request = {}
  request.update( @suggestion.to_hash )
  request
end

#to_json(options = {}) ⇒ Object



79
80
81
82
# File 'lib/tire/suggest.rb', line 79

def to_json(options={})
  payload = to_hash
  MultiJson.encode(payload, :pretty => Configuration.pretty)
end

#urlObject



46
47
48
# File 'lib/tire/suggest.rb', line 46

def url
  Configuration.url + @path
end