uri-query_params 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/.specopts +1 -0
- data/.yardopts +1 -0
- data/ChangeLog.md +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +37 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/lib/uri/query_params.rb +2 -0
- data/lib/uri/query_params/extensions.rb +1 -0
- data/lib/uri/query_params/extensions/http.rb +11 -0
- data/lib/uri/query_params/mixin.rb +107 -0
- data/lib/uri/query_params/query_params.rb +33 -0
- data/spec/extensions/http_spec.rb +8 -0
- data/spec/query_params_spec.rb +48 -0
- data/spec/spec_helper.rb +4 -0
- data/uri-query_params.gemspec +68 -0
- metadata +113 -0
data/.gitignore
ADDED
data/.specopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour --format specdoc
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--quiet --markup markdown --title 'URI query_params Documentation' --protected --files ChangeLog.md,LICENSE.txt
|
data/ChangeLog.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
Copyright (c) 2010 Hal Brodigan
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
'Software'), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
18
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
19
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
20
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
21
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# URI query_params
|
2
|
+
|
3
|
+
* [github.com/postmodern/uri-query_params](http://github.com/postmodern/uri-query_params/)
|
4
|
+
* [github.com/postmodern/uri-query_params/issues](http://github.com/postmodern/uri-query_params/issues/)
|
5
|
+
* Postmodern (postmodern.mod3 at gmail.com)
|
6
|
+
|
7
|
+
## Description
|
8
|
+
|
9
|
+
Allows access to the query component of the URI as a Hash. This is similar
|
10
|
+
to `$_GET` from PHP, except available on any {URI::HTTP} object.
|
11
|
+
|
12
|
+
## Examples
|
13
|
+
|
14
|
+
Inspecting the URI query_params:
|
15
|
+
|
16
|
+
url = URI('http://www.google.com/search?hl=en&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=1HY&q=bob+ross&btnG=Search')
|
17
|
+
url.query_params
|
18
|
+
# => {"btnG"=>"Search", "hs"=>"1HY", "rls"=>"org.mozilla:en-US:official", "client"=>"firefox-a", "hl"=>"en", "q"=>"bob+ross"}
|
19
|
+
|
20
|
+
|
21
|
+
url.query_params['q']
|
22
|
+
# => "bob+ross"
|
23
|
+
|
24
|
+
Setting the URI query_params:
|
25
|
+
|
26
|
+
url.query_params['q'] = 'Upright Citizens Brigade'
|
27
|
+
url.to_s
|
28
|
+
# => "http://www.google.com/search?btnG=Search&hs=1HY&rls=org.mozilla:en-US:official&client=firefox-a&hl=en&q=Upright%20Citizens%20Brigade"
|
29
|
+
|
30
|
+
## Install
|
31
|
+
|
32
|
+
$ sudo gem install uri-query_params
|
33
|
+
|
34
|
+
## License
|
35
|
+
|
36
|
+
See {file:LICENSE.txt} for license information.
|
37
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = 'uri-query_params'
|
8
|
+
gem.license = 'MIT'
|
9
|
+
gem.summary = %Q{Access the query parameters of a URI, just like $_GET in PHP.}
|
10
|
+
gem.description = %Q{Allows access to the query component of the URI as a Hash.}
|
11
|
+
gem.email = 'postmodern.mod3@gmail.com'
|
12
|
+
gem.homepage = 'http://github.com/postmodern/uri-query_params'
|
13
|
+
gem.authors = ['Postmodern']
|
14
|
+
gem.add_development_dependency 'rspec', '>= 1.3.0'
|
15
|
+
gem.add_development_dependency 'yard', '>= 0.5.3'
|
16
|
+
gem.has_rdoc = 'yard'
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
begin
|
24
|
+
require 'spec/rake/spectask'
|
25
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
26
|
+
spec.libs += ['lib', 'spec']
|
27
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
28
|
+
spec.spec_opts = ['--options', '.specopts']
|
29
|
+
end
|
30
|
+
|
31
|
+
task :spec => :check_dependencies
|
32
|
+
task :default => :spec
|
33
|
+
rescue LoadError
|
34
|
+
task :spec do
|
35
|
+
abort "RSpec is not available. In order to run `rake spec`, you must: gem install rspec"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
begin
|
40
|
+
require 'yard'
|
41
|
+
YARD::Rake::YardocTask.new
|
42
|
+
rescue LoadError
|
43
|
+
task :yard do
|
44
|
+
abort "YARD is not available. In order to run yard, you must: gem install yard"
|
45
|
+
end
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.4.0
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'uri/query_params/extensions/http'
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'uri/query_params/query_params'
|
2
|
+
|
3
|
+
require 'cgi'
|
4
|
+
|
5
|
+
module URI
|
6
|
+
module QueryParams
|
7
|
+
#
|
8
|
+
# Adds the ability to parse individual parameters from a the query field
|
9
|
+
# of a URI.
|
10
|
+
#
|
11
|
+
module Mixin
|
12
|
+
# Allows setting the query_params.
|
13
|
+
attr_writer :query_params
|
14
|
+
|
15
|
+
#
|
16
|
+
# Sets the query string and updates query_params.
|
17
|
+
#
|
18
|
+
# @param [String] query_str
|
19
|
+
# The new URI query string to use.
|
20
|
+
#
|
21
|
+
# @return [String]
|
22
|
+
# The new URI query string.
|
23
|
+
#
|
24
|
+
# @example
|
25
|
+
# url.query = 'a=1&b=2'
|
26
|
+
# # => "a=1&b=2"
|
27
|
+
#
|
28
|
+
def query=(query_str)
|
29
|
+
new_query = super(query_str)
|
30
|
+
parse_query_params
|
31
|
+
return new_query
|
32
|
+
end
|
33
|
+
|
34
|
+
#
|
35
|
+
# The query params of the URI.
|
36
|
+
#
|
37
|
+
# @return [Hash{String => String}]
|
38
|
+
# The query params of the URI.
|
39
|
+
#
|
40
|
+
def query_params
|
41
|
+
parse_query_params unless @query_params
|
42
|
+
return @query_params
|
43
|
+
end
|
44
|
+
|
45
|
+
#
|
46
|
+
# Iterates over every query parameter.
|
47
|
+
#
|
48
|
+
# @yield [name, value]
|
49
|
+
# The block to pass each query parameter to.
|
50
|
+
#
|
51
|
+
# @yieldparam [String] name
|
52
|
+
# The name of the query parameter.
|
53
|
+
#
|
54
|
+
# @yieldparam [String] value
|
55
|
+
# The value of the query parameter.
|
56
|
+
#
|
57
|
+
# @example
|
58
|
+
# url.each_query_param do |name,value|
|
59
|
+
# puts "#{name} = #{value}"
|
60
|
+
# end
|
61
|
+
#
|
62
|
+
def each_query_param(&block)
|
63
|
+
query_params.each(&block)
|
64
|
+
end
|
65
|
+
|
66
|
+
protected
|
67
|
+
|
68
|
+
#
|
69
|
+
# Parses the query parameters from the query data, populating
|
70
|
+
# query_params with the parsed parameters.
|
71
|
+
#
|
72
|
+
def parse_query_params
|
73
|
+
@query_params = QueryParams.parse(@query)
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def path_query
|
79
|
+
if @query_params
|
80
|
+
str = @path
|
81
|
+
|
82
|
+
unless @query_params.empty?
|
83
|
+
str += '?' + @query_params.to_a.map { |name,value|
|
84
|
+
if value==true
|
85
|
+
"#{name}=active"
|
86
|
+
elsif value
|
87
|
+
if value.kind_of?(Array)
|
88
|
+
"#{name}=#{CGI.escape(value.join(' '))}"
|
89
|
+
else
|
90
|
+
"#{name}=#{CGI.escape(value.to_s)}"
|
91
|
+
end
|
92
|
+
else
|
93
|
+
"#{name}="
|
94
|
+
end
|
95
|
+
}.join('&')
|
96
|
+
end
|
97
|
+
|
98
|
+
str
|
99
|
+
else
|
100
|
+
# do not rebuild the path-query, if the query_params have not
|
101
|
+
# been parsed yet
|
102
|
+
super
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
module URI
|
4
|
+
module QueryParams
|
5
|
+
#
|
6
|
+
# Parses a URI query string.
|
7
|
+
#
|
8
|
+
# @param [String] query_string
|
9
|
+
# The URI query string.
|
10
|
+
#
|
11
|
+
# @return [Hash{String => String}]
|
12
|
+
# The parsed query parameters.
|
13
|
+
#
|
14
|
+
def QueryParams.parse(query_string)
|
15
|
+
query_params = {}
|
16
|
+
|
17
|
+
if query_string
|
18
|
+
query_string.split('&').each do |param|
|
19
|
+
name, value = param.split('=')
|
20
|
+
|
21
|
+
if value
|
22
|
+
query_params[name] = URI.decode(value)
|
23
|
+
else
|
24
|
+
query_params[name] = ''
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
return query_params
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'uri/query_params'
|
3
|
+
|
4
|
+
require 'uri'
|
5
|
+
|
6
|
+
describe URI::QueryParams do
|
7
|
+
let(:uri) { URI('http://www.example.com/page.php?x=1&y=one%20two&z') }
|
8
|
+
subject { uri }
|
9
|
+
|
10
|
+
it "should provide #query_params" do
|
11
|
+
should respond_to(:query_params)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should update #query_params after #query is set" do
|
15
|
+
subject.query = 'u=3'
|
16
|
+
subject.query_params['u'].should == '3'
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should properly escape query param values" do
|
20
|
+
subject.query_params = {'x' => '1&2', 'y' => 'one=two', 'z' => '?'}
|
21
|
+
|
22
|
+
subject.to_s.should == "http://www.example.com/page.php?x=1%262&y=one%3Dtwo&z=%3F"
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#query_params" do
|
26
|
+
subject { uri.query_params }
|
27
|
+
|
28
|
+
it "should be a Hash" do
|
29
|
+
subject.class.should == Hash
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should contain params" do
|
33
|
+
should_not be_empty
|
34
|
+
end
|
35
|
+
|
36
|
+
it "can contain single-word params" do
|
37
|
+
subject['x'].should == '1'
|
38
|
+
end
|
39
|
+
|
40
|
+
it "can contain multi-word params" do
|
41
|
+
subject['y'].should == 'one two'
|
42
|
+
end
|
43
|
+
|
44
|
+
it "can contain empty params" do
|
45
|
+
subject['z'].should be_empty
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{uri-query_params}
|
8
|
+
s.version = "0.4.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Postmodern"]
|
12
|
+
s.date = %q{2010-08-28}
|
13
|
+
s.description = %q{Allows access to the query component of the URI as a Hash.}
|
14
|
+
s.email = %q{postmodern.mod3@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"ChangeLog.md",
|
17
|
+
"LICENSE.txt",
|
18
|
+
"README.md"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".gitignore",
|
22
|
+
".specopts",
|
23
|
+
".yardopts",
|
24
|
+
"ChangeLog.md",
|
25
|
+
"LICENSE.txt",
|
26
|
+
"README.md",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"lib/uri/query_params.rb",
|
30
|
+
"lib/uri/query_params/extensions.rb",
|
31
|
+
"lib/uri/query_params/extensions/http.rb",
|
32
|
+
"lib/uri/query_params/mixin.rb",
|
33
|
+
"lib/uri/query_params/query_params.rb",
|
34
|
+
"spec/extensions/http_spec.rb",
|
35
|
+
"spec/query_params_spec.rb",
|
36
|
+
"spec/spec_helper.rb",
|
37
|
+
"uri-query_params.gemspec"
|
38
|
+
]
|
39
|
+
s.has_rdoc = %q{yard}
|
40
|
+
s.homepage = %q{http://github.com/postmodern/uri-query_params}
|
41
|
+
s.licenses = ["MIT"]
|
42
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
43
|
+
s.require_paths = ["lib"]
|
44
|
+
s.rubygems_version = %q{1.3.7}
|
45
|
+
s.summary = %q{Access the query parameters of a URI, just like $_GET in PHP.}
|
46
|
+
s.test_files = [
|
47
|
+
"spec/extensions/http_spec.rb",
|
48
|
+
"spec/query_params_spec.rb",
|
49
|
+
"spec/spec_helper.rb"
|
50
|
+
]
|
51
|
+
|
52
|
+
if s.respond_to? :specification_version then
|
53
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
54
|
+
s.specification_version = 3
|
55
|
+
|
56
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
57
|
+
s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
|
58
|
+
s.add_development_dependency(%q<yard>, [">= 0.5.3"])
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
61
|
+
s.add_dependency(%q<yard>, [">= 0.5.3"])
|
62
|
+
end
|
63
|
+
else
|
64
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
65
|
+
s.add_dependency(%q<yard>, [">= 0.5.3"])
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uri-query_params
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 4
|
8
|
+
- 0
|
9
|
+
version: 0.4.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Postmodern
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-08-28 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 3
|
31
|
+
- 0
|
32
|
+
version: 1.3.0
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: yard
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
- 5
|
46
|
+
- 3
|
47
|
+
version: 0.5.3
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
description: Allows access to the query component of the URI as a Hash.
|
51
|
+
email: postmodern.mod3@gmail.com
|
52
|
+
executables: []
|
53
|
+
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files:
|
57
|
+
- ChangeLog.md
|
58
|
+
- LICENSE.txt
|
59
|
+
- README.md
|
60
|
+
files:
|
61
|
+
- .gitignore
|
62
|
+
- .specopts
|
63
|
+
- .yardopts
|
64
|
+
- ChangeLog.md
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- VERSION
|
69
|
+
- lib/uri/query_params.rb
|
70
|
+
- lib/uri/query_params/extensions.rb
|
71
|
+
- lib/uri/query_params/extensions/http.rb
|
72
|
+
- lib/uri/query_params/mixin.rb
|
73
|
+
- lib/uri/query_params/query_params.rb
|
74
|
+
- spec/extensions/http_spec.rb
|
75
|
+
- spec/query_params_spec.rb
|
76
|
+
- spec/spec_helper.rb
|
77
|
+
- uri-query_params.gemspec
|
78
|
+
has_rdoc: yard
|
79
|
+
homepage: http://github.com/postmodern/uri-query_params
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options:
|
84
|
+
- --charset=UTF-8
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
version: "0"
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
version: "0"
|
103
|
+
requirements: []
|
104
|
+
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 1.3.7
|
107
|
+
signing_key:
|
108
|
+
specification_version: 3
|
109
|
+
summary: Access the query parameters of a URI, just like $_GET in PHP.
|
110
|
+
test_files:
|
111
|
+
- spec/extensions/http_spec.rb
|
112
|
+
- spec/query_params_spec.rb
|
113
|
+
- spec/spec_helper.rb
|