Module: ElasticsearchServerless::API::Utils
- Defined in:
- lib/elasticsearch-serverless/api/utils.rb
Class 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 and encoding special characters.
- .process_params(arguments) ⇒ Object
-
.rescue_from_not_found {|block| ... } ⇒ Object
private
Calls the given block, rescuing from ‘StandardError`.
Class Method Details
.bulkify(payload) ⇒ Object
Convert an array of payloads into Elasticsearch ‘headerndata` format
Supports various different formats of the payload: Array of Strings, Header/Data pairs, or the conveniency “combined” format where data is passed along with the header in a single item.
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"}}
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/elasticsearch-serverless/api/utils.rb', line 45 def self.bulkify(payload) operations = %w[index create delete update] case # Hashes with `:data` when payload.any? { |d| d.is_a?(Hash) && d.values.first.is_a?(Hash) && operations.include?(d.keys.first.to_s) && (d.values.first[:data] || d.values.first['data']) } payload = payload .inject([]) do |sum, item| operation, = item.to_a.first = .clone data = .delete(:data) || .delete('data') sum << { operation => } sum << data if data sum end.map { |item| JSON.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| JSON.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
110 111 112 113 114 |
# File 'lib/elasticsearch-serverless/api/utils.rb', line 110 def self.escape(string) return string if string == '*' ERB::Util.url_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 and encoding special characters.
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/elasticsearch-serverless/api/utils.rb', line 91 def self.listify(*list) = list.last.is_a?(Hash) ? list.pop : {} escape = [:escape] Array(list) .flat_map { |e| e.respond_to?(:split) ? e.split(',') : e } .flatten .compact .map { |e| escape == false ? e : escape(e) } .join(',') end |
.process_params(arguments) ⇒ Object
24 25 26 27 |
# File 'lib/elasticsearch-serverless/api/utils.rb', line 24 def self.process_params(arguments) arguments = Hash[arguments] unless arguments.is_a?(Hash) Hash[arguments.map { |k, v| v.is_a?(Array) ? [k, listify(v, { escape: false })] : [k, v] }] # Listify Arrays end |
.rescue_from_not_found {|block| ... } ⇒ 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.
Calls the given block, rescuing from ‘StandardError`.
Primary use case is the ‘:ignore` parameter for API calls.
Returns ‘false` if exception contains NotFound in its class name or message, else re-raises the exception.
127 128 129 130 131 132 133 134 135 |
# File 'lib/elasticsearch-serverless/api/utils.rb', line 127 def self.rescue_from_not_found(&block) yield rescue StandardError => e if e.class.to_s =~ /NotFound/ || e. =~ /Not\s*Found/i false else raise e end end |