uri_template 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 +3 -0
- data/lib/uri_template.rb +72 -0
- data/lib/uri_template/draft7.rb +10 -4
- data/uri_template.gemspec +2 -2
- metadata +6 -8
- data/lib/uri_template/colon.rb +0 -0
- data/lib/uri_template/draft2.rb +0 -0
data/CHANGELOG
CHANGED
data/lib/uri_template.rb
CHANGED
@@ -64,6 +64,78 @@ module URITemplate
|
|
64
64
|
return klass.new(*rest)
|
65
65
|
end
|
66
66
|
|
67
|
+
# Tries to convert the given argument into an {URITemplate}.
|
68
|
+
# Returns nil if this fails.
|
69
|
+
#
|
70
|
+
# @return [nil|{URITemplate}]
|
71
|
+
def self.try_convert(x)
|
72
|
+
if x.kind_of? URITemplate
|
73
|
+
return x
|
74
|
+
elsif x.kind_of? String
|
75
|
+
return URITemplate.new(x)
|
76
|
+
else
|
77
|
+
return nil
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# Same as {.try_convert} but raises an ArgumentError when the given argument could not be converted.
|
82
|
+
#
|
83
|
+
# @raise ArgumentError if the argument is unconvertable
|
84
|
+
# @return {URITemplate}
|
85
|
+
def self.convert(x)
|
86
|
+
o = self.try_convert(x)
|
87
|
+
if o.nil?
|
88
|
+
raise ArgumentError, "Expected to receive something that can be converted to an URITemplate, but got #{x.inspect}"
|
89
|
+
end
|
90
|
+
return o
|
91
|
+
end
|
92
|
+
|
93
|
+
# Tries to coerce two URITemplates into a common representation.
|
94
|
+
# Returns an array with two {URITemplate}s and two booleans indicating which of the two were converted or raises an {ArgumentError}.
|
95
|
+
#
|
96
|
+
# @example
|
97
|
+
# URITemplate.coerce( URITemplate.new(:draft7,'{x}'), '{y}' ) #=> [URITemplate.new(:draft7,'{x}'), URITemplate.new(:draft7,'{y}'), false, true]
|
98
|
+
def self.coerce(a,b)
|
99
|
+
if a.kind_of? URITemplate
|
100
|
+
if a.class == b.class
|
101
|
+
return [a,b,false,false]
|
102
|
+
end
|
103
|
+
b_as_a = a.class.try_convert(b)
|
104
|
+
if b_as_a
|
105
|
+
return [a,b_as_a,false,true]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
if b.kind_of? URITemplate
|
109
|
+
a_as_b = b.class.try_convert(a)
|
110
|
+
if a_as_b
|
111
|
+
return [a_as_b, b, true, false]
|
112
|
+
end
|
113
|
+
end
|
114
|
+
bc = self.try_convert(b)
|
115
|
+
if bc.kind_of? URITemplate
|
116
|
+
a_as_b = bc.class.try_convert(a)
|
117
|
+
if a_as_b
|
118
|
+
return [a_as_b, bc, true, true]
|
119
|
+
end
|
120
|
+
end
|
121
|
+
raise ArgumentError, "Expected at least on URITemplate, but got #{a.inspect} and #{b.inspect}" unless a.kind_of? URITemplate or b.kind_of? URITemplate
|
122
|
+
raise ArgumentError, "Cannot coerce #{a.inspect} and #{b.inspect} into a common representation."
|
123
|
+
end
|
124
|
+
|
125
|
+
# Applies a method to a URITemplate with another URITemplate as argument.
|
126
|
+
# This is a useful shorthand since both URITemplates are automatically coerced.
|
127
|
+
#
|
128
|
+
# @example
|
129
|
+
# tpl = URITemplate.new('foo')
|
130
|
+
# URITemplate.apply( tpl, :/, 'bar' ).pattern #=> 'foo/bar'
|
131
|
+
# URITemplate.apply( 'baz', :/, tpl ).pattern #=> 'baz/foo'
|
132
|
+
# URITemplate.apply( 'bla', :/, 'blub' ).pattern #=> 'bla/blub'
|
133
|
+
#
|
134
|
+
def self.apply(a, method, b, *args)
|
135
|
+
a,b,_,_ = self.coerce(a,b)
|
136
|
+
a.send(method,b,*args)
|
137
|
+
end
|
138
|
+
|
67
139
|
# A base class for all errors which will be raised upon invalid syntax.
|
68
140
|
module Invalid
|
69
141
|
end
|
data/lib/uri_template/draft7.rb
CHANGED
@@ -749,9 +749,12 @@ __REGEXP__
|
|
749
749
|
alias to_s pattern
|
750
750
|
|
751
751
|
# Compares two template patterns.
|
752
|
-
def ==(
|
753
|
-
|
754
|
-
|
752
|
+
def ==(o)
|
753
|
+
this, other, this_converted, other_converted = URITemplate.coerce( self, o )
|
754
|
+
if this_converted
|
755
|
+
return this == other
|
756
|
+
end
|
757
|
+
return this.pattern == other.pattern
|
755
758
|
end
|
756
759
|
|
757
760
|
# @method ===(uri)
|
@@ -811,7 +814,10 @@ __REGEXP__
|
|
811
814
|
# (tpl / 'a' / 'b' ).pattern #=> '/xy/a/b'
|
812
815
|
#
|
813
816
|
def /(o)
|
814
|
-
other =
|
817
|
+
this, other, this_converted, other_converted = URITemplate.coerce( self, o )
|
818
|
+
if this_converted
|
819
|
+
return this / other
|
820
|
+
end
|
815
821
|
|
816
822
|
if other.absolute?
|
817
823
|
raise ArgumentError, "Expected to receive a relative template but got an absoulte one: #{other.inspect}. If you think this is a bug, please report it."
|
data/uri_template.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uri_template
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-11-
|
12
|
+
date: 2011-11-04 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &17225100 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *17225100
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: yard
|
27
|
-
requirement: &
|
27
|
+
requirement: &17224520 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *17224520
|
36
36
|
description: ! 'A templating system for URIs, which implements http://tools.ietf.org/html/draft-gregorio-uritemplate-07
|
37
37
|
. An implementation of an older version of that spec is known as addressable. This
|
38
38
|
gem however is intended to be extended when newer specs evolve. For now only draft
|
@@ -43,8 +43,6 @@ extensions: []
|
|
43
43
|
extra_rdoc_files: []
|
44
44
|
files:
|
45
45
|
- lib/uri_template.rb
|
46
|
-
- lib/uri_template/colon.rb
|
47
|
-
- lib/uri_template/draft2.rb
|
48
46
|
- lib/uri_template/draft7.rb
|
49
47
|
- lib/uri_template/utils.rb
|
50
48
|
- uri_template.gemspec
|
data/lib/uri_template/colon.rb
DELETED
File without changes
|
data/lib/uri_template/draft2.rb
DELETED
File without changes
|