module URITemplate::RFC6570::ClassMethods

The class methods for all rfc6570 templates.

Public Instance Methods

try_convert(x) click to toggle source

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

@example

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
# File lib/uri_template/rfc6570.rb, line 256
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) click to toggle source

Tests whether a given pattern is a valid template pattern. @example

URITemplate::RFC6570.valid? 'foo' #=> true
URITemplate::RFC6570.valid? '{foo}' #=> true
URITemplate::RFC6570.valid? '{foo' #=> false
# File lib/uri_template/rfc6570.rb, line 280
def valid?(pattern)
  URI === pattern
end