Module: URITemplate::RFC6570::ClassMethods

Included in:
URITemplate::RFC6570
Defined in:
lib/uri_template/rfc6570.rb

Overview

The class methods for all rfc6570 templates.

Instance Method Summary collapse

Instance Method Details

#try_convert(x) ⇒ Object

Tries to convert the given param in to a instance of URITemplate::RFC6570 It basically passes thru instances of that class, parses strings and return nil on everything else.

Examples:

URITemplate::RFC6570.try_convert( Object.new ) #=> nil
tpl = URITemplate::RFC6570.new('{foo}')
URITemplate::RFC6570.try_convert( tpl ) #=> tpl
URITemplate::RFC6570.try_convert('{foo}') #=> tpl
URITemplate::RFC6570.try_convert(URITemplate.new(:colon, ':foo')) #=> tpl
# This pattern is invalid, so it wont be parsed:
URITemplate::RFC6570.try_convert('{foo') #=> nil


257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/uri_template/rfc6570.rb', line 257

def try_convert(x)
  if x.class == self
    return x
  elsif x.kind_of? String and valid? x
    return new(x)
  elsif x.kind_of? URITemplate::Colon
    return nil if x.tokens.any?{|tk| tk.kind_of? URITemplate::Colon::Token::Splat }
    return new( x.tokens.map{|tk|
      if tk.literal?
        Literal.new(tk.string)
      else
        Expression.new([[tk.variables.first, false, 0]])
      end
    })
  else
    return nil
  end
end

#valid?(pattern) ⇒ Boolean

Tests whether a given pattern is a valid template pattern.

Examples:

URITemplate::RFC6570.valid? 'foo' #=> true
URITemplate::RFC6570.valid? '{foo}' #=> true
URITemplate::RFC6570.valid? '{foo' #=> false

Returns:

  • (Boolean)


281
282
283
# File 'lib/uri_template/rfc6570.rb', line 281

def valid?(pattern)
  URI === pattern
end