class Sinatra::Browse::Route

Attributes

description[R]
match[R]
name[R]
param_declarations[R]

Public Class Methods

build_name(request_method, path_info) click to toggle source

This is here because we’re using the name as the keys for the _browse_routes hash. We want to build it outside of this class for that.

# File lib/sinatra/browse/route.rb, line 11
def self.build_name(request_method, path_info)
  "#{request_method}  #{path_info}"
end
new(request_method, path_info, description, declaration_maps = nil) click to toggle source
# File lib/sinatra/browse/route.rb, line 15
def initialize(request_method, path_info, description, declaration_maps = nil)
  @name = build_name(request_method, path_info)
  @match = build_match(request_method, path_info)
  @description = description
  build_declarations(declaration_maps || {})
end

Public Instance Methods

delete_undefined(params, allowed) click to toggle source
# File lib/sinatra/browse/route.rb, line 61
def delete_undefined(params, allowed)
  params.delete_if { |i| !(self.has_parameter?(i) || allowed.member?(i)) }
end
has_parameter?(name) click to toggle source
# File lib/sinatra/browse/route.rb, line 34
def has_parameter?(name)
  @param_declarations.has_key?(name.to_sym)
end
matches?(request_method, path_info) click to toggle source
# File lib/sinatra/browse/route.rb, line 30
def matches?(request_method, path_info)
  !! (build_name(request_method,path_info) =~ @match)
end
process(params) click to toggle source
# File lib/sinatra/browse/route.rb, line 38
def process(params)
  @param_declarations.each do |name, pd|
    name = name.to_s # The params hash uses strings but declarations use symbols

    params[name] ||= pd.default if pd.default_set?

    # We specifically check for nil here since a boolean's default can be false
    if params[name].nil?
      return false, pd.build_error_hash(:required, nil) if pd.required?
      next
    end

    params[name] = pd.coerce(params[name])

    success, error_hash = pd.validate(params)
    return false, error_hash unless success

    params[name] = pd.transform(params[name])
  end

  true
end
to_hash(options = {}) click to toggle source
# File lib/sinatra/browse/route.rb, line 22
def to_hash(options = {})
  {
    route: @name,
    description: @description,
    parameters: @param_declarations.map { |name, pd| pd.to_hash(options) }
  }
end

Private Instance Methods

build_declarations(declaration_maps) click to toggle source
# File lib/sinatra/browse/route.rb, line 74
def build_declarations(declaration_maps)
  @param_declarations = {}

  declaration_maps.each do |name, map|
    type = map.delete(:type)
    type_class = Sinatra::Browse::ParameterTypes.const_get(type)

    #TODO: Unit test this error
    unless type_class.is_a?(Class) && type_class.ancestors.member?(ParameterType)
      raise Errors::UnknownParameterTypeError, type_class
    end

    @param_declarations[name] = type_class.new(name, map)
  end
end
build_match(request_method, path_info) click to toggle source
# File lib/sinatra/browse/route.rb, line 70
def build_match(request_method, path_info)
  /^#{request_method}\s\s#{path_info.gsub(/:[^\/]*/, '[^\/]*')}$/
end
build_name(request_method, path_info) click to toggle source
# File lib/sinatra/browse/route.rb, line 66
def build_name(request_method, path_info)
  self.class.build_name(request_method, path_info)
end