Class: BB::Middleware::Response::BBParser

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

Overview

Middleware class responsible for customizing MHV BB response parsing

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Faraday::Env

Override the Faraday #on_complete method to filter body through custom #parse

Parameters:

  • env (Faraday::Env)

    the request environment

Returns:

  • (Faraday::Env)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bb/middleware/response/bb_parser.rb', line 15

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

  # If POST is successful message body is irrelevant
  # if it was not successul an exception would have already been raised
  return if env.method == :post

  # Don't parse the VHIE sharing status calls.
  return if env.url.to_s.include? 'optinout'

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

#parse(body = nil) ⇒ Object (private)



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bb/middleware/response/bb_parser.rb', line 30

def parse(body = nil)
  @parsed_json = body
  @meta_attributes = split_meta_fields!
  @errors = @parsed_json.delete(:errors) || {}

  data =  parsed_extract_status_list || parsed_health_record_types
  @parsed_json = {
    data:,
    errors: @errors,
    metadata: @meta_attributes
  }
  @parsed_json
end

#parsed_extract_status_listObject (private)



54
55
56
57
58
# File 'lib/bb/middleware/response/bb_parser.rb', line 54

def parsed_extract_status_list
  return nil unless @parsed_json.keys.include?(:facility_extract_status_list)

  @parsed_json[:facility_extract_status_list]
end

#parsed_health_record_typesObject (private)



60
61
62
63
64
# File 'lib/bb/middleware/response/bb_parser.rb', line 60

def parsed_health_record_types
  return nil unless @parsed_json.keys.include?(:data_classes)

  @parsed_json[:data_classes].uniq.sort.map { |dc| { name: dc } }
end

#split_meta_fields!Object (private)



44
45
46
47
48
49
50
51
52
# File 'lib/bb/middleware/response/bb_parser.rb', line 44

def split_meta_fields!
  updated_at = @parsed_json.delete(:last_updated_time) ||
               @parsed_json.delete(:last_updatedtime)

  {
    updated_at:,
    failed_station_list: @parsed_json.delete(:failed_station_list)
  }
end