Class: Rx::Middleware::Response::RxParser
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- Rx::Middleware::Response::RxParser
- Defined in:
- lib/rx/middleware/response/rx_parser.rb
Overview
Middleware class responsible for customizing MHV Rx response parsing
Instance Method Summary collapse
-
#on_complete(env) ⇒ Faraday::Env
Override the Faraday #on_complete method to filter body through custom #parse.
- #parse(body = nil) ⇒ Object private
- #parsed_documentation ⇒ Object private
- #parsed_medication_list ⇒ Object private
- #parsed_prescription ⇒ Object private
- #parsed_prescription_list ⇒ Object private
- #parsed_tracking_object ⇒ Object private
- #split_meta_fields! ⇒ Object private
Instance Method Details
#on_complete(env) ⇒ Faraday::Env
Override the Faraday #on_complete method to filter body through custom #parse
15 16 17 18 19 20 21 22 |
# File 'lib/rx/middleware/response/rx_parser.rb', line 15 def on_complete(env) return unless env.response_headers['content-type']&.match?(/\bjson/) # If POST for prescriptions is successful message body is irrelevant # if it was not successul an exception would have already been raised return if env.method == :post env[:body] = parse(env.body) if env.body.present? end |
#parse(body = nil) ⇒ Object (private)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rx/middleware/response/rx_parser.rb', line 26 def parse(body = nil) @parsed_json = body @meta_attributes = @errors = @parsed_json.delete(:errors) || {} data = parsed_prescription_list || parsed_tracking_object || parsed_prescription || parsed_medication_list || parsed_documentation @parsed_json = { data:, errors: @errors, metadata: @meta_attributes } @parsed_json end |
#parsed_documentation ⇒ Object (private)
47 48 49 50 51 |
# File 'lib/rx/middleware/response/rx_parser.rb', line 47 def parsed_documentation return nil unless @parsed_json.keys.include?(:html) @parsed_json[:html] end |
#parsed_medication_list ⇒ Object (private)
41 42 43 44 45 |
# File 'lib/rx/middleware/response/rx_parser.rb', line 41 def parsed_medication_list return nil unless @parsed_json.keys.include?(:medication_list) @parsed_json[:medication_list][:medication] end |
#parsed_prescription ⇒ Object (private)
63 64 65 66 67 |
# File 'lib/rx/middleware/response/rx_parser.rb', line 63 def parsed_prescription return nil unless @parsed_json.keys.include?(:refill_status) @parsed_json end |
#parsed_prescription_list ⇒ Object (private)
69 70 71 72 73 |
# File 'lib/rx/middleware/response/rx_parser.rb', line 69 def parsed_prescription_list return nil unless @parsed_json.keys.include?(:prescription_list) @parsed_json[:prescription_list] end |
#parsed_tracking_object ⇒ Object (private)
75 76 77 78 79 80 81 82 83 |
# File 'lib/rx/middleware/response/rx_parser.rb', line 75 def parsed_tracking_object return nil unless @parsed_json.keys.include?(:tracking_info) infos, base = @parsed_json.partition { |k, _| k == :tracking_info } infos.to_h[:tracking_info].map do |tracking_info| tracking_info[:other_prescriptions] = tracking_info.delete(:other_prescription_list_included) base.to_h.merge(tracking_info) end end |
#split_meta_fields! ⇒ Object (private)
53 54 55 56 57 58 59 60 61 |
# File 'lib/rx/middleware/response/rx_parser.rb', line 53 def 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 |