Class: Tire::Search

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

Direct Known Subclasses

Count

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indices, types, payload) ⇒ Search

Returns a new instance of Search.



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

def initialize(indices, types, payload)
  @indices = Array(indices)
  @types   = Array(types).map { |type| Utils.escape(type) }
  if payload.is_a?(String)
    @payload = payload
  else
    @payload_hash = payload
  end

  @path    = ['/', @indices.join(','), @types.join(','), '_search'].compact.join('/').squeeze('/')
  if indices == 'blog'
    @path += '?preference=_local'
  end
end

Instance Attribute Details

#indicesObject (readonly)

Returns the value of attribute indices.



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

def indices
  @indices
end

#payloadObject (readonly)

Returns the value of attribute payload.



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

def payload
  @payload
end

#payload_hashObject (readonly)

Returns the value of attribute payload_hash.



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

def payload_hash
  @payload_hash
end

Instance Method Details

#logged(error = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/tire/search.rb', line 60

def logged(error=nil)
  if Configuration.logger

    Configuration.logger.log_request '_search', indices, to_curl

    code = @response.code rescue nil

    if Configuration.logger.level.to_s == 'debug'
      # FIXME: Depends on RestClient implementation
      body = if @json
        defined?(Yajl) ? Yajl::Encoder.encode(@json, :pretty => true) : MultiJson.encode(@json)
      else
        @response.body rescue nil
      end
    else
      body = ''
    end

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

#performObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tire/search.rb', line 36

def perform
  @response = Configuration.client.get(self.url, self.payload)
  if @response.failure?
    STDERR.puts "[REQUEST FAILED] #{self.to_curl}\n"
    raise SearchRequestFailed, @response.to_s
  end
  @json     = MultiJson.decode(@response.body)
  return @json
ensure
  logged
end

#responseObject



28
29
30
# File 'lib/tire/search.rb', line 28

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

#resultsObject



24
25
26
# File 'lib/tire/search.rb', line 24

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

#to_curlObject



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

def to_curl
  %Q|curl -X GET #{url} -d '#{payload}'|
end

#to_hashObject



56
57
58
# File 'lib/tire/search.rb', line 56

def to_hash
  @hash_payload ||= MultiJson.decode(payload)
end

#urlObject



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

def url
  Configuration.url + @path
end