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::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
- #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.
23 24 25 26 27 28 29 30 |
# File 'lib/praxis/response.rb', line 23 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.
14 15 16 |
# File 'lib/praxis/response.rb', line 14 def response_name @response_name end |
.status ⇒ Object
Returns the value of attribute status.
15 16 17 |
# File 'lib/praxis/response.rb', line 15 def status @status end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
10 11 12 |
# File 'lib/praxis/response.rb', line 10 def body @body end |
#headers ⇒ Object
Returns the value of attribute headers.
9 10 11 |
# File 'lib/praxis/response.rb', line 9 def headers @headers end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/praxis/response.rb', line 5 def name @name end |
#parts ⇒ Object (readonly)
Returns the value of attribute parts.
6 7 8 |
# File 'lib/praxis/response.rb', line 6 def parts @parts end |
#request ⇒ Object
Returns the value of attribute request.
11 12 13 |
# File 'lib/praxis/response.rb', line 11 def request @request end |
#status ⇒ Object
Returns the value of attribute status.
8 9 10 |
# File 'lib/praxis/response.rb', line 8 def status @status end |
Class Method Details
.inherited(klass) ⇒ Object
18 19 20 21 |
# File 'lib/praxis/response.rb', line 18 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
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/praxis/response.rb', line 35 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 |
#encode! ⇒ Object
54 55 56 57 58 59 |
# File 'lib/praxis/response.rb', line 54 def encode! case @body when Hash, Array @body = JSON.pretty_generate(@body) end end |
#finish ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/praxis/response.rb', line 61 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
51 52 |
# File 'lib/praxis/response.rb', line 51 def format! end |
#handle ⇒ Object
32 33 |
# File 'lib/praxis/response.rb', line 32 def handle end |
#response_name ⇒ Object
47 48 49 |
# File 'lib/praxis/response.rb', line 47 def response_name self.class.response_name end |
#validate(action) ⇒ Object
Validates the response
96 97 98 99 100 101 102 103 |
# File 'lib/praxis/response.rb', line 96 def validate(action) 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 |