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

VAR =
/(?:\{:([a-z]+)\}|:([a-z]+)(?![a-z])|\*)/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, convert, #eq, #expand, #host?, new, #path_concat, #relative?, resolve_class, #scheme?, #static_characters, #variables

Constructor Details

- (Colon) initialize(pattern)

A new instance of Colon

Raises:

  • (ArgumentError)


154
155
156
157
# File 'lib/uri_template/colon.rb', line 154

def initialize(pattern)
  raise ArgumentError,"Expected a String but got #{pattern.inspect}" unless pattern.kind_of? String
  @pattern = pattern
end

Instance Attribute Details

- (Object) pattern (readonly)

Returns the value of attribute pattern



136
137
138
# File 'lib/uri_template/colon.rb', line 136

def pattern
  @pattern
end

Class Method Details

+ (Object) try_convert(x)

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}/'


142
143
144
145
146
147
148
149
150
151
152
# File 'lib/uri_template/colon.rb', line 142

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

- (Object) extract(uri)

Extracts variables from an uri.

Parameters:

  • uri (String)

Returns:

  • nil,Hash



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/uri_template/colon.rb', line 163

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

- (Object) to_r



186
187
188
# File 'lib/uri_template/colon.rb', line 186

def to_r
  @regexp ||= Regexp.new('\A' + tokens.map(&:to_r).join + '\z', Utils::KCODE_UTF8)
end

- (Object) tokens



190
191
192
# File 'lib/uri_template/colon.rb', line 190

def tokens
  @tokens ||= tokenize!
end

- (Object) type



182
183
184
# File 'lib/uri_template/colon.rb', line 182

def type
  :colon
end