Method: HTTP::FormData.ensure_hash

Defined in:
lib/http/form_data.rb

.ensure_hash(obj) ⇒ Hash

Note:

Internal usage helper, to workaround lack of #to_h on Ruby < 2.1

Coerce obj to Hash.

Returns:

  • (Hash)

Raises:

  • (Error)

    obj can't be coerced.



59
60
61
62
63
64
65
66
# File 'lib/http/form_data.rb', line 59

def ensure_hash(obj)
  case
  when obj.nil?               then {}
  when obj.is_a?(Hash)        then obj
  when obj.respond_to?(:to_h) then obj.to_h
  else raise Error, "#{obj.inspect} is neither Hash nor responds to :to_h"
  end
end