uri-component 0.0.3 → 0.0.4

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/uri/component.rb CHANGED
@@ -1,10 +1,21 @@
1
+ ## = uri/component.rb
2
+ ##
3
+ ## Ruby URI::Component module
4
+ ##
5
+ ## Author:: SATOH Fumiyasu
6
+ ## Copyright:: (c) 2007-2011 SATOH Fumiyasu @ OSS Technology, Corp.
7
+ ## License:: You can redistribute it and/or modify it under the same term as Ruby.
8
+ ##
9
+
1
10
  require "uri/component/userinfo"
2
11
  require "uri/component/path"
3
12
  require "uri/component/query"
4
13
 
5
- module URI
14
+ module URI #:nodoc:
6
15
  ## Handle URI components as an object
7
16
  module Component
17
+ ## == Description
18
+ ##
8
19
  ## Add the following instance methods to the class +klass+:
9
20
  ##
10
21
  ## userinfo_component::
@@ -15,7 +26,7 @@ module URI
15
26
  ## query_component::
16
27
  ## Returns the query component of the URI as URI::Component::Query object.
17
28
  ##
18
- ## Usage:
29
+ ## == Example
19
30
  ##
20
31
  ## require "uri"
21
32
  ## require "uri/component"
@@ -7,14 +7,14 @@
7
7
 
8
8
  require 'uri'
9
9
 
10
- module URI
10
+ module URI #:nodoc:
11
11
  module Component
12
12
  ## Handle a path component in an URI as an object
13
13
  class Path
14
14
  #:stopdoc:
15
- ## Same as URI::UNSAFE, plus ';' (separator for path nodes)
15
+ ## Same as URI::UNSAFE, plus '/' (separator for path nodes)
16
16
  ## and '?' (separator for path and query)
