Class: JSONAPIonify::Api::Response
- Inherits:
-
Object
- Object
- JSONAPIonify::Api::Response
- Defined in:
- lib/jsonapionify/api/response.rb
Instance Attribute Summary collapse
-
#accept ⇒ Object
readonly
Returns the value of attribute accept.
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#example_accept ⇒ Object
readonly
Returns the value of attribute example_accept.
-
#extension ⇒ Object
readonly
Returns the value of attribute extension.
-
#matcher ⇒ Object
readonly
Returns the value of attribute matcher.
-
#response_block ⇒ Object
readonly
Returns the value of attribute response_block.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #accept_with_header?(accept:, extension:) ⇒ Boolean
- #accept_with_matcher?(context) ⇒ Boolean
- #cacheable ⇒ Object
- #call(instance, context, status: nil) ⇒ Object
- #documentation_object ⇒ Object
-
#initialize(action, accept: 'application/vnd.api+json', content_type: nil, status: nil, match: nil, cacheable: true, extension: nil, &block) ⇒ Response
constructor
A new instance of Response.
Constructor Details
#initialize(action, accept: 'application/vnd.api+json', content_type: nil, status: nil, match: nil, cacheable: true, extension: nil, &block) ⇒ Response
Returns a new instance of Response.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/jsonapionify/api/response.rb', line 8 def initialize(action, accept: 'application/vnd.api+json', content_type: nil, status: nil, match: nil, cacheable: true, extension: nil, &block) accept = MIME::Types.type_for("ex.#{extension}")[0]&.content_type if extension @extension = extension.to_s if extension @action = action @response_block = block || proc {} @accept = accept unless match @example_accept = accept @content_type = content_type || (@accept == '*/*' ? nil : @accept) @matcher = match || proc {} @status = status || 200 @cacheable = cacheable end |
Instance Attribute Details
#accept ⇒ Object (readonly)
Returns the value of attribute accept.
5 6 7 |
# File 'lib/jsonapionify/api/response.rb', line 5 def accept @accept end |
#action ⇒ Object (readonly)
Returns the value of attribute action.
5 6 7 |
# File 'lib/jsonapionify/api/response.rb', line 5 def action @action end |
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
5 6 7 |
# File 'lib/jsonapionify/api/response.rb', line 5 def content_type @content_type end |
#example_accept ⇒ Object (readonly)
Returns the value of attribute example_accept.
5 6 7 |
# File 'lib/jsonapionify/api/response.rb', line 5 def example_accept @example_accept end |
#extension ⇒ Object (readonly)
Returns the value of attribute extension.
5 6 7 |
# File 'lib/jsonapionify/api/response.rb', line 5 def extension @extension end |
#matcher ⇒ Object (readonly)
Returns the value of attribute matcher.
5 6 7 |
# File 'lib/jsonapionify/api/response.rb', line 5 def matcher @matcher end |
#response_block ⇒ Object (readonly)
Returns the value of attribute response_block.
5 6 7 |
# File 'lib/jsonapionify/api/response.rb', line 5 def response_block @response_block end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
5 6 7 |
# File 'lib/jsonapionify/api/response.rb', line 5 def status @status end |
Instance Method Details
#==(other) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/jsonapionify/api/response.rb', line 25 def ==(other) self.class == other.class && %i{@accept}.all? do |ivar| instance_variable_get(ivar) == other.instance_variable_get(ivar) end end |
#accept_with_header?(accept:, extension:) ⇒ Boolean
68 69 70 |
# File 'lib/jsonapionify/api/response.rb', line 68 def accept_with_header?(accept:, extension:) self.accept == accept || (self.accept == '*/*' && !extension) || (accept == '*/*' && !extension) end |
#accept_with_matcher?(context) ⇒ Boolean
72 73 74 |
# File 'lib/jsonapionify/api/response.rb', line 72 def accept_with_matcher?(context) !!matcher.call(context) end |
#cacheable ⇒ Object
21 22 23 |
# File 'lib/jsonapionify/api/response.rb', line 21 def cacheable action.cacheable && @cacheable end |
#call(instance, context, status: nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/jsonapionify/api/response.rb', line 40 def call(instance, context, status: nil) status ||= self.status response = self instance.instance_eval do body = instance_exec(context, &response.response_block.destructure) Rack::Response.new.tap do |rack_response| rack_response.status = status response_headers.each do |k, v| rack_response.headers[k.split('-').map(&:capitalize).join('-')] = v end rack_response.headers['Content-Type'] = case response.content_type when nil raise(Errors::MissingContentType, 'missing content type') when Proc response.content_type.call(context) else response.content_type end if body.respond_to?(:each) rack_response.body = body elsif !body.nil? rack_response.write(body) end end.finish end end |
#documentation_object ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/jsonapionify/api/response.rb', line 32 def documentation_object OpenStruct.new( accept: accept, content_type: accept, status: status ) end |