Class: Vets::Response

Inherits:
Object
  • Object
show all
Includes:
Model
Defined in:
lib/vets/response.rb

Constant Summary collapse

STATUS_TEXTS =
{
  200 => 'OK',
  403 => 'NOT_AUTHORIZED',
  404 => 'NOT_FOUND'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

#attributes, #nested_attributes

Methods included from Attributes

included

Constructor Details

#initialize(status_code:, body:, schema_name: nil) ⇒ Response

Returns a new instance of Response.



33
34
35
36
37
# File 'lib/vets/response.rb', line 33

def initialize(status_code:, body:, schema_name: nil)
  super(status_code: status_code.to_i, body: parse_json(body))

  validate_body_schema(body, schema_name) if schema_name
end

Instance Attribute Details

#bodyHash

Returns Validated response body.

Returns:

  • (Hash)

    Validated response body.



33
34
35
36
37
# File 'lib/vets/response.rb', line 33

def initialize(status_code:, body:, schema_name: nil)
  super(status_code: status_code.to_i, body: parse_json(body))

  validate_body_schema(body, schema_name) if schema_name
end

#schema_nameString

Returns File name without extention.

Returns:

  • (String)

    File name without extention

See Also:



33
34
35
36
37
# File 'lib/vets/response.rb', line 33

def initialize(status_code:, body:, schema_name: nil)
  super(status_code: status_code.to_i, body: parse_json(body))

  validate_body_schema(body, schema_name) if schema_name
end

#statusInteger

Returns The HTTP status code.

Returns:

  • (Integer)

    The HTTP status code.



33
34
35
36
37
# File 'lib/vets/response.rb', line 33

def initialize(status_code:, body:, schema_name: nil)
  super(status_code: status_code.to_i, body: parse_json(body))

  validate_body_schema(body, schema_name) if schema_name
end

Class Method Details

.build_from_response(response, schema_name: nil) ⇒ Object



18
19
20
21
22
# File 'lib/vets/response.rb', line 18

def self.build_from_response(response, schema_name: nil)
  status_code = response.try(:status) || response[:status]
  body = response.try(:body) || response[:body]
  Vets::Response.new(status_code:, body:, schema_name:)
end

Instance Method Details

#accepted?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/vets/response.rb', line 43

def accepted?
  status_code == 202
end

#cache?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/vets/response.rb', line 47

def cache?
  ok?
end

#metadataObject



51
52
53
# File 'lib/vets/response.rb', line 51

def 
  { status: STATUS_TEXTS.fetch(status_code, 'SERVER_ERROR') }
end

#ok?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/vets/response.rb', line 39

def ok?
  status_code == 200
end

#parse_json(body) ⇒ Object (private)



62
63
64
65
66
# File 'lib/vets/response.rb', line 62

def parse_json(body)
  return body if body.is_a?(Hash) || body.nil?

  JSON.parse(body)
end

#validate_body_schema(body, schema_name) ⇒ Object (private)



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

def validate_body_schema(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