-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathresponse.rb
67 lines (55 loc) · 1.91 KB
/
response.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
67
# frozen_string_literal: true
require 'common/models/base'
require_relative 'eligibility'
require_relative 'supply'
require_relative 'token'
require_relative 'address'
module MDOT
class Response < Common::Base
attr_reader :status
attribute :permanent_address, MDOT::Address
attribute :temporary_address, MDOT::Address
attribute :supplies, Array[MDOT::Supply]
attribute :eligibility, MDOT::Eligibility
attribute :vet_email, String
def initialize(args)
validate_response_against_schema(args[:schema], args[:response])
@uuid = args[:uuid]
@response = args[:response]
@token = @response.response_headers['VAAPIKEY']
@body = @response.body
@parsed_body = @body.is_a?(String) ? JSON.parse(@body) : @body
self.permanent_address = @parsed_body['permanent_address']
self.temporary_address = @parsed_body['temporary_address']
self.supplies = @parsed_body['supplies']
self.vet_email = @parsed_body['vet_email']
self.eligibility = determine_eligibility
@status = args[:response][:status]
update_token
end
def determine_eligibility
eligibility = MDOT::Eligibility.new
supplies.each do |supply|
group = supply.product_group.downcase.pluralize.to_sym
eligibility.send("#{group}=", true) if eligibility.attributes.key?(group) && supply.available_for_reorder
end
eligibility
end
def ok?
@status == 200
end
def accepted?
@status == 202
end
private
def update_token
token_params = { REDIS_CONFIG[:mdot][:namespace] => @uuid }
token = MDOT::Token.new(token_params)
token.update(token: @token, uuid: @uuid)
end
def validate_response_against_schema(schema, response)
schema_path = Rails.root.join('lib', 'mdot', 'schemas', "#{schema}.json").to_s
JSON::Validator.validate!(schema_path, response.body, strict: false)
end
end
end