addressable 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +4 -0
- data/README +21 -0
- data/coverage/index.html +48 -5
- data/coverage/lib-addressable-uri_rb.html +1154 -1071
- data/coverage/lib-addressable-version_rb.html +663 -0
- data/doc/classes/Addressable/URI.html +1246 -1142
- data/doc/classes/Addressable/URI/IDNA.html +34 -34
- data/doc/created.rid +1 -1
- data/doc/files/CHANGELOG.html +7 -2
- data/doc/files/LICENSE.html +1 -1
- data/doc/files/README.html +24 -1
- data/doc/files/lib/addressable/uri_rb.html +2 -1
- data/doc/files/lib/addressable/version_rb.html +1 -1
- data/doc/fr_method_index.html +53 -52
- data/lib/addressable/uri.rb +84 -1
- data/lib/addressable/version.rb +1 -1
- data/spec/addressable/uri_spec.rb +67 -0
- data/specdoc/index.html +348 -325
- metadata +2 -1
@@ -89,8 +89,8 @@ returning nil if libidn can‘t be used.
|
|
89
89
|
<h3 class="section-bar">Methods</h3>
|
90
90
|
|
91
91
|
<div class="name-list">
|
92
|
-
<a href="#
|
93
|
-
<a href="#
|
92
|
+
<a href="#M000055">to_ascii</a>
|
93
|
+
<a href="#M000056">to_unicode</a>
|
94
94
|
</div>
|
95
95
|
</div>
|
96
96
|
|
@@ -112,11 +112,11 @@ returning nil if libidn can‘t be used.
|
|
112
112
|
<div id="methods">
|
113
113
|
<h3 class="section-bar">Public Class methods</h3>
|
114
114
|
|
115
|
-
<div id="method-
|
116
|
-
<a name="
|
115
|
+
<div id="method-M000055" class="method-detail">
|
116
|
+
<a name="M000055"></a>
|
117
117
|
|
118
118
|
<div class="method-heading">
|
119
|
-
<a href="#
|
119
|
+
<a href="#M000055" class="method-signature">
|
120
120
|
<span class="method-name">to_ascii</span><span class="method-args">(label)</span>
|
121
121
|
</a>
|
122
122
|
</div>
|
@@ -126,30 +126,30 @@ returning nil if libidn can‘t be used.
|
|
126
126
|
Returns the ascii representation of the label.
|
127
127
|
</p>
|
128
128
|
<p><a class="source-toggle" href="#"
|
129
|
-
onclick="toggleCode('
|
130
|
-
<div class="method-source-code" id="
|
129
|
+
onclick="toggleCode('M000055-source');return false;">[Source]</a></p>
|
130
|
+
<div class="method-source-code" id="M000055-source">
|
131
131
|
<pre>
|
132
|
-
<span class="ruby-comment cmt"># File lib/addressable/uri.rb, line
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
132
|
+
<span class="ruby-comment cmt"># File lib/addressable/uri.rb, line 1074</span>
|
133
|
+
1074: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">to_ascii</span>(<span class="ruby-identifier">label</span>)
|
134
|
+
1075: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">label</span>.<span class="ruby-identifier">nil?</span>
|
135
|
+
1076: <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">use_libidn?</span>
|
136
|
+
1077: <span class="ruby-keyword kw">return</span> <span class="ruby-constant">IDN</span><span class="ruby-operator">::</span><span class="ruby-constant">Idna</span>.<span class="ruby-identifier">toASCII</span>(<span class="ruby-identifier">label</span>)
|
137
|
+
1078: <span class="ruby-keyword kw">else</span>
|
138
|
+
1079: <span class="ruby-identifier">raise</span> <span class="ruby-constant">NotImplementedError</span>,
|
139
|
+
1080: <span class="ruby-value str">"There is no available pure-ruby implementation. "</span> <span class="ruby-operator">+</span>
|
140
|
+
1081: <span class="ruby-value str">"Install libidn bindings."</span>
|
141
|
+
1082: <span class="ruby-keyword kw">end</span>
|
142
|
+
1083: <span class="ruby-keyword kw">end</span>
|
143
143
|
</pre>
|
144
144
|
</div>
|
145
145
|
</div>
|
146
146
|
</div>
|
147
147
|
|
148
|
-
<div id="method-
|
149
|
-
<a name="
|
148
|
+
<div id="method-M000056" class="method-detail">
|
149
|
+
<a name="M000056"></a>
|
150
150
|
|
151
151
|
<div class="method-heading">
|
152
|
-
<a href="#
|
152
|
+
<a href="#M000056" class="method-signature">
|
153
153
|
<span class="method-name">to_unicode</span><span class="method-args">(label)</span>
|
154
154
|
</a>
|
155
155
|
</div>
|
@@ -159,20 +159,20 @@ Returns the ascii representation of the label.
|
|
159
159
|
Returns the unicode representation of the label.
|
160
160
|
</p>
|
161
161
|
<p><a class="source-toggle" href="#"
|
162
|
-
onclick="toggleCode('
|
163
|
-
<div class="method-source-code" id="
|
162
|
+
onclick="toggleCode('M000056-source');return false;">[Source]</a></p>
|
163
|
+
<div class="method-source-code" id="M000056-source">
|
164
164
|
<pre>
|
165
|
-
<span class="ruby-comment cmt"># File lib/addressable/uri.rb, line
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
165
|
+
<span class="ruby-comment cmt"># File lib/addressable/uri.rb, line 1086</span>
|
166
|
+
1086: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">to_unicode</span>(<span class="ruby-identifier">label</span>)
|
167
|
+
1087: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">label</span>.<span class="ruby-identifier">nil?</span>
|
168
|
+
1088: <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">use_libidn?</span>
|
169
|
+
1089: <span class="ruby-keyword kw">return</span> <span class="ruby-constant">IDN</span><span class="ruby-operator">::</span><span class="ruby-constant">Idna</span>.<span class="ruby-identifier">toUnicode</span>(<span class="ruby-identifier">label</span>)
|
170
|
+
1090: <span class="ruby-keyword kw">else</span>
|
171
|
+
1091: <span class="ruby-identifier">raise</span> <span class="ruby-constant">NotImplementedError</span>,
|
172
|
+
1092: <span class="ruby-value str">"There is no available pure-ruby implementation. "</span> <span class="ruby-operator">+</span>
|
173
|
+
1093: <span class="ruby-value str">"Install libidn bindings."</span>
|
174
|
+
1094: <span class="ruby-keyword kw">end</span>
|
175
|
+
1095: <span class="ruby-keyword kw">end</span>
|
176
176
|
</pre>
|
177
177
|
</div>
|
178
178
|
</div>
|
data/doc/created.rid
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Wed Sep 05
|
1
|
+
Wed Sep 05 16:05:51 -0400 2007
|
data/doc/files/CHANGELOG.html
CHANGED
@@ -56,7 +56,7 @@
|
|
56
56
|
</tr>
|
57
57
|
<tr class="top-aligned-row">
|
58
58
|
<td><strong>Last Update:</strong></td>
|
59
|
-
<td>Wed Sep 05
|
59
|
+
<td>Wed Sep 05 13:34:16 -0400 2007</td>
|
60
60
|
</tr>
|
61
61
|
</table>
|
62
62
|
</div>
|
@@ -69,7 +69,12 @@
|
|
69
69
|
<div id="contextContent">
|
70
70
|
|
71
71
|
<div id="description">
|
72
|
-
<h3><a href="../classes/Addressable.html">Addressable</a> 0.1.
|
72
|
+
<h3><a href="../classes/Addressable.html">Addressable</a> 0.1.1</h3>
|
73
|
+
<pre>
|
74
|
+
* updated documentation
|
75
|
+
* added URI Template variable extraction
|
76
|
+
</pre>
|
77
|
+
<h3><a href="../classes/Addressable.html">Addressable</a> 0.1.0</h3>
|
73
78
|
<pre>
|
74
79
|
* initial release
|
75
80
|
* implementation based on RFC 3986, 3987
|
data/doc/files/LICENSE.html
CHANGED
data/doc/files/README.html
CHANGED
@@ -56,7 +56,7 @@
|
|
56
56
|
</tr>
|
57
57
|
<tr class="top-aligned-row">
|
58
58
|
<td><strong>Last Update:</strong></td>
|
59
|
-
<td>
|
59
|
+
<td>Wed Sep 05 13:10:21 -0400 2007</td>
|
60
60
|
</tr>
|
61
61
|
</table>
|
62
62
|
</div>
|
@@ -75,6 +75,29 @@ the URI implementation that is part of Ruby‘s standard library. It
|
|
75
75
|
more closely conforms to the relevant RFCs and adds support for IRIs and
|
76
76
|
URI templates.
|
77
77
|
</p>
|
78
|
+
<p>
|
79
|
+
Example usage:
|
80
|
+
</p>
|
81
|
+
<pre>
|
82
|
+
require 'addressable/uri'
|
83
|
+
|
84
|
+
uri = Addressable::URI.parse("http://example.com/path/to/resource/")
|
85
|
+
uri.scheme
|
86
|
+
=> "http"
|
87
|
+
uri.host
|
88
|
+
=> "example.com"
|
89
|
+
uri.path
|
90
|
+
=> "/path/to/resource/"
|
91
|
+
|
92
|
+
Addressable::URI.expand_template("http://example.com/{query}/", {
|
93
|
+
"query" => "an+example+query"
|
94
|
+
})
|
95
|
+
=> #<Addressable::URI:0x123456 URI:http://example.com/an+example+query/>
|
96
|
+
|
97
|
+
uri = Addressable::URI.parse("http://www.詹姆斯.com/")
|
98
|
+
uri.normalize
|
99
|
+
=> #<Addressable::URI:0x654321 URI:http://www.xn--8ws00zhy3a.com/>
|
100
|
+
</pre>
|
78
101
|
|
79
102
|
</div>
|
80
103
|
|
@@ -56,7 +56,7 @@
|
|
56
56
|
</tr>
|
57
57
|
<tr class="top-aligned-row">
|
58
58
|
<td><strong>Last Update:</strong></td>
|
59
|
-
<td>Wed Sep 05
|
59
|
+
<td>Wed Sep 05 16:01:22 -0400 2007</td>
|
60
60
|
</tr>
|
61
61
|
</table>
|
62
62
|
</div>
|
@@ -73,6 +73,7 @@
|
|
73
73
|
<h3 class="section-bar">Required files</h3>
|
74
74
|
|
75
75
|
<div class="name-list">
|
76
|
+
addressable/version
|
76
77
|
rubygems
|
77
78
|
idn
|
78
79
|
</div>
|
data/doc/fr_method_index.html
CHANGED
@@ -20,61 +20,62 @@
|
|
20
20
|
<div id="index">
|
21
21
|
<h1 class="section-bar">Methods</h1>
|
22
22
|
<div id="index-entries">
|
23
|
-
<a href="classes/Addressable/URI.html#
|
24
|
-
<a href="classes/Addressable/URI.html#
|
25
|
-
<a href="classes/Addressable/URI.html#
|
26
|
-
<a href="classes/Addressable/URI.html#
|
27
|
-
<a href="classes/Addressable/URI.html#
|
28
|
-
<a href="classes/Addressable/URI.html#
|
29
|
-
<a href="classes/Addressable/URI.html#
|
23
|
+
<a href="classes/Addressable/URI.html#M000040">+ (Addressable::URI)</a><br />
|
24
|
+
<a href="classes/Addressable/URI.html#M000049">== (Addressable::URI)</a><br />
|
25
|
+
<a href="classes/Addressable/URI.html#M000048">=== (Addressable::URI)</a><br />
|
26
|
+
<a href="classes/Addressable/URI.html#M000039">absolute? (Addressable::URI)</a><br />
|
27
|
+
<a href="classes/Addressable/URI.html#M000022">authority (Addressable::URI)</a><br />
|
28
|
+
<a href="classes/Addressable/URI.html#M000023">authority= (Addressable::URI)</a><br />
|
29
|
+
<a href="classes/Addressable/URI.html#M000031">basename (Addressable::URI)</a><br />
|
30
30
|
<a href="classes/Addressable/URI.html#M000002">convert_path (Addressable::URI)</a><br />
|
31
|
-
<a href="classes/Addressable/URI.html#
|
32
|
-
<a href="classes/Addressable/URI.html#
|
33
|
-
<a href="classes/Addressable/URI.html#
|
34
|
-
<a href="classes/Addressable/URI.html#
|
35
|
-
<a href="classes/Addressable/URI.html#
|
31
|
+
<a href="classes/Addressable/URI.html#M000047">display_uri (Addressable::URI)</a><br />
|
32
|
+
<a href="classes/Addressable/URI.html#M000051">dup (Addressable::URI)</a><br />
|
33
|
+
<a href="classes/Addressable/URI.html#M000008">encode (Addressable::URI)</a><br />
|
34
|
+
<a href="classes/Addressable/URI.html#M000006">encode_segment (Addressable::URI)</a><br />
|
35
|
+
<a href="classes/Addressable/URI.html#M000050">eql? (Addressable::URI)</a><br />
|
36
36
|
<a href="classes/Addressable/URI.html#M000003">expand_template (Addressable::URI)</a><br />
|
37
|
-
<a href="classes/Addressable/URI.html#
|
38
|
-
<a href="classes/Addressable/URI.html#
|
39
|
-
<a href="classes/Addressable/URI.html#
|
40
|
-
<a href="classes/Addressable/URI.html#M000035">fragment
|
41
|
-
<a href="classes/Addressable/URI.html#
|
42
|
-
<a href="classes/Addressable/URI.html#M000020">host
|
43
|
-
<a href="classes/Addressable/URI.html#
|
44
|
-
<a href="classes/Addressable/URI.html#
|
45
|
-
<a href="classes/Addressable/URI.html#
|
46
|
-
<a href="classes/Addressable/URI.html#
|
47
|
-
<a href="classes/Addressable/URI.html#
|
48
|
-
<a href="classes/Addressable/URI.html#M000041">merge
|
49
|
-
<a href="classes/Addressable/URI.html#
|
50
|
-
<a href="classes/Addressable/URI.html#
|
51
|
-
<a href="classes/Addressable/URI.html#M000045">normalize
|
52
|
-
<a href="classes/Addressable/URI.html#
|
37
|
+
<a href="classes/Addressable/URI.html#M000032">extname (Addressable::URI)</a><br />
|
38
|
+
<a href="classes/Addressable/URI.html#M000010">extract (Addressable::URI)</a><br />
|
39
|
+
<a href="classes/Addressable/URI.html#M000004">extract_mapping (Addressable::URI)</a><br />
|
40
|
+
<a href="classes/Addressable/URI.html#M000035">fragment (Addressable::URI)</a><br />
|
41
|
+
<a href="classes/Addressable/URI.html#M000036">fragment= (Addressable::URI)</a><br />
|
42
|
+
<a href="classes/Addressable/URI.html#M000020">host (Addressable::URI)</a><br />
|
43
|
+
<a href="classes/Addressable/URI.html#M000021">host= (Addressable::URI)</a><br />
|
44
|
+
<a href="classes/Addressable/URI.html#M000054">inspect (Addressable::URI)</a><br />
|
45
|
+
<a href="classes/Addressable/URI.html#M000037">ip_based? (Addressable::URI)</a><br />
|
46
|
+
<a href="classes/Addressable/URI.html#M000024">ip_based_schemes (Addressable::URI)</a><br />
|
47
|
+
<a href="classes/Addressable/URI.html#M000005">join (Addressable::URI)</a><br />
|
48
|
+
<a href="classes/Addressable/URI.html#M000041">merge (Addressable::URI)</a><br />
|
49
|
+
<a href="classes/Addressable/URI.html#M000042">merge! (Addressable::URI)</a><br />
|
50
|
+
<a href="classes/Addressable/URI.html#M000011">new (Addressable::URI)</a><br />
|
51
|
+
<a href="classes/Addressable/URI.html#M000045">normalize (Addressable::URI)</a><br />
|
52
|
+
<a href="classes/Addressable/URI.html#M000046">normalize! (Addressable::URI)</a><br />
|
53
|
+
<a href="classes/Addressable/URI.html#M000009">normalized_encode (Addressable::URI)</a><br />
|
53
54
|
<a href="classes/Addressable/URI.html#M000001">parse (Addressable::URI)</a><br />
|
54
|
-
<a href="classes/Addressable/URI.html#
|
55
|
-
<a href="classes/Addressable/URI.html#
|
56
|
-
<a href="classes/Addressable/URI.html#
|
57
|
-
<a href="classes/Addressable/URI.html#
|
58
|
-
<a href="classes/Addressable/URI.html#
|
59
|
-
<a href="classes/Addressable/URI.html#
|
60
|
-
<a href="classes/Addressable/URI.html#
|
61
|
-
<a href="classes/Addressable/URI.html#
|
62
|
-
<a href="classes/Addressable/URI.html#
|
63
|
-
<a href="classes/Addressable/URI.html#
|
64
|
-
<a href="classes/Addressable/URI.html#
|
65
|
-
<a href="classes/Addressable/URI.html#
|
66
|
-
<a href="classes/Addressable/URI.html#
|
67
|
-
<a href="classes/Addressable/URI.html#
|
68
|
-
<a href="classes/Addressable/URI.html#
|
69
|
-
<a href="classes/Addressable/URI/IDNA.html#
|
70
|
-
<a href="classes/Addressable/URI.html#
|
71
|
-
<a href="classes/Addressable/URI.html#
|
72
|
-
<a href="classes/Addressable/URI/IDNA.html#
|
73
|
-
<a href="classes/Addressable/URI.html#
|
74
|
-
<a href="classes/Addressable/URI.html#
|
75
|
-
<a href="classes/Addressable/URI.html#
|
76
|
-
<a href="classes/Addressable/URI.html#
|
77
|
-
<a href="classes/Addressable/URI.html#
|
55
|
+
<a href="classes/Addressable/URI.html#M000016">password (Addressable::URI)</a><br />
|
56
|
+
<a href="classes/Addressable/URI.html#M000017">password= (Addressable::URI)</a><br />
|
57
|
+
<a href="classes/Addressable/URI.html#M000029">path (Addressable::URI)</a><br />
|
58
|
+
<a href="classes/Addressable/URI.html#M000030">path= (Addressable::URI)</a><br />
|
59
|
+
<a href="classes/Addressable/URI.html#M000026">port (Addressable::URI)</a><br />
|
60
|
+
<a href="classes/Addressable/URI.html#M000027">port= (Addressable::URI)</a><br />
|
61
|
+
<a href="classes/Addressable/URI.html#M000033">query (Addressable::URI)</a><br />
|
62
|
+
<a href="classes/Addressable/URI.html#M000034">query= (Addressable::URI)</a><br />
|
63
|
+
<a href="classes/Addressable/URI.html#M000038">relative? (Addressable::URI)</a><br />
|
64
|
+
<a href="classes/Addressable/URI.html#M000043">route_from (Addressable::URI)</a><br />
|
65
|
+
<a href="classes/Addressable/URI.html#M000044">route_to (Addressable::URI)</a><br />
|
66
|
+
<a href="classes/Addressable/URI.html#M000012">scheme (Addressable::URI)</a><br />
|
67
|
+
<a href="classes/Addressable/URI.html#M000013">scheme= (Addressable::URI)</a><br />
|
68
|
+
<a href="classes/Addressable/URI.html#M000025">scheme_mapping (Addressable::URI)</a><br />
|
69
|
+
<a href="classes/Addressable/URI.html#M000028">specified_port (Addressable::URI)</a><br />
|
70
|
+
<a href="classes/Addressable/URI/IDNA.html#M000055">to_ascii (Addressable::URI::IDNA)</a><br />
|
71
|
+
<a href="classes/Addressable/URI.html#M000053">to_h (Addressable::URI)</a><br />
|
72
|
+
<a href="classes/Addressable/URI.html#M000052">to_s (Addressable::URI)</a><br />
|
73
|
+
<a href="classes/Addressable/URI/IDNA.html#M000056">to_unicode (Addressable::URI::IDNA)</a><br />
|
74
|
+
<a href="classes/Addressable/URI.html#M000007">unencode_segment (Addressable::URI)</a><br />
|
75
|
+
<a href="classes/Addressable/URI.html#M000014">user (Addressable::URI)</a><br />
|
76
|
+
<a href="classes/Addressable/URI.html#M000015">user= (Addressable::URI)</a><br />
|
77
|
+
<a href="classes/Addressable/URI.html#M000018">userinfo (Addressable::URI)</a><br />
|
78
|
+
<a href="classes/Addressable/URI.html#M000019">userinfo= (Addressable::URI)</a><br />
|
78
79
|
</div>
|
79
80
|
</div>
|
80
81
|
</body>
|
data/lib/addressable/uri.rb
CHANGED
@@ -21,6 +21,11 @@
|
|
21
21
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
22
|
#++
|
23
23
|
|
24
|
+
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '/..')))
|
25
|
+
$:.uniq!
|
26
|
+
|
27
|
+
require 'addressable/version'
|
28
|
+
|
24
29
|
module Addressable
|
25
30
|
# This is an implementation of a URI parser based on RFC 3986.
|
26
31
|
class URI
|
@@ -197,10 +202,88 @@ module Addressable
|
|
197
202
|
|
198
203
|
result.gsub!(/\{#{Regexp.escape(name)}\}/, transformed_value)
|
199
204
|
end
|
200
|
-
result.gsub!(
|
205
|
+
result.gsub!(
|
206
|
+
/\{[#{Addressable::URI::CharacterClasses::UNRESERVED}]+\}/, "")
|
201
207
|
return Addressable::URI.parse(result)
|
202
208
|
end
|
203
209
|
|
210
|
+
# Extracts a mapping from the URI using a URI Template pattern.
|
211
|
+
# Returns nil if the pattern doesn't match the URI.
|
212
|
+
#
|
213
|
+
# An optional processor object may be supplied. The object should
|
214
|
+
# respond to either the :restore or :match messages or both.
|
215
|
+
# The :restore method should take two parameters: :name and :value.
|
216
|
+
# The :restore method should reverse any transformations that have been
|
217
|
+
# performed on the value to ensure a valid URI. The :match method
|
218
|
+
# should take a single parameter: :name. The :match method should
|
219
|
+
# return a String containing a regular expression capture group for
|
220
|
+
# matching on that particular variable. The default value is ".*".
|
221
|
+
#
|
222
|
+
# An example:
|
223
|
+
#
|
224
|
+
# class ExampleProcessor
|
225
|
+
# def self.restore(name, value)
|
226
|
+
# return value.gsub(/\+/, " ") if name == "query"
|
227
|
+
# return value
|
228
|
+
# end
|
229
|
+
#
|
230
|
+
# def self.match(name)
|
231
|
+
# return ".*?" if name == "first"
|
232
|
+
# return ".*"
|
233
|
+
# end
|
234
|
+
# end
|
235
|
+
#
|
236
|
+
# uri = Addressable::URI.parse(
|
237
|
+
# "http://example.com/search/an+example+search+query/")
|
238
|
+
# uri.extract_mapping("http://example.com/search/{query}/",
|
239
|
+
# ExampleProcessor)
|
240
|
+
# => {"query" => "an example search query"}
|
241
|
+
#
|
242
|
+
# uri = Addressable::URI.parse(
|
243
|
+
# "http://example.com/a/b/c/")
|
244
|
+
# uri.extract_mapping("http://example.com/{first}/{second}/",
|
245
|
+
# ExampleProcessor)
|
246
|
+
# => {"first" => "a", "second" => "b/c"}
|
247
|
+
def extract_mapping(pattern, processor=nil)
|
248
|
+
mapping = {}
|
249
|
+
variable_regexp =
|
250
|
+
/\{([#{Addressable::URI::CharacterClasses::UNRESERVED}]+)\}/
|
251
|
+
variables = pattern.scan(variable_regexp).flatten
|
252
|
+
variables.each { |v| mapping[v] = "" }
|
253
|
+
escaped_pattern =
|
254
|
+
Regexp.escape(pattern).gsub(/\\\{/, "{").gsub(/\\\}/, "}")
|
255
|
+
regexp = Regexp.new(escaped_pattern.gsub(variable_regexp) do |v|
|
256
|
+
capture_group = "(.*)"
|
257
|
+
|
258
|
+
if processor != nil
|
259
|
+
if processor.respond_to?(:match)
|
260
|
+
name = v.scan(variable_regexp).flatten[0]
|
261
|
+
capture_group = "(#{processor.match(name)})"
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
capture_group
|
266
|
+
end)
|
267
|
+
values = self.to_s.scan(regexp).flatten
|
268
|
+
if variables.size == values.size
|
269
|
+
for i in 0...variables.size
|
270
|
+
name = variables[i]
|
271
|
+
value = values[i]
|
272
|
+
|
273
|
+
if processor != nil
|
274
|
+
if processor.respond_to?(:restore)
|
275
|
+
value = processor.restore(name, value)
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
mapping[name] = value
|
280
|
+
end
|
281
|
+
return mapping
|
282
|
+
else
|
283
|
+
return nil
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
204
287
|
# Joins several uris together.
|
205
288
|
def self.join(*uris)
|
206
289
|
uri_objects = uris.collect do |uri|
|
data/lib/addressable/version.rb
CHANGED
@@ -36,6 +36,16 @@ class ExampleProcessor
|
|
36
36
|
return value.gsub(/ /, "+") if name == "query"
|
37
37
|
return value
|
38
38
|
end
|
39
|
+
|
40
|
+
def self.restore(name, value)
|
41
|
+
return value.gsub(/\+/, " ") if name == "query"
|
42
|
+
return value
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.match(name)
|
46
|
+
return ".*?" if name == "first"
|
47
|
+
return ".*"
|
48
|
+
end
|
39
49
|
end
|
40
50
|
|
41
51
|
context "A completely nil URI" do
|
@@ -2276,3 +2286,60 @@ context "A mapping that contains values that are already percent-encoded" do
|
|
2276
2286
|
@mapping).to_s.should == "http://example.com/%257Bb%257D/"
|
2277
2287
|
end
|
2278
2288
|
end
|
2289
|
+
|
2290
|
+
context "http://example.com/search/an+example+search+query/" do
|
2291
|
+
setup do
|
2292
|
+
@uri = Addressable::URI.parse(
|
2293
|
+
"http://example.com/search/an+example+search+query/")
|
2294
|
+
end
|
2295
|
+
|
2296
|
+
specify "when extracting using the pattern " +
|
2297
|
+
"'http://example.com/search/{query}/' with the " +
|
2298
|
+
"ExampleProcessor to extract values should have the correct mapping" do
|
2299
|
+
@uri.extract_mapping(
|
2300
|
+
"http://example.com/search/{query}/", ExampleProcessor
|
2301
|
+
).should == {
|
2302
|
+
"query" => "an example search query"
|
2303
|
+
}
|
2304
|
+
end
|
2305
|
+
|
2306
|
+
specify "when extracting using a non-matching pattern should return nil" do
|
2307
|
+
@uri.extract_mapping(
|
2308
|
+
"http://bogus.com/{thingy}/"
|
2309
|
+
).should == nil
|
2310
|
+
end
|
2311
|
+
end
|
2312
|
+
|
2313
|
+
context "http://example.com/a/b/c/" do
|
2314
|
+
setup do
|
2315
|
+
@uri = Addressable::URI.parse(
|
2316
|
+
"http://example.com/a/b/c/")
|
2317
|
+
end
|
2318
|
+
|
2319
|
+
specify "when extracting using the pattern " +
|
2320
|
+
"'http://example.com/{first}/{second}/' with the " +
|
2321
|
+
"ExampleProcessor to extract values should have the correct mapping" do
|
2322
|
+
@uri.extract_mapping(
|
2323
|
+
"http://example.com/{first}/{second}/", ExampleProcessor
|
2324
|
+
).should == {
|
2325
|
+
"first" => "a",
|
2326
|
+
"second" => "b/c"
|
2327
|
+
}
|
2328
|
+
end
|
2329
|
+
end
|
2330
|
+
|
2331
|
+
context "http://example.com/one/spacer/two/" do
|
2332
|
+
setup do
|
2333
|
+
@uri = Addressable::URI.parse("http://example.com/one/spacer/two/")
|
2334
|
+
end
|
2335
|
+
|
2336
|
+
specify "when extracting using the pattern " +
|
2337
|
+
"'http://example.com/{first}/spacer/{second}/' to extract values " +
|
2338
|
+
"should have the correct mapping" do
|
2339
|
+
@uri.extract_mapping(
|
2340
|
+
"http://example.com/{first}/spacer/{second}/").should == {
|
2341
|
+
"first" => "one",
|
2342
|
+
"second" => "two"
|
2343
|
+
}
|
2344
|
+
end
|
2345
|
+
end
|