Class: Apps::Responses::Response

Inherits:
Common::Base show all
Defined in:
lib/apps/responses/response.rb

Overview

Model for Apps responses. Body is passed straight through from the service with a validation check that it matches the expected schema.

Instance Attribute Summary collapse

Attributes inherited from Common::Base

#errors_hash, #metadata

Instance Method Summary collapse

Methods inherited from Common::Base

#changed, #changed?, #changes, default_sort, filterable_attributes, max_per_page, per_page, sortable_attributes

Constructor Details

#initialize(status, body, schema_name) ⇒ Response

Returns a new instance of Response.



21
22
23
24
25
26
27
28
# File 'lib/apps/responses/response.rb', line 21

def initialize(status, body, schema_name)
  self.body = if status == 204
                nil
              else
                json_format_is_valid?(body, schema_name) ? body : {}
              end
  self.status = status
end

Instance Attribute Details

#bodyHash

Returns Validated response body.

Returns:

  • (Hash)

    Validated response body.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/apps/responses/response.rb', line 17

class Response < Common::Base
  attribute :body, String
  attribute :status, Integer

  def initialize(status, body, schema_name)
    self.body = if status == 204
                  nil
                else
                  json_format_is_valid?(body, schema_name) ? body : {}
                end
    self.status = status
  end

  private

  def json_format_is_valid?(body, schema_name)
    schema_path = Rails.root.join('lib', 'apps', 'schemas', "#{schema_name}.json").to_s
    JSON::Validator.validate!(schema_path, body, strict: false)
  end
end

#statusInteger

Returns The HTTP status code.

Returns:

  • (Integer)

    The HTTP status code.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/apps/responses/response.rb', line 17

class Response < Common::Base
  attribute :body, String
  attribute :status, Integer

  def initialize(status, body, schema_name)
    self.body = if status == 204
                  nil
                else
                  json_format_is_valid?(body, schema_name) ? body : {}
                end
    self.status = status
  end

  private

  def json_format_is_valid?(body, schema_name)
    schema_path = Rails.root.join('lib', 'apps', 'schemas', "#{schema_name}.json").to_s
    JSON::Validator.validate!(schema_path, body, strict: false)
  end
end

Instance Method Details

#json_format_is_valid?(body, schema_name) ⇒ Boolean (private)

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/apps/responses/response.rb', line 32

def json_format_is_valid?(body, schema_name)
  schema_path = Rails.root.join('lib', 'apps', 'schemas', "#{schema_name}.json").to_s
  JSON::Validator.validate!(schema_path, body, strict: false)
end