class RestlessRouter::Route

Public Class Methods

new(name, path, options={}) click to toggle source

Create a new Route that can be used to issue requests against.

@example

# With a fully qualified URI
route = RestlessRouter::Route.new('home', 'http://example.com')

route.name
# => 'home'

route.url_for
# => 'http://example.com'

# With a templated route
route = RestlessRouter::Route.new('search', 'http://example.com/search{?q}', templated: true)

route.name
# => 'search'

route.url_for(q: 'search-term')
# => 'http://example.com/search?q=search-term'
# File lib/restless_router/route.rb, line 29
def initialize(name, path, options={})
  @name = name
  @path = path

  @options = default_options.merge(options)
end

Public Instance Methods

<=>(other) click to toggle source

Define the spaceship operator for use with Comparable

# File lib/restless_router/route.rb, line 38
def <=>(other)
  self.name <=> other.name
end
name() click to toggle source

Return the name of the Route. This is either the IANA or custom link relationship.

@return [String] Name of the route

# File lib/restless_router/route.rb, line 46
def name
  @name
end
path() click to toggle source

Return the specified path of the Route. This is either a fully qualified URL or a URI Template.

@return [String] Path of the route

# File lib/restless_router/route.rb, line 54
def path
  @path
end
url_for(options={}) click to toggle source

Returns the URL for the route. If it’s templated, then we utilize the provided options hash to expand the route. Otherwise we return the ‘path`

@return [String] The templated or base URI

# File lib/restless_router/route.rb, line 63
def url_for(options={})
  if templated?
    template = Addressable::Template.new(base_path)
    template = template.expand(options)
    template.to_s
  else
    base_path
  end
end

Private Instance Methods

base_path() click to toggle source

The base path provided for the route

@return [String] The base path

# File lib/restless_router/route.rb, line 93
def base_path
  @path
end
default_options() click to toggle source

Provide a set of default options. Currently this is used to set ‘templated` to false.

# File lib/restless_router/route.rb, line 76
def default_options
  {
    :templated => false
  }
end
templated?() click to toggle source

Query method to see if the URI path provided is templated

@return [Boolean] True if the path is templated

# File lib/restless_router/route.rb, line 86
def templated?
  @options.fetch(:templated)
end