Class: Tire::Suggest::Suggest
- Inherits:
-
Object
- Object
- Tire::Suggest::Suggest
- Defined in:
- lib/tire/suggest.rb
Instance Attribute Summary collapse
-
#indices ⇒ Object
readonly
Returns the value of attribute indices.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#suggestion(name, &block) ⇒ Object
readonly
Returns the value of attribute suggestion.
Instance Method Summary collapse
-
#initialize(indices = nil, options = {}, &block) ⇒ Suggest
constructor
A new instance of Suggest.
- #json ⇒ Object
- #logged(endpoint = '_search') ⇒ Object
- #multi(&block) ⇒ Object
- #params ⇒ Object
- #perform ⇒ Object
- #response ⇒ Object
- #results ⇒ Object
- #to_curl ⇒ Object
- #to_hash ⇒ Object
- #to_json(options = {}) ⇒ Object
- #url ⇒ Object
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, ={}, &block) if indices.is_a?(Hash) @indices = indices.keys else @indices = Array(indices) end #TODO no options for now @options = @path = ['/', @indices.join(','), '_suggest'].compact.join('/').squeeze('/') block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given? end |
Instance Attribute Details
#indices ⇒ Object (readonly)
Returns the value of attribute indices.
7 8 9 |
# File 'lib/tire/suggest.rb', line 7 def indices @indices end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/tire/suggest.rb', line 7 def @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
#json ⇒ Object
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 |
#params ⇒ Object
50 51 52 53 |
# File 'lib/tire/suggest.rb', line 50 def params = @options.except(:wrapper, :payload, :load) .empty? ? '' : '?' + .to_param end |
#perform ⇒ Object
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 |
#response ⇒ Object
38 39 40 |
# File 'lib/tire/suggest.rb', line 38 def response @response || (perform; @response) end |
#results ⇒ Object
34 35 36 |
# File 'lib/tire/suggest.rb', line 34 def results @results || (perform; @results) end |
#to_curl ⇒ Object
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_hash ⇒ Object
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(={}) payload = to_hash MultiJson.encode(payload, :pretty => Configuration.pretty) end |
#url ⇒ Object
46 47 48 |
# File 'lib/tire/suggest.rb', line 46 def url Configuration.url + @path end |