Class: PHP

Inherits:
Object
  • Object
show all
Defined in:
lib/php_http_build_query.rb

Class Method Summary collapse

Class Method Details

.hashify(object, parent_key = '') ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/php_http_build_query.rb', line 17

def hashify(object, parent_key = '')
  raise ArgumentError.new('This is made for serializing Hashes and Arrays only') unless (object.is_a?(Hash) or object.is_a?(Array) or parent_key.length > 0)

  result = {}
  case object
  when String, Symbol, Numeric
    result[parent_key] = object.to_s
  when Hash
    # Recursively call hashify, building closure-like state by
    # appending the current location in the tree as new "parent_key"
    # values.
    hashes = object.map do |key, value|
      if parent_key =~ /^[0-9]+/ or parent_key.length == 0
        new_parent_key = key.to_s
      else
        new_parent_key = parent_key + '[' + key.to_s + ']'
      end
      hashify(value, new_parent_key)
    end
    hash = hashes.reduce { |memo, hash| memo.merge hash }
    result.merge! hash
  when Enumerable
    # _Very_ similar to above, but iterating with "each_with_index"
    hashes = {}
    object.each_with_index do |value, index|
      if parent_key.length == 0
        new_parent_key = index.to_s
      else
        new_parent_key = parent_key + '[' + index.to_s + ']'
      end
      hashes.merge! hashify(value, new_parent_key)
    end
    result.merge! hashes
  else
    raise Exception.new("This should only be serializing Strings, Symbols, or Numerics.")
  end

  return result
end

.http_build_query(object) ⇒ Object

Build an HTTP URL-encoded string suitable for appending to GET paths. This is intended to have an as-similar-as-possible usage as PHP’s.



7
8
9
10
11
12
13
14
15
16
# File 'lib/php_http_build_query.rb', line 7

def http_build_query(object)
  h = hashify(object)
  result = ""
  separator = '&'
  h.keys.sort.each do |key|
    result << (CGI.escape(key) + '=' + CGI.escape(h[key]) + separator)
  end
  result = result.sub(/#{separator}$/, '') # Remove the trailing k-v separator
  return result
end