Module: OpenSearch::API::Utils
Overview
Generic utility methods
Instance Method Summary collapse
-
#__bulkify(payload) ⇒ Object
Convert an array of payloads into OpenSearch ‘headerndata` format.
-
#__escape(string) ⇒ Object
private
URL-escape a string.
- #__extract_params(arguments, params = [], options = {}) ⇒ Object
-
#__extract_parts(arguments, valid_parts = []) ⇒ Array<String>
private
Extracts the valid parts of the URL from the arguments.
-
#__listify(*list) ⇒ Object
private
Create a “list” of values from arguments, ignoring nil values and encoding special characters.
-
#__pathify(*segments) ⇒ Object
private
Create a path (URL part) from arguments, ignoring nil values and empty strings.
- #__report_unsupported_method(name) ⇒ Object
- #__report_unsupported_parameters(arguments, params = []) ⇒ Object
-
#__rescue_from_not_found {|block| ... } ⇒ Object
private
Calls the given block, rescuing from ‘StandardError`.
-
#__validate_and_extract_params(arguments, params = [], options = {}) ⇒ Hash
private
Validates the argument Hash against common and valid API parameters.
- #__validate_params(arguments, valid_params = []) ⇒ Object
Instance Method Details
#__bulkify(payload) ⇒ Object
Convert an array of payloads into OpenSearch ‘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.
OpenSearch::API::Utils.__bulkify [
{ :index => { :_index => 'myindexA', :_id => '1', :data => { :title => 'Test' } } },
{ :update => { :_index => 'myindexB', :_id => '2', :data => { :doc => { :title => 'Update' } } } }
]
# => {"index":{"_index":"myindexA","_id":"1"}}
# => {"title":"Test"}
# => {"update":{"_index":"myindexB","_id":"2"}}
# => {"doc":{"title":"Update"}}
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/opensearch/api/utils.rb', line 108 def __bulkify(payload) operations = %w[index create delete update] # Hashes with `:data` if payload.any? do |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']) end payload = payload .each_with_object([]) do |item, sum| operation, = item.to_a.first = .clone data = .delete(:data) || .delete('data') sum << { operation => } sum << data if data end payload = payload.map { |item| OpenSearch::API.serializer.dump(item) } payload << '' unless payload.empty? # Array of strings elsif payload.all? { |d| d.is_a? String } payload << '' # Header/Data pairs else payload = payload.map { |item| OpenSearch::API.serializer.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
39 40 41 42 43 |
# File 'lib/opensearch/api/utils.rb', line 39 def __escape(string) return string if string == '*' CGI.escape(string.to_s) end |
#__extract_params(arguments, params = [], options = {}) ⇒ Object
183 184 185 186 187 |
# File 'lib/opensearch/api/utils.rb', line 183 def __extract_params(arguments, params = [], = {}) result = arguments.select { |k, _v| COMMON_QUERY_PARAMS.include?(k) || params.include?(k) } result = result.to_h unless result.is_a?(Hash) # Normalize Ruby 1.8 and Ruby 1.9 Hash#select behaviour result.map { |k, v| v.is_a?(Array) ? [k, __listify(v, )] : [k, v] }.to_h # Listify Arrays end |
#__extract_parts(arguments, valid_parts = []) ⇒ Array<String>
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.
Mutates the ‘arguments` argument, to prevent failures in `__validate_and_extract_params`.
Extracts the valid parts of the URL from the arguments
205 206 207 208 209 210 211 212 213 214 |
# File 'lib/opensearch/api/utils.rb', line 205 def __extract_parts(arguments, valid_parts = []) parts = arguments.select { |k, _v| valid_parts.include?(k) }.to_h parts = parts.reduce([]) do |sum, item| k, v = item sum << (v.is_a?(TrueClass) ? k.to_s : v) end arguments.delete_if { |k, _v| valid_parts.include? k } parts 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.
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/opensearch/api/utils.rb', line 60 def __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 |
#__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.
# @example Encode special characters
__pathify(['foo', 'bar^bam']) # => 'foo/bar%5Ebam'
84 85 86 87 88 89 90 |
# File 'lib/opensearch/api/utils.rb', line 84 def __pathify(*segments) Array(segments).flatten .compact .reject { |s| s.to_s.strip.empty? } .join('/') .squeeze('/') end |
#__report_unsupported_method(name) ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/opensearch/api/utils.rb', line 278 def __report_unsupported_method(name) = "[!] You are using unsupported method [#{name}]" if (source = caller&.last) += " in `#{source}`" end += ". This method is not supported in the version you're using: #{OpenSearch::VERSION}, and will be removed in the next release. Suppress this warning by the `-WO` command line flag." if $stderr.tty? Kernel.warn "\e[31;1m#{}\e[0m" else Kernel.warn end end |
#__report_unsupported_parameters(arguments, params = []) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/opensearch/api/utils.rb', line 234 def __report_unsupported_parameters(arguments, params = []) = [] unsupported_params = params.select do |d| d.is_a?(Hash) ? arguments.include?(d.keys.first) : arguments.include?(d) end unsupported_params.each do |param| name = case param when Symbol param when Hash param.keys.first else raise ArgumentError, 'The param must be a Symbol or a Hash' end explanation = if param.is_a?(Hash) ". #{param.values.first[:explanation]}." else ". This parameter is not supported in the version you're using: #{OpenSearch::VERSION}, and will be removed in the next release." end = "[!] You are using unsupported parameter [:#{name}]" if (source = caller&.last) += " in `#{source}`" end += explanation << end return if .empty? << 'Suppress this warning by the `-WO` command line flag.' if $stderr.tty? Kernel.warn .map { |m| "\e[31;1m#{m}\e[0m" }.join("\n") else Kernel.warn .join("\n") end 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.
227 228 229 230 231 232 |
# File 'lib/opensearch/api/utils.rb', line 227 def __rescue_from_not_found yield rescue StandardError => e raise e unless e.class.to_s =~ /NotFound/ || e. =~ /Not\s*Found/i false end |
#__validate_and_extract_params(arguments, params = [], options = {}) ⇒ Hash
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.
Validates the argument Hash against common and valid API parameters
167 168 169 170 171 172 173 174 |
# File 'lib/opensearch/api/utils.rb', line 167 def __validate_and_extract_params(arguments, params = [], = {}) if [:skip_parameter_validation] || OpenSearch::API.settings[:skip_parameter_validation] arguments else __validate_params(arguments, params) __extract_params(arguments, params, .merge(escape: false)) end end |
#__validate_params(arguments, valid_params = []) ⇒ Object
176 177 178 179 180 181 |
# File 'lib/opensearch/api/utils.rb', line 176 def __validate_params(arguments, valid_params = []) arguments.each do |k, _v| raise ArgumentError, "URL parameter '#{k}' is not supported" \ unless COMMON_PARAMS.include?(k) || COMMON_QUERY_PARAMS.include?(k) || valid_params.include?(k) end end |