Class: Praxis::Response

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status: self.class.status, headers: {}, body: nil, location: nil) ⇒ Response

Returns a new instance of Response.



18
19
20
21
22
23
24
25
26
# File 'lib/praxis/response.rb', line 18

def initialize(status: self.class.status, headers: {}, body: nil, location: nil)
  @name    = response_name
  @status  = status
  @headers = headers
  @body    = body
  @headers['Location'] = location if location
  @form_data = nil
  @parts = {}
end

Class Attribute Details

.response_nameObject

Returns the value of attribute response_name.



10
11
12
# File 'lib/praxis/response.rb', line 10

def response_name
  @response_name
end

.statusObject

Returns the value of attribute status.



10
11
12
# File 'lib/praxis/response.rb', line 10

def status
  @status
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



7
8
9
# File 'lib/praxis/response.rb', line 7

def body
  @body
end

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/praxis/response.rb', line 7

def headers
  @headers
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#partsObject (readonly)

Returns the value of attribute parts.



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

def parts
  @parts
end

#requestObject

Returns the value of attribute request.



7
8
9
# File 'lib/praxis/response.rb', line 7

def request
  @request
end

#statusObject

Returns the value of attribute status.



7
8
9
# File 'lib/praxis/response.rb', line 7

def status
  @status
end

Class Method Details

.inherited(klass) ⇒ Object



13
14
15
16
# File 'lib/praxis/response.rb', line 13

def self.inherited(klass)
  klass.response_name = klass.name.demodulize.underscore.to_sym
  klass.status = status if status
end

Instance Method Details

#add_part(part, name = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/praxis/response.rb', line 46

def add_part(part, name = nil)
  @form_data ||= begin
    form = MIME::Multipart::FormData.new
    @headers.merge! form.headers.headers
    form
  end

  name ||= "part-#{part.object_id}"
  part.name = name
  @parts[name.to_s] = part
end

#content_typeMediaTypeIdentifier

Determine the content type of this response.

Returns:



31
32
33
# File 'lib/praxis/response.rb', line 31

def content_type
  MediaTypeIdentifier.load(headers['Content-Type']).freeze
end

#content_type=(identifier) ⇒ String

TODO:

DRY this out (also used in Multipart::Part)

Set the content type for this response.

Parameters:

Returns:

  • (String)


40
41
42
# File 'lib/praxis/response.rb', line 40

def content_type=(identifier)
  headers['Content-Type'] = MediaTypeIdentifier.load(identifier).to_s
end

#encode!Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/praxis/response.rb', line 64

def encode!
  case @body
  when Hash, Array
    # response payload is structured data; transform it into an entity using the handler
    # implied by the response's media type. If no handler is registered for this
    # name, assume JSON as a default handler.
    handlers = Praxis::Application.instance.handlers
    handler = (content_type && handlers[content_type.handler_name]) || handlers['json']
    @body = handler.generate(@body)
  end
end

#finishObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/praxis/response.rb', line 76

def finish
  format!
  encode!

  @body = Array(@body)

  if @form_data
    @body << "\r\n" if @body.any? && @body.last !~ /\n$/

    @parts.each do |name, part|
      part.encode!
      entity = MIME::Text.new(part.body)

      part.headers.each do |header_name, header_value|
        entity.headers.set header_name, header_value
      end

      @form_data.add entity, name
    end

    @body << @form_data.body.to_s
  end

  [@status, @headers, @body]
end

#format!Object



62
# File 'lib/praxis/response.rb', line 62

def format!; end

#handleObject



44
# File 'lib/praxis/response.rb', line 44

def handle; end

#response_nameObject



58
59
60
# File 'lib/praxis/response.rb', line 58

def response_name
  self.class.response_name
end

#validate(action, validate_body: false) ⇒ Object

Validates the response

Parameters:

  • action (Object)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/praxis/response.rb', line 106

def validate(action, validate_body: false)
  return if response_name == :validation_error

  unless (response_definition = action.responses[response_name])
    raise Exceptions::Validation, "Attempting to return a response with name #{response_name} " \
      'but no response definition with that name can be found'
  end
  response_definition.validate(self, validate_body: validate_body)
rescue Exceptions::Validation => e
  ve = Application.instance.validation_handler.handle!(
    summary: 'Error validating response',
    exception: e,
    request: request,
    stage: 'response',
    errors: e.errors
  )
  body = ve.format!

  Responses::InternalServerError.new(body: body)
end