Class: Sinatra::Browse::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/browse/route.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_method, path_info, description, declaration_maps = nil) ⇒ Route

Returns a new instance of Route.



16
17
18
19
20
21
# File 'lib/sinatra/browse/route.rb', line 16

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

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



8
9
10
# File 'lib/sinatra/browse/route.rb', line 8

def description
  @description
end

#matchObject (readonly)

Returns the value of attribute match.



7
8
9
# File 'lib/sinatra/browse/route.rb', line 7

def match
  @match
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/sinatra/browse/route.rb', line 6

def name
  @name
end

#param_declarationsObject (readonly)

Returns the value of attribute param_declarations.



5
6
7
# File 'lib/sinatra/browse/route.rb', line 5

def param_declarations
  @param_declarations
end

Class Method Details

.build_name(request_method, path_info) ⇒ Object

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.



12
13
14
# File 'lib/sinatra/browse/route.rb', line 12

def self.build_name(request_method, path_info)
  "#{request_method}  #{path_info}"
end

Instance Method Details

#delete_undefined(params, allowed) ⇒ Object



62
63
64
# File 'lib/sinatra/browse/route.rb', line 62

def delete_undefined(params, allowed)
  params.delete_if { |i| !(self.has_parameter?(i) || allowed.member?(i)) }
end

#has_parameter?(name) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/sinatra/browse/route.rb', line 35

def has_parameter?(name)
  @param_declarations.has_key?(name.to_sym)
end

#matches?(request_method, path_info) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/sinatra/browse/route.rb', line 31

def matches?(request_method, path_info)
  !! (build_name(request_method,path_info) =~ @match)
end

#process(params) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sinatra/browse/route.rb', line 39

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 = {}) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/sinatra/browse/route.rb', line 23

def to_hash(options = {})
  {
    route: @name,
    description: @description,
    parameters: @param_declarations.map { |name, pd| pd.to_hash(options) }
  }
end