Class: Etna::Route

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

Constant Summary collapse

NAMED_PARAM =
/:([\w]+)/
GLOB_PARAM =
/\*([\w]+)$/
PARAM_TYPES =
[ NAMED_PARAM, GLOB_PARAM ]
UNSAFE =
/[^\-_.!~*'()a-zA-Z\d;\/?:@&=+$,]/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, route, options, &block) ⇒ Route

Returns a new instance of Route.



5
6
7
8
9
10
11
12
13
# File 'lib/etna/route.rb', line 5

def initialize(method, route, options, &block)
  @method = method
  @action = options[:action]
  @auth = options[:auth]
  @name = route_name(options)
  @route = route.gsub(/\A(?=[^\/])/, '/')
  @block = block
  @match_ext = options[:match_ext]
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/etna/route.rb', line 3

def name
  @name
end

Class Method Details

.path(route, params = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/etna/route.rb', line 35

def self.path(route, params=nil)
  if params
    PARAM_TYPES.reduce(route) do |path,pat|
      path.gsub(pat) do
       URI.encode( params[$1.to_sym], UNSAFE)
      end
    end
  else
    route
  end
end

Instance Method Details

#call(app, request) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/etna/route.rb', line 60

def call(app, request)
  update_params(request)

  unless authorized?(request)
    return [ 403, { 'Content-Type' => 'application/json' }, [ { error: 'You are forbidden from performing this action.' }.to_json ] ]
  end

  if @action
    controller, action = @action.split('#')
    controller_class = Kernel.const_get(
      :"#{controller.camel_case}Controller"
    )
    logger = request.env['etna.logger']
    user = request.env['etna.user']

    params = request.env['rack.request.params'].map do |key,value|
      [ key, redact(key, value) ]
    end.to_h

    logger.warn("User #{user ? user.email : :unknown} calling #{controller}##{action} with params #{params}")
    return controller_class.new(request, action).response
  elsif @block
    application = Etna::Application.find(app.class).class
    controller_class = application.const_defined?(:Controller) ? application.const_get(:Controller) : Etna::Controller

    controller_class.new(request).response(&@block)
  end
end

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/etna/route.rb', line 24

def matches?(request)
  @method == request.request_method && request.path.match(route_regexp)
end

#noauth?Boolean

the route does not require authorization

Returns:

  • (Boolean)


90
91
92
# File 'lib/etna/route.rb', line 90

def noauth?
  @auth && @auth[:noauth]
end

#partsObject



51
52
53
54
55
56
57
58
# File 'lib/etna/route.rb', line 51

def parts
  part_list = PARAM_TYPES.map do |pat|
    "(?:#{pat.source})"
  end
  @route.scan(
    /(?:#{part_list.join('|')})/
  ).flatten.compact
end

#path(params = nil) ⇒ Object



47
48
49
# File 'lib/etna/route.rb', line 47

def path(params=nil)
  self.class.path(@route, params)
end

#to_hashObject



15
16
17
18
19
20
21
22
# File 'lib/etna/route.rb', line 15

def to_hash
  {
    method: @method,
    route: @route,
    name: @name.to_s,
    params: parts
  }.compact
end