Class: URITemplate::Colon

Inherits:
Object
  • Object
show all
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.

Examples:

tpl = URITemplate::Colon.new('/foo/:bar')
tpl.extract('/foo/baz') #=> {'bar'=>'baz'}
tpl.expand('bar'=>'boom') #=> '/foo/boom'

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

Class Method Summary collapse

Instance Method Summary collapse

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.

Raises:

  • (ArgumentError)


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

#patternObject (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.

Examples:

URITemplate::Colon.try_convert('/foo/:bar/').pattern #=> '/foo/:bar/'
URITemplate::Colon.try_convert(URITemplate.new(:rfc6570, '/foo/{bar}/')).pattern #=> '/foo/{:bar}/'


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.

Parameters:

  • uri (String)

Returns:

  • nil,Hash



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_rObject



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

#tokensObject



196
197
198
# File 'lib/uri_template/colon.rb', line 196

def tokens
  @tokens ||= tokenize!
end

#typeObject



188
189
190
# File 'lib/uri_template/colon.rb', line 188

def type
  :colon
end