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

- (Object) try_convert(x)

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


228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/uri_template/rfc6570.rb', line 228

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

- (Boolean) valid?(pattern)

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)


252
253
254
# File 'lib/uri_template/rfc6570.rb', line 252

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