Class: Preneeds::Middleware::Response::PreneedsParser

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/preneeds/middleware/response/preneeds_parser.rb

Overview

Faraday middleware responsible for customizing parsing of the EOAS response.

Instance Method Summary collapse

Instance Method Details

#extract_soap_body(hash) ⇒ Object (private)



34
35
36
37
38
39
# File 'lib/preneeds/middleware/response/preneeds_parser.rb', line 34

def extract_soap_body(hash)
  hash = hash['envelope'] if hash.keys.include?('envelope')
  hash = hash['body'] if hash.keys.include?('body')

  hash
end

#map_military_rank_id_to_details(hash) ⇒ Object (private)



41
42
43
44
45
46
# File 'lib/preneeds/middleware/response/preneeds_parser.rb', line 41

def map_military_rank_id_to_details(hash)
  hash.map do |rank|
    rank['military_rank_detail'] = rank.delete('id')
    rank
  end
end

#on_complete(env) ⇒ Faraday::Env

Parses the EOAS response.

Returns:

  • (Faraday::Env)


13
14
15
16
17
# File 'lib/preneeds/middleware/response/preneeds_parser.rb', line 13

def on_complete(env)
  return unless env.response_headers['content-type']&.match?(/\bxml/)

  env[:body] = parse(env.body) if env.body.present?
end

#parse(body) ⇒ Object (private)



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/preneeds/middleware/response/preneeds_parser.rb', line 21

def parse(body)
  hash = Hash.from_xml(Ox.dump(body))&.deep_transform_keys(&:underscore)
  hash = extract_soap_body(hash)

  key = hash&.keys&.first
  if key.present?
    hash = hash[key]['return']
    hash = map_military_rank_id_to_details(hash) if key == 'get_military_rank_for_branch_of_service_response'
  end

  { data: hash }
end