Class: Hyperdrive::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperdrive/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, resource) ⇒ Response

Returns a new instance of Response.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/hyperdrive/response.rb', line 7

def initialize(env, resource)
  @resource = resource
  @env = env
  @http_request_method = env['REQUEST_METHOD']

  unless request_method_supported?
    raise Errors::NotImplemented.new(http_request_method)
  end

  unless resource.request_method_allowed?(http_request_method)
    raise Errors::MethodNotAllowed.new(http_request_method)
  end

  @headers = default_headers
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



5
6
7
# File 'lib/hyperdrive/response.rb', line 5

def env
  @env
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/hyperdrive/response.rb', line 5

def headers
  @headers
end

#http_request_methodObject (readonly)

Returns the value of attribute http_request_method.



5
6
7
# File 'lib/hyperdrive/response.rb', line 5

def http_request_method
  @http_request_method
end

#resourceObject (readonly)

Returns the value of attribute resource.



5
6
7
# File 'lib/hyperdrive/response.rb', line 5

def resource
  @resource
end

Instance Method Details

#responseObject



23
24
25
26
27
# File 'lib/hyperdrive/response.rb', line 23

def response
  @headers.merge({ 'Content-Type' => 'text/plain' })
  status = (http_request_method == 'POST') ? 201 : 200
  ::Rack::Response.new(resource.request_handler(http_request_method).call(env), status, headers).finish
end