uri-component 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,4 @@
1
+ Ruby URI::Conponent 0.0.5 (2013-10-23)
2
+ * Query.new: Support nil (not empty) query string
3
+ * UserInfoMixin#userinfo: Return nil if @userinfo_component is not set
4
+
data/Makefile ADDED
@@ -0,0 +1,19 @@
1
+ RAKE= rake
2
+ GEM= gem
3
+
4
+ test t: PHONY
5
+ $(RAKE) test
6
+
7
+ gem: PHONY
8
+ $(RAKE) build
9
+
10
+ upload: PHONY
11
+ $(RM) pkg/*.gem
12
+ $(MAKE) gem
13
+ $(GEM) push pkg/*.gem
14
+
15
+ install: PHONY
16
+ $(RAKE) install
17
+
18
+ PHONY:
19
+
@@ -45,6 +45,24 @@ module URI #:nodoc:
45
45
  ## p query.to_uri
46
46
  ## #=> "foo=1&foo=2&foo=3&bar=baz%40example.jp"
47
47
  class QueryParamsHash < Hash
48
+ def initialize
49
+ super
50
+ @nil = true
51
+ end
52
+
53
+ def clear
54
+ super
55
+ @nil = true
56
+ end
57
+
58
+ def nil=(flag)
59
+ @nil = flag
60
+ end
61
+
62
+ def nil?
63
+ return !@nil
64
+ end
65
+
48
66
  def convert_key(key) #:nodoc:
49
67
  return key.kind_of?(String) ? key : key.to_s
50
68
  end
@@ -70,10 +88,12 @@ module URI #:nodoc:
70
88
  end
71
89
 
72
90
  def []=(key, values) #:nodoc:
91
+ @nil = false
73
92
  values = [values] unless values.kind_of?(Array)
74
93
  super(self.convert_key(key), values)
75
94
  end
76
95
  def store(key, values) #:nodoc:
96
+ @nil = false
77
97
  self[key] = values
78
98
  end
79
99
 
@@ -126,27 +146,31 @@ module URI #:nodoc:
126
146
  QueryMixin.__send__(:included, klass)
127
147
  end
128
148
 
129
- def initialize(query_str='')
130
- unless query_str =~ RE_COMPONENT
149
+ def initialize(query_str=nil)
150
+ unless !query_str || query_str =~ RE_COMPONENT
131
151
  raise InvalidURIError, "bad Query component for URI: #{query_str}"
132
152
  end
133
153
 
134
154
  @params = QueryParamsHash.new
135
- @params.default_proc = Proc.new {|hash, key|
136
- hash[key] = [] unless hash.key?(key)
137
- }
138
155
  @param_separator = DEFAULT_PARAM_SEPARATOR
139
156
 
140
- query_str.split(/[&;]/).each do |param|
141
- next if param.empty?
142
- name, value = param.split('=', 2).map do |v|
143
- CGI.unescape(v)
157
+ if query_str
158
+ @params.nil = false
159
+ query_str.split(/[&;]/).each do |param|
160
+ next if param.nil?
161
+ name, value = param.split('=', 2).map do |v|
162
+ CGI.unescape(v)
163
+ end
164
+ @params[name] ||= []
165
+ @params[name] << value ? value : nil
144
166
  end
145
- @params[name] ||= []
146
- @params[name] << value ? value : nil
147
167
  end
148
168
  end
149
169
 
170
+ def clear
171
+ @params.clear
172
+ end
173
+
150
174
  def [](key)
151
175
  return @params[key]
152
176
  end
@@ -165,6 +189,8 @@ module URI #:nodoc:
165
189
  end
166
190
 
167
191
  def to_uri(separator=@param_separator)
192
+ return nil unless @params.nil?
193
+
168
194
  query = []
169
195
 
170
196
  @params.each do |name, values|
@@ -98,7 +98,7 @@ module URI #:nodoc:
98
98
  end
99
99
 
100
100
  def userinfo
101
- @userinfo_component.to_uri
101
+ return @userinfo_component ? @userinfo_component.to_uri : nil
102
102
  end
103
103
 
104
104
  def userinfo=(info_str)
@@ -1,6 +1,6 @@
1
1
  module URI #:nodoc:
2
2
  module Component
3
- VERSION = "0.0.4" ## :nodoc:
3
+ VERSION = "0.0.5" ## :nodoc:
4
4
  end
5
5
  end
6
6
 
@@ -14,6 +14,10 @@ class QueryTest < Test::Unit::TestCase
14
14
  end
15
15
 
16
16
  def test_parse
17
+ q_uri = nil
18
+ q = UCQ.new(q_uri)
19
+ assert_equal(q_uri, q.to_uri)
20
+
17
21
  q_uri = ''
18
22
  q = UCQ.new(q_uri)
19
23
  assert_equal(q_uri, q.to_uri)
@@ -100,6 +104,15 @@ class QueryTest < Test::Unit::TestCase
100
104
  end
101
105
 
102
106
  def test_set
107
+ q = UCQ.new()
108
+ assert_nil(q.to_uri)
109
+ q.params['foo'] = [1]
110
+ assert_equal('foo=1', q.to_uri)
111
+ q.params['foo'] = []
112
+ assert_equal('', q.to_uri)
113
+ q.params.clear
114
+ assert_nil(q.to_uri)
115
+
103
116
  q = UCQ.new('')
104
117
  q.params['foo'] = [1]
105
118
  assert_equal('foo=1', q.to_uri)
@@ -143,6 +156,23 @@ class QueryTest < Test::Unit::TestCase
143
156
  def test_mixin
144
157
  UCQ.mixin(URI::HTTPS)
145
158
 
159
+ u_uri = 'https://example.jp/'
160
+ u = URI.parse(u_uri)
161
+ assert_equal(u_uri, u.to_s)
162
+ assert_nil(u.query)
163
+ q = u.query_component
164
+ assert_kind_of(UCQ, q)
165
+ assert_nil(q.to_s)
166
+ q.params['foo'] = [1]
167
+ assert_equal('foo=1', u.query)
168
+ assert_equal(u_uri + '?foo=1', u.to_s)
169
+ q.params['foo'] = []
170
+ assert_equal('', u.query)
171
+ assert_equal(u_uri + '?', u.to_s)
172
+ q.clear
173
+ assert_nil(q.to_s)
174
+ assert_equal(u_uri, u.to_s)
175
+
146
176
  u_uri = 'https://example.jp/?foo=123%40example&bar=abc%20xyz'
147
177
  u_uri_x = u_uri.gsub('+', '%2B').gsub('%20', '+')
148
178
  u = URI.parse(u_uri)
@@ -103,6 +103,20 @@ class UserInfoTest < Test::Unit::TestCase
103
103
  def test_mixin
104
104
  UCUI.mixin(URI::HTTPS)
105
105
 
106
+ u_uri = 'https://example.jp/'
107
+ u = URI.parse(u_uri)
108
+ assert_equal(u_uri, u.to_s)
109
+ assert_nil(u.userinfo)
110
+ i = u.userinfo_component
111
+ assert_kind_of(UCUI, i)
112
+ assert_nil(i.to_s)
113
+ i.user = 'user'
114
+ assert_equal(u_uri.sub('//', '//user@'), u.to_s)
115
+ assert_equal('user', i.to_s)
116
+ i.user = nil
117
+ assert_equal(u_uri, u.to_s)
118
+ assert_nil(u.userinfo)
119
+
106
120
  u_uri = 'https://user%20name:p%40ssword@example.jp/'
107
121
  u = URI.parse(u_uri)
108
122
  i = u.userinfo_component
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uri-component
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-10 00:00:00.000000000 Z
12
+ date: 2013-10-23 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Handle URI components as an object
15
15
  email:
@@ -18,7 +18,9 @@ executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
+ - CHANGES
21
22
  - Gemfile
23
+ - Makefile
22
24
  - Rakefile
23
25
  - lib/uri/component.rb
24
26
  - lib/uri/component/path.rb
@@ -49,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
51
  version: '0'
50
52
  requirements: []
51
53
  rubyforge_project:
52
- rubygems_version: 1.8.11
54
+ rubygems_version: 1.8.23
53
55
  signing_key:
54
56
  specification_version: 3
55
57
  summary: URI::Component::* classes