Module: Nori::CoreExt::Hash

Defined in:
lib/nori/core_ext/hash.rb

Instance Method Summary collapse

Instance Method Details

#normalize_param(key, value) ⇒ String

Returns This key value pair as a param.

Examples:

normalize_param(:name, “Bob Jones”) #=> “name=Bob%20Jones”

Parameters:

  • key (Object)

    The key for the param.

  • value (Object)

    The value for the param.

Returns:

  • (String)

    This key value pair as a param



28
29
30
31
32
33
34
35
36
# File 'lib/nori/core_ext/hash.rb', line 28

def normalize_param(key, value)
  if value.is_a?(Array)
    normalize_array_params(key, value)
  elsif value.is_a?(Hash)
    normalize_hash_params(key, value)
  else
    normalize_simple_type_params(key, value)
  end
end

#to_paramsString

Returns This hash as a query string.

Examples:

{ :name => "Bob",
  :address => {
    :street => '111 Ruby Ave.',
    :city => 'Ruby Central',
    :phones => ['111-111-1111', '222-222-2222']
  }
}.to_params
  #=> "name=Bob&address[city]=Ruby Central&address[phones][]=111-111-1111&address[phones][]=222-222-2222&address[street]=111 Ruby Ave."

Returns:

  • (String)

    This hash as a query string



18
19
20
# File 'lib/nori/core_ext/hash.rb', line 18

def to_params
  map { |k, v| normalize_param(k,v) }.flatten.join('&')
end

#to_xml_attributesString

Returns The hash as attributes for an XML tag.

Examples:

{ :one => 1, "two"=>"TWO" }.to_xml_attributes
  #=> 'one="1" two="TWO"'

Returns:

  • (String)

    The hash as attributes for an XML tag.



43
44
45
46
47
# File 'lib/nori/core_ext/hash.rb', line 43

def to_xml_attributes
  map do |k, v|
    %{#{k.to_s.snakecase.sub(/^(.{1,1})/) { |m| m.downcase }}="#{v}"}
  end.join(' ')
end