Class: Tire::Search::Count

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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



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

def initialize(indices=nil, options={}, &block)
  @indices = Array(indices)
  @types   = Array(options.delete(:type)).map { |type| Utils.escape(type) }
  @options = options

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

  if block_given?
    @query = Query.new
    block.arity < 1 ? @query.instance_eval(&block) : block.call(@query)
  end
end

Instance Attribute Details

#indicesObject (readonly)

Returns the value of attribute indices



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

def indices
  @indices
end

#jsonObject (readonly)

Returns the value of attribute json



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

def json
  @json
end

#queryObject (readonly)

Returns the value of attribute query



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

def query
  @query
end

#responseObject (readonly)

Returns the value of attribute response



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

def response
  @response
end

#typesObject (readonly)

Returns the value of attribute types



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

def types
  @types
end

Instance Method Details

#logged(endpoint = '_count') ⇒ Object



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

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

    Configuration.logger.log_request endpoint, indices, to_curl

    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', 'N/A', body || 'N/A'
  end
end

#paramsObject



26
27
28
29
# File 'lib/tire/count.rb', line 26

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

#performObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tire/count.rb', line 31

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 CountRequestFailed, @response.to_s
  end
  @json     = MultiJson.decode(@response.body)
  @value    = @json['count']
  return self
ensure
  logged
end

#to_curlObject



52
53
54
55
56
57
58
59
# File 'lib/tire/count.rb', line 52

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

#to_json(options = {}) ⇒ Object



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

def to_json(options={})
  @query.to_json if @query
end

#urlObject



22
23
24
# File 'lib/tire/count.rb', line 22

def url
  Configuration.url + @path
end

#valueObject



44
45
46
# File 'lib/tire/count.rb', line 44

def value
  @value || (perform and return @value)
end