php_http_build_query 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/php_http_build_query.rb +57 -0
- metadata +77 -0
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
|
3
|
+
class PHP
|
4
|
+
class << self
|
5
|
+
# Build an HTTP URL-encoded string suitable for appending to GET paths.
|
6
|
+
# This is intended to have an as-similar-as-possible usage as PHP's.
|
7
|
+
def http_build_query(object)
|
8
|
+
h = hashify(object)
|
9
|
+
result = ""
|
10
|
+
separator = '&'
|
11
|
+
h.keys.sort.each do |key|
|
12
|
+
result << (CGI.escape(key) + '=' + CGI.escape(h[key]) + separator)
|
13
|
+
end
|
14
|
+
result = result.sub(/#{separator}$/, '') # Remove the trailing k-v separator
|
15
|
+
return result
|
16
|
+
end
|
17
|
+
def hashify(object, parent_key = '')
|
18
|
+
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)
|
19
|
+
|
20
|
+
result = {}
|
21
|
+
case object
|
22
|
+
when String, Symbol, Numeric
|
23
|
+
result[parent_key] = object.to_s
|
24
|
+
when Hash
|
25
|
+
# Recursively call hashify, building closure-like state by
|
26
|
+
# appending the current location in the tree as new "parent_key"
|
27
|
+
# values.
|
28
|
+
hashes = object.map do |key, value|
|
29
|
+
if parent_key =~ /^[0-9]+/ or parent_key.length == 0
|
30
|
+
new_parent_key = key.to_s
|
31
|
+
else
|
32
|
+
new_parent_key = parent_key + '[' + key.to_s + ']'
|
33
|
+
end
|
34
|
+
hashify(value, new_parent_key)
|
35
|
+
end
|
36
|
+
hash = hashes.reduce { |memo, hash| memo.merge hash }
|
37
|
+
result.merge! hash
|
38
|
+
when Enumerable
|
39
|
+
# _Very_ similar to above, but iterating with "each_with_index"
|
40
|
+
hashes = {}
|
41
|
+
object.each_with_index do |value, index|
|
42
|
+
if parent_key.length == 0
|
43
|
+
new_parent_key = index.to_s
|
44
|
+
else
|
45
|
+
new_parent_key = parent_key + '[' + index.to_s + ']'
|
46
|
+
end
|
47
|
+
hashes.merge! hashify(value, new_parent_key)
|
48
|
+
end
|
49
|
+
result.merge! hashes
|
50
|
+
else
|
51
|
+
raise Exception.new("This should only be serializing Strings, Symbols, or Numerics.")
|
52
|
+
end
|
53
|
+
|
54
|
+
return result
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: php_http_build_query
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 9
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: "0.1"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Jonathan Lassoff <jof@thejof.com>
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2012-08-17 00:00:00 Z
|
18
|
+
dependencies:
|
19
|
+
- !ruby/object:Gem::Dependency
|
20
|
+
name: rspec
|
21
|
+
prerelease: false
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 3
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
33
|
+
description: The PHP language has a built-in http_build_query() function that creates a URL-encoded string that is a representation of multi-dimensional datatypes. This gem provides a mostly-congruent method in Ruby for dealing with PHP-based HTTP applications that expect parameters in this format.
|
34
|
+
email: jof@thejof.com
|
35
|
+
executables: []
|
36
|
+
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files: []
|
40
|
+
|
41
|
+
files:
|
42
|
+
- lib/php_http_build_query.rb
|
43
|
+
homepage: http://github.com/jof/php_http_build_query
|
44
|
+
licenses:
|
45
|
+
- BSD 3-clause
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.8.13
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: A Ruby implementation of PHP's http_build_query()
|
76
|
+
test_files: []
|
77
|
+
|