Class: MDOT::Response

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

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(args) ⇒ Response

Returns a new instance of Response.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mdot/response.rb', line 19

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

Instance Attribute Details

#statusObject (readonly)

Returns the value of attribute status.



11
12
13
# File 'lib/mdot/response.rb', line 11

def status
  @status
end

Instance Method Details

#accepted?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/mdot/response.rb', line 50

def accepted?
  @status == 202
end

#determine_eligibilityObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/mdot/response.rb', line 35

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

#ok?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/mdot/response.rb', line 46

def ok?
  @status == 200
end

#update_tokenObject (private)



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

def update_token
  token_params = { REDIS_CONFIG[:mdot][:namespace] => @uuid }
  token = MDOT::Token.new(token_params)
  token.update(token: @token, uuid: @uuid)
end

#validate_response_against_schema(schema, response) ⇒ Object (private)



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

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