queryparams 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rvmrc +2 -0
- data/Gemfile +4 -0
- data/README.md +15 -0
- data/Rakefile +1 -0
- data/lib/queryparams.rb +24 -0
- data/lib/queryparams/version.rb +3 -0
- data/querystring.gemspec +24 -0
- data/spec/queryparams_spec.rb +24 -0
- metadata +67 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# QueryParams
|
2
|
+
|
3
|
+
A straight forward gem to convert ruby hashes and arrays to http query strings. It supports hashes, arrays, nested hashes and arrays of hashes and all combinations thereof. E.g.:
|
4
|
+
|
5
|
+
Querystring.encode({a: 1, b: "c"})
|
6
|
+
=> "a=1&b=c"
|
7
|
+
|
8
|
+
Querystring.encode({a: {b: 'c', d: 'e'}, f: 'g'})
|
9
|
+
=> "a[b]=c&a[d]=e&f=g"
|
10
|
+
|
11
|
+
Querystring.encode({a: ['bingo', 'hepp']})
|
12
|
+
=> "a[0]=bingo&a[1]=hepp"
|
13
|
+
|
14
|
+
Querystring.encode({a: [{b:'c', d:'e'}, {f:'g'}]})
|
15
|
+
=> "a[0][b]=c&a[0][d]=e&a[1][f]=g"
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/queryparams.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "queryparams/version"
|
2
|
+
require "cgi"
|
3
|
+
|
4
|
+
module QueryParams
|
5
|
+
|
6
|
+
def self.encode(value, key = nil)
|
7
|
+
q = []
|
8
|
+
case value
|
9
|
+
when Hash
|
10
|
+
value.each { |k,v| q << encode(v, append_key(key,k)) }
|
11
|
+
when Array
|
12
|
+
value.each_with_index { |v,i| q << encode(v, append_key(key, i)) }
|
13
|
+
else
|
14
|
+
return "#{key}=#{CGI.escape(value.to_s)}"
|
15
|
+
end
|
16
|
+
q.join('&')
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def self.append_key(root_key, key)
|
22
|
+
root_key.nil? ? key : "#{root_key}[#{key.to_s}]"
|
23
|
+
end
|
24
|
+
end
|
data/querystring.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "queryparams/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "queryparams"
|
7
|
+
s.version = QueryParams::VERSION
|
8
|
+
s.authors = ["Simen Svale Skogsrud"]
|
9
|
+
s.email = ["simen@bengler.no"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{A straight forward gem to convert ruby hashes and arrays to http query strings.}
|
12
|
+
s.description = %q{ It supports hashes, arrays, nested hashes and arrays of hashes and all combinations thereof.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "queryparams"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler.require
|
3
|
+
|
4
|
+
describe QueryParams do
|
5
|
+
it "generates nothing if need be" do
|
6
|
+
QueryParams.encode({}).should eq ""
|
7
|
+
end
|
8
|
+
|
9
|
+
it "generates a straight forward query string" do
|
10
|
+
QueryParams.encode({a: 1, b: "c"}).should eq "a=1&b=c"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "handles nested hashes" do
|
14
|
+
QueryParams.encode({a: {b: 'c', d: 'e'}, f: 'g'}).should eq "a[b]=c&a[d]=e&f=g"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "handles arrays" do
|
18
|
+
QueryParams.encode({a: ['bingo', 'hepp']}).should eq "a[0]=bingo&a[1]=hepp"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "handles arrays of hashes" do
|
22
|
+
QueryParams.encode({a: [{b:'c', d:'e'}, {f:'g'}]}).should eq "a[0][b]=c&a[0][d]=e&a[1][f]=g"
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: queryparams
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Simen Svale Skogsrud
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-29 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70326031061260 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70326031061260
|
25
|
+
description: ! ' It supports hashes, arrays, nested hashes and arrays of hashes and
|
26
|
+
all combinations thereof.'
|
27
|
+
email:
|
28
|
+
- simen@bengler.no
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- .rvmrc
|
35
|
+
- Gemfile
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- lib/queryparams.rb
|
39
|
+
- lib/queryparams/version.rb
|
40
|
+
- querystring.gemspec
|
41
|
+
- spec/queryparams_spec.rb
|
42
|
+
homepage: ''
|
43
|
+
licenses: []
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project: queryparams
|
62
|
+
rubygems_version: 1.8.10
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: A straight forward gem to convert ruby hashes and arrays to http query strings.
|
66
|
+
test_files:
|
67
|
+
- spec/queryparams_spec.rb
|