uri-query_params 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/.yardopts CHANGED
@@ -1 +1 @@
1
- --quiet --markup markdown --title 'URI query_params Documentation' --protected --files ChangeLog.md,LICENSE.txt
1
+ --markup markdown --title 'URI query_params Documentation' --protected --files ChangeLog.md,LICENSE.txt
@@ -1,3 +1,20 @@
1
+ ### 0.5.2 / 2010-11-11
2
+
3
+ * Added {URI::QueryParams::UNSAFE}:
4
+ * Contains RFC 3986 unsafe URI characters.
5
+ * Use {URI::QueryParams::UNSAFE} in {URI::QueryParams.dump} for safer
6
+ query strings.
7
+ * Added {URI::QueryParams::Mixin#query}:
8
+ * If any query-params are set, dump them out using
9
+ {URI::QueryParams.dump}.
10
+ * Renamed `parse_query_params` to
11
+ {URI::QueryParams::Mixin#parse_query_params!}.
12
+
13
+ ### 0.5.1 / 2010-11-11
14
+
15
+ * Added {URI::QueryParams::Mixin#initialize_copy} to properly copy the
16
+ `query_params` when calling `clone` or `dup` on a URI.
17
+
1
18
  ### 0.5.0 / 2010-11-07
2
19
 
3
20
  * Added {URI::QueryParams.dump}.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # URI query_params
2
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/)
3
+ * [Source](http://github.com/postmodern/uri-query_params/)
4
+ * [Issues](http://github.com/postmodern/uri-query_params/issues/)
5
5
  * Postmodern (postmodern.mod3 at gmail.com)
6
6
 
7
7
  ## Description
@@ -1,7 +1,5 @@
1
1
  require 'uri/query_params/query_params'
2
2
 
3
- require 'cgi'
4
-
5
3
  module URI
6
4
  module QueryParams
7
5
  #
@@ -28,6 +26,24 @@ module URI
28
26
  super(url)
29
27
  end
30
28
 
29
+ #
30
+ # The raw query-string.
31
+ #
32
+ # @return [String, nil]
33
+ # The raw query-string.
34
+ #
35
+ # @see QueryParams.dump
36
+ #
37
+ # @since 0.5.2
38
+ #
39
+ def query
40
+ if @query_params
41
+ QueryParams.dump(@query_params)
42
+ else
43
+ super
44
+ end
45
+ end
46
+
31
47
  #
32
48
  # Sets the query string and updates query_params.
33
49
  #
@@ -41,9 +57,12 @@ module URI
41
57
  # url.query = 'a=1&b=2'
42
58
  # # => "a=1&b=2"
43
59
  #
44
- def query=(query_str)
45
- new_query = super(query_str)
46
- parse_query_params
60
+ # @see QueryParams.parse
61
+ #
62
+ def query=(raw_query)
63
+ new_query = super(raw_query)
64
+
65
+ parse_query_params! if @query_params
47
66
  return new_query
48
67
  end
49
68
 
@@ -53,8 +72,10 @@ module URI
53
72
  # @return [Hash{String => String}]
54
73
  # The query params of the URI.
55
74
  #
75
+ # @see QueryParams.parse
76
+ #
56
77
  def query_params
57
- parse_query_params unless @query_params
78
+ parse_query_params! unless @query_params
58
79
  return @query_params
59
80
  end
60
81
 
@@ -85,14 +106,18 @@ module URI
85
106
  # Parses the query parameters from the query data, populating
86
107
  # query_params with the parsed parameters.
87
108
  #
88
- def parse_query_params
109
+ # @see QueryParams.parse
110
+ #
111
+ # @since 0.5.2
112
+ #
113
+ def parse_query_params!
89
114
  @query_params = QueryParams.parse(@query)
90
115
  end
91
116
 
92
117
  private
93
118
 
94
119
  def path_query
95
- unless (@query_params.nil? || @query_params.empty?)
120
+ if @query_params
96
121
  str = @path
97
122
 
98
123
  unless @query_params.empty?
@@ -2,6 +2,12 @@ require 'uri'
2
2
 
3
3
  module URI
4
4
  module QueryParams
5
+ # RFC 3986 unsafe characters (including ' ')
6
+ UNSAFE = [
7
+ ' ', '!', '*', "'", '(', ')', ';', ':', '@', '&', '=', '+', '$', ',',
8
+ '/', '?', '%', '#', '[', ']'
9
+ ].join
10
+
5
11
  #
6
12
  # Parses a URI query string.
7
13
  #
@@ -11,6 +17,14 @@ module URI
11
17
  # @return [Hash{String => String}]
12
18
  # The parsed query parameters.
13
19
  #
20
+ # @example
21
+ # QueryParams.parse("x=1&y=2")
22
+ # # => {"x"=>"1", "y"=>"2"}
23
+ #
24
+ # @example
25
+ # QueryParams.parse("x=a%20b%20c&y")
26
+ # # => {"x"=>"a b c", "y"=>""}
27
+ #
14
28
  def QueryParams.parse(query_string)
15
29
  query_params = {}
16
30
 
@@ -19,7 +33,7 @@ module URI
19
33
  name, value = param.split('=',2)
20
34
 
21
35
  if value
22
- query_params[name] = URI.decode(value)
36
+ query_params[name] = URI.unescape(value)
23
37
  else
24
38
  query_params[name] = ''
25
39
  end
@@ -38,24 +52,36 @@ module URI
38
52
  # @return [String]
39
53
  # The dumped URI query string.
40
54
  #
55
+ # @example Dumping Strings
56
+ # QueryParams.dump('x' => '1', 'y' => '2')
57
+ # # => "x=1&x=2"
58
+ #
59
+ # @example Dumping non-Strings
60
+ # QueryParams.dump(:x => 1, :y => true, :z => false)
61
+ # # => "x=1&x=active&z="
62
+ #
63
+ # @example Dumping Arrays
64
+ # QueryParams.dump(:x => ['a','b','c'])
65
+ # # => "x=a%20b%20c"
66
+ #
41
67
  # @since 0.5.0
42
68
  #
43
69
  def QueryParams.dump(query_params)
44
70
  query = []
45
71
 
46
72
  query_params.each do |name,value|
47
- param = case value
73
+ value = case value
48
74
  when Array
49
- "#{name}=#{CGI.escape(value.join(' '))}"
75
+ URI.escape(value.join(' '),UNSAFE)
50
76
  when true
51
- "#{name}=active"
77
+ 'active'
52
78
  when false, nil
53
- "#{name}="
79
+ ''
54
80
  else
55
- "#{name}=#{CGI.escape(value.to_s)}"
81
+ URI.escape(value.to_s,UNSAFE)
56
82
  end
57
83
 
58
- query << param
84
+ query << "#{name}=#{value}"
59
85
  end
60
86
 
61
87
  return query.join('&')
@@ -1,6 +1,6 @@
1
1
  module URI
2
2
  module QueryParams
3
3
  # uri-query_params version
4
- VERSION = '0.5.1'
4
+ VERSION = '0.5.2'
5
5
  end
6
6
  end
@@ -4,23 +4,32 @@ require 'uri/query_params'
4
4
  require 'uri'
5
5
 
6
6
  shared_examples_for "URI::QueryParams::Mixin" do
7
+ let(:query) { 'x=1&y=one%20two&z' }
8
+
7
9
  subject { uri }
8
10
 
9
- before(:each) do
10
- uri.query = 'x=1&y=one%20two&z'
11
- end
11
+ before(:each) { uri.query = query }
12
12
 
13
13
  it "should include QueryParams" do
14
14
  subject.class.should include(URI::QueryParams::Mixin)
15
15
  end
16
16
 
17
+ it "should still provide access to #query" do
18
+ uri.query.should == query
19
+ end
20
+
17
21
  it "should provide #query_params" do
18
22
  should respond_to(:query_params)
19
23
  end
20
24
 
21
25
  it "should update #query_params after #query is set" do
22
- subject.query = 'u=3'
23
- subject.query_params['u'].should == '3'
26
+ subject.query = 'u=2'
27
+ subject.query_params['u'].should == '2'
28
+ end
29
+
30
+ it "should dump out the #query_params when accessing #query" do
31
+ subject.query_params = {'u' => '3'}
32
+ subject.query.should == 'u=3'
24
33
  end
25
34
 
26
35
  it "should properly escape query param values" do
@@ -62,7 +62,7 @@ describe URI::QueryParams do
62
62
  end
63
63
 
64
64
  it "should dump query params with Array values" do
65
- subject.dump({'x' => [1,2]}).should == 'x=1+2'
65
+ subject.dump({'x' => [1,2]}).should == 'x=1%202'
66
66
  end
67
67
 
68
68
  it "should dump multiple query params" do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 5
8
- - 1
9
- version: 0.5.1
8
+ - 2
9
+ version: 0.5.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Postmodern