Class: Praxis::Response
- Inherits:
-
Object
- Object
- Praxis::Response
- Defined in:
- lib/praxis/response.rb
Direct Known Subclasses
Praxis::Responses::Accepted, Praxis::Responses::BadRequest, Praxis::Responses::Conflict, Praxis::Responses::Created, Praxis::Responses::Forbidden, Praxis::Responses::Found, Praxis::Responses::InternalServerError, Praxis::Responses::MethodNotAllowed, Praxis::Responses::MovedPermanently, Praxis::Responses::MultipleChoices, Praxis::Responses::NoContent, Praxis::Responses::NotAcceptable, Praxis::Responses::NotFound, Praxis::Responses::NotModified, Praxis::Responses::Ok, Praxis::Responses::PreconditionFailed, Praxis::Responses::RequestTimeout, Praxis::Responses::SeeOther, Praxis::Responses::TemporaryRedirect, Praxis::Responses::Unauthorized, Praxis::Responses::UnprocessableEntity
Class Attribute Summary collapse
-
.response_name ⇒ Object
Returns the value of attribute response_name.
-
.status ⇒ Object
Returns the value of attribute status.
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parts ⇒ Object
readonly
Returns the value of attribute parts.
-
#request ⇒ Object
Returns the value of attribute request.
-
#status ⇒ Object
Returns the value of attribute status.
Class Method Summary collapse
Instance Method Summary collapse
- #add_part(name = nil, part) ⇒ Object
-
#content_type ⇒ MediaTypeIdentifier
Determine the content type of this response.
-
#content_type=(identifier) ⇒ String
Set the content type for this response.
- #encode! ⇒ Object
- #finish ⇒ Object
- #format! ⇒ Object
- #handle ⇒ Object
-
#initialize(status: self.class.status, headers: {}, body: '') ⇒ Response
constructor
A new instance of Response.
- #response_name ⇒ Object
-
#validate(action) ⇒ Object
Validates the response.
Constructor Details
#initialize(status: self.class.status, headers: {}, body: '') ⇒ Response
Returns a new instance of Response.
21 22 23 24 25 26 27 28 |
# File 'lib/praxis/response.rb', line 21 def initialize(status:self.class.status, headers:{}, body:'') @name = response_name @status = status @headers = headers @body = body @form_data = nil @parts = Hash.new end |
Class Attribute Details
.response_name ⇒ Object
Returns the value of attribute response_name.
12 13 14 |
# File 'lib/praxis/response.rb', line 12 def response_name @response_name end |
.status ⇒ Object
Returns the value of attribute status.
13 14 15 |
# File 'lib/praxis/response.rb', line 13 def status @status end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
8 9 10 |
# File 'lib/praxis/response.rb', line 8 def body @body end |
#headers ⇒ Object
Returns the value of attribute headers.
7 8 9 |
# File 'lib/praxis/response.rb', line 7 def headers @headers end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/praxis/response.rb', line 3 def name @name end |
#parts ⇒ Object (readonly)
Returns the value of attribute parts.
4 5 6 |
# File 'lib/praxis/response.rb', line 4 def parts @parts end |
#request ⇒ Object
Returns the value of attribute request.
9 10 11 |
# File 'lib/praxis/response.rb', line 9 def request @request end |
#status ⇒ Object
Returns the value of attribute status.
6 7 8 |
# File 'lib/praxis/response.rb', line 6 def status @status end |
Class Method Details
.inherited(klass) ⇒ Object
16 17 18 19 |
# File 'lib/praxis/response.rb', line 16 def self.inherited(klass) klass.response_name = klass.name.demodulize.underscore.to_sym klass.status = self.status if self.status end |
Instance Method Details
#add_part(name = nil, part) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/praxis/response.rb', line 49 def add_part(name=nil, part) @form_data ||= begin form = MIME::Multipart::FormData.new @headers.merge! form.headers.headers form end name ||= "part-#{part.object_id}" @parts[name.to_s] = part end |
#content_type ⇒ MediaTypeIdentifier
Determine the content type of this response.
33 34 35 |
# File 'lib/praxis/response.rb', line 33 def content_type MediaTypeIdentifier.load(headers['Content-Type']).freeze end |
#content_type=(identifier) ⇒ String
DRY this out (also used in Multipart::Part)
Set the content type for this response.
42 43 44 |
# File 'lib/praxis/response.rb', line 42 def content_type=(identifier) headers['Content-Type'] = MediaTypeIdentifier.load(identifier).to_s end |
#encode! ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/praxis/response.rb', line 68 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 |
#finish ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/praxis/response.rb', line 80 def finish format! encode! @body = Array(@body) if @form_data if @body.any? unless @body.last =~ /\n$/ @body << "\r\n" end end @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
65 66 |
# File 'lib/praxis/response.rb', line 65 def format! end |
#handle ⇒ Object
46 47 |
# File 'lib/praxis/response.rb', line 46 def handle end |
#response_name ⇒ Object
61 62 63 |
# File 'lib/praxis/response.rb', line 61 def response_name self.class.response_name end |
#validate(action) ⇒ Object
Validates the response
115 116 117 118 119 120 121 122 123 |
# File 'lib/praxis/response.rb', line 115 def validate(action) return if response_name == :validation_error unless ( response_definition = action.responses[response_name] ) raise ArgumentError, "Attempting to return a response with name #{response_name} " \ "but no response definition with that name can be found" end response_definition.validate(self) end |