Class: Hibana::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/hibana/controller.rb

Constant Summary collapse

DEFAULT_RESPONSE_BODY =
''
DEFAULT_RESPONSE_STATUS =
200

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:) ⇒ Controller

Returns a new instance of Controller.

Parameters:

  • env (Hash{String => Object})


22
23
24
25
26
27
28
# File 'lib/hibana/controller.rb', line 22

def initialize(env:)
  @env = env
  @response = ::Rack::Response.new(
    DEFAULT_RESPONSE_BODY,
    DEFAULT_RESPONSE_STATUS
  )
end

Instance Attribute Details

#responseRack::Response (readonly)

Returns:

  • (Rack::Response)


19
20
21
# File 'lib/hibana/controller.rb', line 19

def response
  @response
end

Class Method Details

.call(env) ⇒ Array

Parameters:

  • env (Hash{String => Object})

Returns:

  • (Array)


11
12
13
14
15
# File 'lib/hibana/controller.rb', line 11

def call(env)
  controller = new(env: env)
  controller.call
  controller.to_rack_spec_response
end

Instance Method Details

#callObject

Raises:

  • (::NotImplementedError)


30
31
32
# File 'lib/hibana/controller.rb', line 30

def call
  raise ::NotImplementedError
end

#parametersHash{Symbol => String}

TODO:

Returns:

  • (Hash{Symbol => String})


41
42
43
# File 'lib/hibana/controller.rb', line 41

def parameters
  path_parameters
end

#path_parametersHash{Symbol => String}

Returns:

  • (Hash{Symbol => String})


35
36
37
# File 'lib/hibana/controller.rb', line 35

def path_parameters
  request.env['router.params']
end

#requestRack::Request

Returns:

  • (Rack::Request)


46
47
48
# File 'lib/hibana/controller.rb', line 46

def request
  @request ||= ::Rack::Request.new(@env)
end

#to_rack_spec_responseArray

Returns:

  • (Array)


52
53
54
55
56
57
58
# File 'lib/hibana/controller.rb', line 52

def to_rack_spec_response
  [
    @response.status,
    @response.headers,
    @response.body,
  ]
end