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.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/etna/route.rb', line 9

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]
  @log_redact_keys = options[:log_redact_keys]
  @dont_log = options[:dont_log]
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.path(route, params = nil, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/etna/route.rb', line 41

def self.path(route, params=nil, &block)
  if params
    PARAM_TYPES.reduce(route) do |path,pat|
      path.gsub(pat) do
        params[$1.to_sym].split('/').map { |c| block_given? ? yield(c) : URI.encode_www_form_component(c) }.join('/')
      end
    end
  else
    route
  end
end

Instance Method Details

#call(app, request) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/etna/route.rb', line 66

def call(app, request)
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  try_yabeda(request)  do |tags|
    Yabeda.etna.visits.increment(tags)
  end

  begin
    process_call(app, request)
  ensure
    try_yabeda(request) do |tags|
      dur = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
      Yabeda.etna.response_time.measure(tags, dur)
    end
  end
end

#has_user_constraint?(constraint) ⇒ Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/etna/route.rb', line 167

def has_user_constraint?(constraint)
  @auth && @auth[:user] && @auth[:user][constraint]
end

#hash_user_email(email) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/etna/route.rb', line 83

def hash_user_email(email)
  secret = Etna::Application.instance.config(:user_hash_secret) || 'notsosecret'
  digest = email + secret + Date.today.to_s

  if @name
    digest += @name.to_s
  else
    digest += @route.to_s
  end

  Digest::MD5.hexdigest(digest)
end

#ignore_janus?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/etna/route.rb', line 163

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

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/etna/route.rb', line 30

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

#noauth?Boolean

the route does not require authorization

Returns:

  • (Boolean)


159
160
161
# File 'lib/etna/route.rb', line 159

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

#partsObject



57
58
59
60
61
62
63
64
# File 'lib/etna/route.rb', line 57

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

#path(params = nil, &block) ⇒ Object



53
54
55
# File 'lib/etna/route.rb', line 53

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

#process_call(app, request) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/etna/route.rb', line 123

def process_call(app, request)
  update_params(request)

  unless authorized?(request)
    if cc_available?(request)
      if request.content_type == 'application/json'
        return [ 403, { 'Content-Type' => 'application/json' }, [ { error: 'You are forbidden from performing this action, but you can visit the project home page and request access.' }.to_json ] ]
      else
        return cc_redirect(request)
      end
    end

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

  request.env['etna.redact_keys'] = @log_redact_keys
  request.env['etna.dont_log'] = @dont_log

  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']

    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

#to_hashObject



21
22
23
24
25
26
27
28
# File 'lib/etna/route.rb', line 21

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

#try_yabeda(request, &block) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/etna/route.rb', line 96

def try_yabeda(request, &block)
  if @action
    controller, action = @action.split('#')
  elsif @name
    controller = "none"
    action = @name
  else
    controller = "none"
    action = @route
  end

  params = request.env['rack.request.params']
  user = request.env['etna.user']
  user_hash = user ? hash_user_email(user.email) : 'unknown'
  project_name = "unknown"

  if params && (params.include?(:project_name) || params.include?('project_name'))
    project_name = params[:project_name] || params['project_name']
  end

  begin
    block.call({ controller: controller, action: action, user_hash: user_hash, project_name: project_name })
  rescue => e
    raise e unless Etna::Application.instance.environment == :production
  end
end