Class: Kea::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/kea/router.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



7
8
# File 'lib/kea/router.rb', line 7

def initialize
end

Class Method Details

.do_routeObject



3
4
5
# File 'lib/kea/router.rb', line 3

def self.do_route
  new
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kea/router.rb', line 10

def call(env)
  @env = env

  request = Rack::Request.new(env)
  body = request.body.read

  params = nil

  if body.present?
    params = JSON.parse(body).with_indifferent_access
    request.body.rewind
  else
    # googlebot seems to have given us data in this format
    if env['rack.request.form_vars'].present?
      json_params = JSON.parse(env['rack.request.form_vars']) rescue nil
      params = json_params.with_indifferent_access if json_params.present?

      if params.blank? && env['rack.request.form_hash'].present?
        params = env['rack.request.form_hash'].with_indifferent_access
      end
    end
  end

  @env['kea.params'] = params.with_indifferent_access

  begin
    @endpoint = params[:endpoint].classify.constantize

    unless @endpoint.ancestors.include? Kea::Controller
      return render_error({endpoint: 'does not descend from Kea::Controller'})
    end
  rescue => e
    return render_error({exception: e.message})
  end

  @endpoint.action(:run_kea_action).call(env)
end

#render_error(error) ⇒ Object



48
49
50
51
# File 'lib/kea/router.rb', line 48

def render_error(error)
  @env['kea.error'] = error
  Kea::ErrorController.action(:render_kea_error).call(@env)
end