Class: Tire::Results::Suggestions

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tire/results/suggestions.rb

Instance Method Summary collapse

Constructor Details

#initialize(response, options = {}) ⇒ Suggestions

Returns a new instance of Suggestions.



7
8
9
10
11
12
# File 'lib/tire/results/suggestions.rb', line 7

def initialize(response, options={})
  @response = response
  @options = options
  @shards_info ||= @response.delete '_shards'
  @keys ||= @response.keys
end

Instance Method Details

#each(&block) ⇒ Object



23
24
25
# File 'lib/tire/results/suggestions.rb', line 23

def each(&block)
  results.each(&block)
end

#errorObject



55
56
57
# File 'lib/tire/results/suggestions.rb', line 55

def error
  @response['error']
end

#failure?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/tire/results/suggestions.rb', line 63

def failure?
  ! success?
end

#keysObject



19
20
21
# File 'lib/tire/results/suggestions.rb', line 19

def keys
  @keys
end

#options(suggestion = :all) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/tire/results/suggestions.rb', line 31

def options(suggestion=:all)
  if suggestion == :all
    results.map {|k,v| v.map{|s| s['options']}}.flatten
  else
    results[suggestion.to_s].map{|s| s['options']}.flatten
  end
end

#payloads(suggestion = :all) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/tire/results/suggestions.rb', line 47

def payloads(suggestion=:all)
  if suggestion == :all
    results.map {|k,v| v.map{|s| s['options'].map {|o| o['payload']}}}.flatten
  else
    results[suggestion.to_s].map{|s| s['options'].map {|o| o['payload']}}.flatten
  end
end

#resultsObject



14
15
16
17
# File 'lib/tire/results/suggestions.rb', line 14

def results
  return [] if failure?
  @results ||= @response
end

#sizeObject



27
28
29
# File 'lib/tire/results/suggestions.rb', line 27

def size
  results.size
end

#success?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/tire/results/suggestions.rb', line 59

def success?
  error.to_s.empty?
end

#texts(suggestion = :all) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/tire/results/suggestions.rb', line 39

def texts(suggestion=:all)
  if suggestion == :all
    results.map {|k,v| v.map{|s| s['options'].map {|o| o['text']}}}.flatten
  else
    results[suggestion.to_s].map{|s| s['options'].map {|o| o['text']}}.flatten
  end
end