Module URITemplate
In: lib/uri_template.rb
lib/uri_template/colon.rb
lib/uri_template/utils.rb

The MIT License (MIT)

Copyright (c) 2011-2014 Hannes Georg

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Methods

+   /   ==   >>   absolute?   apply   coerce   coerce_first_arg   concat   eq   expand   expand_partial   host?   new   path_concat   pattern   relative?   resolve_class   scheme?   static_characters   to_s   tokens   type   variables  

Classes and Modules

Module URITemplate::ClassMethods
Module URITemplate::Expression
Module URITemplate::Invalid
Module URITemplate::InvalidValue
Module URITemplate::Literal
Module URITemplate::Token
Module URITemplate::Utils
Class URITemplate::Colon
Class URITemplate::RFC6570
Class URITemplate::RegexpEnumerator
Class URITemplate::Unconvertable

Constants

SCHEME_REGEX = /\A[a-z]+:/i.freeze   @api private
HOST_REGEX = /\A(?:[a-z]+:)?\/\/[^\/]+/i.freeze   @api private
URI_SPLIT = /\A(?:([a-z]+):)?(?:\/\/)?([^\/]+)?/i.freeze   @api private
VERSIONS = { :rfc6570 => :RFC6570, :default => :RFC6570, :colon => :Colon, :latest => :RFC6570   A hash with all available implementations. @see resolve_class

Public Class methods

Applies a method to a URITemplate with another URITemplate as argument. This is a useful shorthand since both URITemplates are automatically coerced.

@example

  tpl = URITemplate.new('foo')
  URITemplate.apply( tpl, :/, 'bar' ).pattern #=> 'foo/bar'
  URITemplate.apply( 'baz', :/, tpl ).pattern #=> 'baz/foo'
  URITemplate.apply( 'bla', :/, 'blub' ).pattern #=> 'bla/blub'

Tries to coerce two URITemplates into a common representation. Returns an array with two {URITemplate}s and two booleans indicating which of the two were converted or raises an ArgumentError.

@example

  URITemplate.coerce( URITemplate.new(:rfc6570,'{x}'), '{y}' ) #=> [URITemplate.new(:rfc6570,'{x}'), URITemplate.new(:rfc6570,'{y}'), false, true]
  URITemplate.coerce( '{y}', URITemplate.new(:rfc6570,'{x}') ) #=> [URITemplate.new(:rfc6570,'{y}'), URITemplate.new(:rfc6570,'{x}'), true, false]

@return [Tuple<URITemplate,URITemplate,Bool,Bool>]

Creates an uri template using an implementation. The args should at least contain a pattern string. Symbols in the args are used to determine the actual implementation.

@example

  tpl = URITemplate.new('{x}') # a new template using the default implementation
  tpl.expand('x'=>'y') #=> 'y'

@example

  tpl = URITemplate.new(:colon,'/:x') # a new template using the colon implementation

Looks up which implementation to use. Extracts all symbols from args and looks up the first in {VERSIONS}.

@return Array an array of the class to use and the unused parameters.

@example

  URITemplate.resolve_class() #=> [ URITemplate::RFC6570, [] ]
  URITemplate.resolve_class(:colon) #=> [ URITemplate::Colon, [] ]
  URITemplate.resolve_class("template",:rfc6570) #=> [ URITemplate::RFC6570, ["template"] ]

@raise ArgumentError when no class was found.

Public Instance methods

+(other)

Alias for concat

/(other)

Alias for path_concat

==(other)

Alias for eq

>>(other)

Alias for concat

absolute?()

Alias for host?

Concatenate two template with conversion.

@example

  tpl = URITemplate::RFC6570.new('foo')
  (tpl + '{bar}' ).pattern #=> 'foo{bar}'

@param other [URITemplate, String, …] @return URITemplate

Compares two template patterns.

Expands this uri template with the given variables. The variables should be converted to strings using {Utils#object_to_param}.

The keys in the variables hash are converted to strings in order to support the Ruby 1.9 hash syntax.

If the variables are given as an array, they will be matched against the variables in the template based on their order.

@raise {Unconvertable} if a variable could not be converted to a string. @raise {InvalidValue} if a value is not suiteable for a certain variable ( e.g. a string when a list is expected ).

@param variables [map, Array] @return String

Works like expand with two differences:

 - the result is a uri template instead of string
 - undefined variables are left in the template

@see {expand} @param variables [map, Array] @return [URITemplate]

Returns whether this uri-template includes a host name

This method is usefull to check wheter this template will generate or match a uri with a host.

@see scheme?

@example

  URITemplate.new('/foo').host? #=> false
  URITemplate.new('//example.com/foo').host? #=> true
  URITemplate.new('//{host}/foo').host? #=> true
  URITemplate.new('http://example.com/foo').host? #=> true
  URITemplate.new('{scheme}://example.com/foo').host? #=> true

Tries to concatenate two templates, as if they were path segments. Removes double slashes or insert one if they are missing.

@example

  tpl = URITemplate::RFC6570.new('/xy/')
  (tpl / '/z/' ).pattern #=> '/xy/z/'
  (tpl / 'z/' ).pattern #=> '/xy/z/'
  (tpl / '{/z}' ).pattern #=> '/xy{/z}'
  (tpl / 'a' / 'b' ).pattern #=> '/xy/a/b'

@param other [URITemplate, String, …] @return URITemplate

Returns the pattern for this template.

@return String

Opposite of {absolute?}

Returns whether this uri-template includes a scheme

This method is usefull to check wheter this template will generate or match a uri with a scheme.

@see host?

@example

  URITemplate.new('/foo').scheme? #=> false
  URITemplate.new('//example.com/foo').scheme? #=> false
  URITemplate.new('http://example.com/foo').scheme? #=> true
  URITemplate.new('{scheme}://example.com/foo').scheme? #=> true

Returns the number of static characters in this template. This method is useful for routing, since it‘s often pointful to use the url with fewer variable characters. For example ‘static’ and ‘sta\{var\}’ both match ‘static’, but in most cases ‘static’ should be prefered over ‘sta\{var\}’ since it‘s more specific.

@example

  URITemplate.new('/xy/').static_characters #=> 4
  URITemplate.new('{foo}').static_characters #=> 0
  URITemplate.new('a{foo}b').static_characters #=> 2

@return Numeric

to_s()

Alias for pattern

@abstract Returns the tokens of this templates. Tokens should include either {Literal} or {Expression}.

@return [Array<URITemplate::Token>]

@abstract Returns the type of this template. The type is a symbol which can be used in {.resolve_class} to resolve the type of this template.

@return [Symbol]

Returns an array containing all variables. Repeated variables are ignored. The concrete order of the variables may change. @example

  URITemplate.new('{foo}{bar}{baz}').variables #=> ['foo','bar','baz']
  URITemplate.new('{a}{c}{a}{b}').variables #=> ['a','c','b']

@return [Array<String>]

[Validate]