Class: URITemplate::Colon
- Inherits:
-
Object
- Object
- URITemplate::Colon
- Includes:
- URITemplate
- Defined in:
- lib/uri_template/colon.rb
Overview
A colon based template denotes variables with a colon.
This template type is somewhat compatible with sinatra.
Defined Under Namespace
Classes: InvalidValue, Token
Constant Summary collapse
- VAR =
/(?:\{:(\w+)\}|:(\w+)(?!\w)|\*)/u
Constants included from URITemplate
HOST_REGEX, SCHEME_REGEX, URI_SPLIT, VERSIONS
Instance Attribute Summary collapse
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
Class Method Summary collapse
-
.try_convert(x) ⇒ Object
Tries to convert the value into a colon-template.
Instance Method Summary collapse
-
#extract(uri) ⇒ Object
Extracts variables from an uri.
-
#initialize(pattern) ⇒ Colon
constructor
A new instance of Colon.
- #to_r ⇒ Object
- #tokens ⇒ Object
- #type ⇒ Object
Methods included from URITemplate
apply, coerce, coerce_first_arg, #concat, #eq, #expand, #expand_partial, #host?, new, #path_concat, #relative?, resolve_class, #scheme?, #static_characters, #variables
Methods included from ClassMethods
#convert, #included, #try_convert
Constructor Details
#initialize(pattern) ⇒ Colon
Returns a new instance of Colon.
160 161 162 163 |
# File 'lib/uri_template/colon.rb', line 160 def initialize(pattern) raise ArgumentError,"Expected a String but got #{pattern.inspect}" unless pattern.kind_of? String @pattern = pattern end |
Instance Attribute Details
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
142 143 144 |
# File 'lib/uri_template/colon.rb', line 142 def pattern @pattern end |
Class Method Details
.try_convert(x) ⇒ Object
Tries to convert the value into a colon-template.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/uri_template/colon.rb', line 148 def self.try_convert(x) if x.kind_of? String return new(x) elsif x.kind_of? self return x elsif x.kind_of? URITemplate::RFC6570 and x.level == 1 return new( x.pattern.gsub(/\{(.*?)\}/u){ "{:#{$1}}" } ) else return nil end end |
Instance Method Details
#extract(uri) ⇒ Object
Extracts variables from an uri.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/uri_template/colon.rb', line 169 def extract(uri) md = self.to_r.match(uri) return nil unless md result = {} splat = [] self.tokens.select{|tk| tk.kind_of? URITemplate::Expression }.each_with_index do |tk,i| if tk.kind_of? Token::Splat splat << md[i+1] result['splat'] = splat unless result.key? 'splat' else result[tk.name] = Utils.unescape_url( md[i+1] ) end end if block_given? return yield(result) end return result end |
#to_r ⇒ Object
192 193 194 |
# File 'lib/uri_template/colon.rb', line 192 def to_r @regexp ||= Regexp.new('\A' + tokens.map(&:to_r).join + '\z', Utils::KCODE_UTF8) end |
#tokens ⇒ Object
196 197 198 |
# File 'lib/uri_template/colon.rb', line 196 def tokens @tokens ||= tokenize! end |
#type ⇒ Object
188 189 190 |
# File 'lib/uri_template/colon.rb', line 188 def type :colon end |