Class: Tire::Search::Count
- Inherits:
-
Object
- Object
- Tire::Search::Count
- Defined in:
- lib/tire/count.rb
Instance Attribute Summary collapse
-
#indices ⇒ Object
readonly
Returns the value of attribute indices.
-
#json ⇒ Object
readonly
Returns the value of attribute json.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
-
#initialize(indices = nil, options = {}, &block) ⇒ Count
constructor
A new instance of Count.
- #logged(endpoint = '_count') ⇒ Object
- #params ⇒ Object
- #perform ⇒ Object
- #to_curl ⇒ Object
- #to_json(options = {}) ⇒ Object
- #url ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(indices = nil, options = {}, &block) ⇒ Count
Returns a new instance of Count.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/tire/count.rb', line 9 def initialize(indices=nil, ={}, &block) @indices = Array(indices) @types = Array(.delete(:type)).map { |type| Utils.escape(type) } @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
#indices ⇒ Object (readonly)
Returns the value of attribute indices.
7 8 9 |
# File 'lib/tire/count.rb', line 7 def indices @indices end |
#json ⇒ Object (readonly)
Returns the value of attribute json.
7 8 9 |
# File 'lib/tire/count.rb', line 7 def json @json end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
7 8 9 |
# File 'lib/tire/count.rb', line 7 def query @query end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
7 8 9 |
# File 'lib/tire/count.rb', line 7 def response @response end |
#types ⇒ Object (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 |
#params ⇒ Object
26 27 28 29 |
# File 'lib/tire/count.rb', line 26 def params = @options.except(:wrapper) .empty? ? '' : '?' + .to_param end |
#perform ⇒ Object
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_curl ⇒ Object
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(={}) @query.to_json if @query end |
#url ⇒ Object
22 23 24 |
# File 'lib/tire/count.rb', line 22 def url Configuration.url + @path end |
#value ⇒ Object
44 45 46 |
# File 'lib/tire/count.rb', line 44 def value @value || (perform and return @value) end |