Module: Elasticsearch::API::Utils
Overview
Generic utility methods
Instance Method Summary collapse
-
#__bulkify(payload) ⇒ Object
Convert an array of payloads into Elasticsearch ‘headerndata` format.
-
#__escape(string) ⇒ Object
private
URL-escape a string.
-
#__listify(*list) ⇒ Object
private
Create a “list” of values from arguments, ignoring nil values.
-
#__pathify(*segments) ⇒ Object
private
Create a path (URL part) from arguments, ignoring nil values and empty strings, and encoding special characters.
Instance Method Details
#__bulkify(payload) ⇒ Object
Convert an array of payloads into Elasticsearch ‘headerndata` format
Elasticsearch::API::Utils.__bulkify [
{ :index => { :_index => 'myindexA', :_type => 'mytype', :_id => '1', :data => { :title => 'Test' } } },
{ :update => { :_index => 'myindexB', :_type => 'mytype', :_id => '2', :data => { :doc => { :title => 'Update' } } } }
]
# => {"index":{"_index":"myindexA","_type":"mytype","_id":"1"}}
# => {"title":"Test"}
# => {"update":{"_index":"myindexB","_type":"mytype","_id":"2"}}
# => {"doc":{"title":"Update"}}
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/elasticsearch/api/utils.rb', line 65 def __bulkify(payload) case # Hashes with `:data` when payload.any? { |d| d.is_a?(Hash) && d.values.first.is_a?(Hash) && (d.values.first[:data] || d.values.first['data']) } payload = payload. inject([]) do |sum, item| operation, = item.to_a.first data = .delete(:data) || .delete('data') sum << { operation => } sum << data if data sum end. map { |item| MultiJson.dump(item) } payload << "" unless payload.empty? # Array of strings when payload.all? { |d| d.is_a? String } payload << '' # Header/Data pairs else payload = payload.map { |item| MultiJson.dump(item) } payload << '' end payload = payload.join("\n") end |
#__escape(string) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
URL-escape a string
14 15 16 |
# File 'lib/elasticsearch/api/utils.rb', line 14 def __escape(string) URI.encode(string.to_s) end |
#__listify(*list) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a “list” of values from arguments, ignoring nil values
27 28 29 |
# File 'lib/elasticsearch/api/utils.rb', line 27 def __listify(*list) Array(list).flatten.compact.join(',') end |
#__pathify(*segments) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a path (URL part) from arguments, ignoring nil values and empty strings, and encoding special characters
# @example Encode special characters
__pathify(['foo', 'bar^bam']) # => 'foo/bar%5Ebam'
44 45 46 47 48 49 50 51 |
# File 'lib/elasticsearch/api/utils.rb', line 44 def __pathify(*segments) Array(segments).flatten. compact. reject { |s| s.to_s =~ /^\s*$/ }. map { |s| __escape(s) }. join('/'). squeeze('/') end |