17
- RE_UNSAFE = /
17
+ RE_NODE_UNSAFE = /
18
18
  [^#{URI::REGEXP::PATTERN::UNRESERVED}#{URI::REGEXP::PATTERN::RESERVED}]|
19
19
  [\/?]
20
20
  /x
@@ -41,18 +41,22 @@ module URI
41
41
  end
42
42
  end
43
43
 
44
- def escape(v)
45
- return URI.escape(v, RE_UNSAFE)
44
+ def escape_node(v)
45
+ return URI.escape(v, RE_NODE_UNSAFE)
46
46
  end
47
47
 
48
48
  def to_uri
49
49
  return '' if @nodes.empty?
50
50
  return '/' + @nodes.map do |node|
51
- self.escape(node)
51
+ self.escape_node(node)
52
52
  end.join('/')
53
53
  end
54
54
  alias to_s to_uri
55
55
 
56
+ def [](*index)
57
+ return @nodes[*index]
58
+ end
59
+
56
60
  def nodes
57
61
  return @nodes
58
62
  end
@@ -80,7 +84,7 @@ module URI
80
84
  end
81
85
  end
82
86
 
83
- module PathMixin
87
+ module PathMixin #:nodoc:
84
88
  def initialize_copy(uri)
85
89
  if (path = uri.instance_variable_get('@path_component'))
86
90
  @path_component = path.dup
@@ -8,59 +8,87 @@
8
8
  require 'uri'
9
9
  require 'cgi'
10
10
 
11
- module URI
11
+ module URI #:nodoc:
12
12
  module Component
13
- class QueryParamsHash < Hash #:nodoc:
14
- def convert_key(key)
13
+ ## Handle query parameters for the URI as a hash
14
+ ##
15
+ ## == Example
16
+ ##
17
+ ## require "uri/component/query"
18
+ ##
19
+ ## query = URI::Component::Query.new('foo=123&bar=x+y&bar=%40example')
20
+ ## params = query.params
21
+ ## #=> #<URI::Component::QueryParamsHash: {"foo"=>["123"], "bar"=>["x y", "@example"]}>
22
+ ##
23
+ ## p params['foo']
24
+ ## #=> ["123"]
25
+ ## p params[:foo]
26
+ ## #=> ["123"]
27
+ ## p params.values(:foo)
28
+ ## #=> ["123"]
29
+ ## p params.value(:foo)
30
+ ## #=> "123"
31
+ ##
32
+ ## p params['bar']
33
+ ## #=> ["x y", "@example"]
34
+ ## p params[:bar]
35
+ ## #=> ["x y", "@example"]
36
+ ## p params.values(:bar)
37
+ ## #=> ["x y", "@example"]
38
+ ## p params.value(:bar)
39
+ ## #=> "x y"
40
+ ##
41
+ ## params[:foo] = [1, 2, 3]
42
+ ## #=> [1, 2, 3]
43
+ ## params[:bar] = 'baz@baz.example.jp'
44
+ ## #=> ["baz@baz.example.jp"]
45
+ ## p query.to_uri
46
+ ## #=> "foo=1&foo=2&foo=3&bar=baz%40example.jp"
47
+ class QueryParamsHash < Hash
48
+ def convert_key(key) #:nodoc:
15
49
  return key.kind_of?(String) ? key : key.to_s
16
50
  end
17
- def [](key)
51
+ def [](key) #:nodoc:
18
52
  super(self.convert_key(key))
19
53
  end
20
- def fetch(key, default = nil)
54
+ def fetch(key, default = nil) #:nodoc:
21
55
  super(self.convert_key(key), default)
22
56
  end
23
57
 
24
- def values(key = nil)
25
- return key ? self[key] : super
26
- end
27
-
28
- def values=(key, values)
29
- self[key] = values
58
+ ## Returns an array of values from the hash for the given key.
59
+ def values(key)
60
+ return self[key]
30
61
  end
31
62
 
63
+ ## Returns a value from the hash for the given key.
32
64
  def value(key)
33
65
  return self[key][0]
34
66
  end
35
67
 
36
- def value=(key, value)
37
- self[key] = value
38
- end
39
-
40
- def values_at(*keys)
68
+ def values_at(*keys) #:nodoc:
41
69
  super(*keys.map {|key| self.convert_key(key)})
42
70
  end
43
71
 
44
- def []=(key, values)
72
+ def []=(key, values) #:nodoc:
45
73
  values = [values] unless values.kind_of?(Array)
46
74
  super(self.convert_key(key), values)
47
75
  end
48
- def store(key, values)
76
+ def store(key, values) #:nodoc:
49
77
  self[key] = values
50
78
  end
51
79
 
52
- def delete(key)
80
+ def delete(key) #:nodoc:
53
81
  super(self.convert_key(key))
54
82
  end
55
83
 
56
- def has_key?(key)
84
+ def has_key?(key) #:nodoc:
57
85
  super(self.convert_key(key))
58
86
  end
59
87
  alias :include? :has_key?
60
88
  alias :key? :has_key?
61
89
  alias :member? :has_key?
62
90
 
63
- def merge(hash)
91
+ def merge(hash) #:nodoc:
64
92
  hash_new = self.class.new
65
93
  hash.each do |key, value|
66
94
  hash_new[key] = value
@@ -68,7 +96,7 @@ module URI
68
96
  return hash_new
69
97
  end
70
98
 
71
- def merge!(hash)
99
+ def merge!(hash) #:nodoc:
72
100
  hash.each do |key, value|
73
101
  self[key] = value
74
102
  end
@@ -76,7 +104,7 @@ module URI
76
104
  end
77
105
  alias :update :merge!
78
106
 
79
- def replace(hash)
107
+ def replace(hash) #:nodoc:
80
108
  self.clear
81
109
  hash.each do |key, value|
82
110
  self[key] = value
@@ -91,6 +119,8 @@ module URI
91
119
  RE_COMPONENT = /^#{URI::REGEXP::PATTERN::QUERY}?$/
92
120
  #:startdoc:
93
121
 
122
+ DEFAULT_PARAM_SEPARATOR = '&'
123
+
94
124
  def self.mixin(klass) #:nodoc:
95
125
  QueryMixin.__send__(:append_features, klass)
96
126
  QueryMixin.__send__(:included, klass)
@@ -105,7 +135,7 @@ module URI
105
135
  @params.default_proc = Proc.new {|hash, key|
106
136
  hash[key] = [] unless hash.key?(key)
107
137
  }
108
- @param_separator = '&'
138
+ @param_separator = DEFAULT_PARAM_SEPARATOR
109
139
 
110
140
  query_str.split(/[&;]/).each do |param|
111
141
  next if param.empty?
@@ -117,6 +147,11 @@ module URI
117
147
  end
118
148
  end
119
149
 
150
+ def [](key)
151
+ return @params[key]
152
+ end
153
+
154
+ ## Returns query parameters as an URI::Component::QueryParamsHash object
120
155
  def params
121
156
  return @params
122
157
  end
@@ -144,7 +179,7 @@ module URI
144
179
  alias to_s to_uri
145
180
  end
146
181
 
147
- module QueryMixin
182
+ module QueryMixin #:nodoc:
148
183
  def initialize_copy(uri)
149
184
  if (query = uri.instance_variable_get('@query_component'))
150
185
  @query_component = query.dup
@@ -7,18 +7,18 @@
7
7
 
8
8
  require 'uri'
9
9
 
10
- module URI
10
+ module URI #:nodoc:
11
11
  module Component
12
12
  ## Handle an userinfo component in an URI as an object
13
13
  class UserInfo
14
14
  #:stopdoc:
15
- RE_UNSAFE = /[^#{URI::REGEXP::PATTERN::UNRESERVED}]/
15
+ RE_ELEMENT_UNSAFE = /[^#{URI::REGEXP::PATTERN::UNRESERVED}]/
16
16
  ## Same as URI::USERINFO, except ';' and ':'
17
- RE_PART = /(?:
17
+ RE_ELEMENT = /(?:
18
18
  [#{URI::REGEXP::PATTERN::UNRESERVED}&=+$,]|
19
19
  #{URI::REGEXP::PATTERN::ESCAPED})*
20
20
  /x
21
- RE_COMPONENT = /^(?:(#{RE_PART});)?(#{RE_PART})(?::(#{RE_PART}))?$/
21
+ RE_COMPONENT = /^(?:(#{RE_ELEMENT});)?(#{RE_ELEMENT})(?::(#{RE_ELEMENT}))?$/
22
22
  #:startdoc:
23
23
 
24
24
  def self.mixin(klass) #:nodoc:
@@ -26,8 +26,8 @@ module URI
26
26
  UserInfoMixin.__send__(:included, klass)
27
27
  end
28
28
 
29
- def escape(v)
30
- return URI.escape(v, RE_UNSAFE)
29
+ def escape_element(v)
30
+ return URI.escape(v, RE_ELEMENT_UNSAFE)
31
31
  end
32
32
 
33
33
  def initialize(info_str=nil)
@@ -80,15 +80,15 @@ module URI
80
80
  return nil unless @user
81
81
 
82
82
  info_str = ''
83
- info_str += self.escape(@domain) + ';' if @domain
84
- info_str += @user ? self.escape(@user) : '';
85
- info_str += ':' + self.escape(@password) if @password
83
+ info_str += self.escape_element(@domain) + ';' if @domain
84
+ info_str += @user ? self.escape_element(@user) : '';
85
+ info_str += ':' + self.escape_element(@password) if @password
86
86
  return info_str
87
87
  end
88
88
  alias to_s to_uri
89
89
  end
90
90
 
91
- module UserInfoMixin
91
+ module UserInfoMixin #:nodoc:
92
92
  def initialize_copy(uri)
93
93
  if (userinfo = uri.instance_variable_get('@userinfo_component'))
94
94
  @userinfo_component = userinfo.dup
@@ -113,8 +113,8 @@ module URI
113
113
  return nil unless user
114
114
 
115
115
  domain = @userinfo_component.domain
116
- user_uri = domain ? @userinfo_component.escape(domain) + ';' : ''
117
- user_uri += @userinfo_component.escape(user)
116
+ user_uri = domain ? @userinfo_component.escape_element(domain) + ';' : ''
117
+ user_uri += @userinfo_component.escape_element(user)
118
118
  return user_uri
119
119
  end
120
120
 
@@ -130,7 +130,7 @@ module URI
130
130
 
131
131
  def password
132
132
  pass = @userinfo_component.password
133
- return pass ? @userinfo_component.escape(pass) : nil
133
+ return pass ? @userinfo_component.escape_element(pass) : nil
134
134
  end
135
135
 
136
136
  def password=(pass_uri)
@@ -1,6 +1,6 @@
1
- module URI
1
+ module URI #:nodoc:
2
2
  module Component
3
- VERSION = "0.0.3" ## :nodoc:
3
+ VERSION = "0.0.4" ## :nodoc:
4
4
  end
5
5
  end
6
6
 
@@ -6,7 +6,7 @@ UCP = URI::Component::Path
6
6
  module URI
7
7
  module Component
8
8
 
9
- class TestPathClass < Test::Unit::TestCase
9
+ class PathTest < Test::Unit::TestCase
10
10
  def setup
11
11
  end
12
12
 
@@ -36,6 +36,9 @@ class TestPathClass < Test::Unit::TestCase
36
36
  assert_equal(p_uri, p.to_uri)
37
37
  assert_equal(2, p.nodes.size)
38
38
  assert_equal('foo', p.nodes[0])
39
+ assert_equal('foo', p[0])
40
+ assert_empty(p.nodes[1])
41
+ assert_empty(p[1])
39
42
 
40
43
  p_uri = '/123%20abc'
41
44
  p = UCP.new(p_uri)
@@ -6,7 +6,7 @@ UCQ = URI::Component::Query
6
6
  module URI
7
7
  module Component
8
8
 
9
- class TestQueryClass < Test::Unit::TestCase
9
+ class QueryTest < Test::Unit::TestCase
10
10
  def setup
11
11
  end
12
12
 
@@ -6,7 +6,7 @@ UCUI = URI::Component::UserInfo
6
6
  module URI
7
7
  module Component
8
8
 
9
- class TestUserInfoClass < Test::Unit::TestCase
9
+ class UserInfoTest < Test::Unit::TestCase
10
10
  def setup
11
11
  end
12
12
 
@@ -11,8 +11,6 @@ Gem::Specification.new do |s|
11
11
  s.summary = %q{URI::Component::* classes}
12
12
  s.description = %q{Handle URI components as an object}
13
13
 
14
- s.rubyforge_project = "uri-component"
15
-
16
14
  s.files = `git ls-files`.split("\n")
17
15
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
16
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
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.3
4
+ version: 0.0.4
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-05 00:00:00.000000000 Z
12
+ date: 2011-11-10 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Handle URI components as an object
15
15
  email:
@@ -48,7 +48,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
48
  - !ruby/object:Gem::Version
49
49
  version: '0'
50
50
  requirements: []
51
- rubyforge_project: uri-component
51
+ rubyforge_project:
52
52
  rubygems_version: 1.8.11
53
53
  signing_key:
54
54
  specification_version: 3