Class: SM::Middleware::Response::SMParser
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- SM::Middleware::Response::SMParser
- Defined in:
- lib/sm/middleware/response/sm_parser.rb
Overview
Middleware class responsible for customizing MHV SM response parsing
Instance Method Summary collapse
- #fix_attachments(message_json) ⇒ Object private
- #normalize_message(object) ⇒ Object private
-
#on_complete(env) ⇒ Faraday::Env
Override the Faraday #on_complete method to filter body through custom #parse.
- #parse(body = nil) ⇒ Object private
- #parsed_all_triage ⇒ Object private
- #parsed_all_triage_meta ⇒ Object private
- #parsed_categories ⇒ Object private
- #parsed_folders ⇒ Object private
- #parsed_messages ⇒ Object private
- #parsed_signature ⇒ Object private
- #parsed_threads ⇒ Object private
- #parsed_threads_object ⇒ Object private
- #parsed_triage ⇒ Object private
- #preferences ⇒ Object private
- #split_errors! ⇒ Object private
- #split_meta_fields! ⇒ Object private
Instance Method Details
#fix_attachments(message_json) ⇒ Object (private)
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/sm/middleware/response/sm_parser.rb', line 112 def () return .except(:attachments) if [:attachments].blank? = [:id] = Array.wrap([:attachments]) # remove the outermost object name for attachment and inject message_id = .map do || [:attachment].map { |e| e.merge(message_id:) } end.flatten .merge(attachments:) end |
#normalize_message(object) ⇒ Object (private)
102 103 104 105 106 107 108 109 110 |
# File 'lib/sm/middleware/response/sm_parser.rb', line 102 def (object) return object if object.blank? if object.is_a?(Array) object.map { |a| (a) } else (object) end end |
#on_complete(env) ⇒ Faraday::Env
Override the Faraday #on_complete method to filter body through custom #parse
15 16 17 18 19 |
# File 'lib/sm/middleware/response/sm_parser.rb', line 15 def on_complete(env) return unless env.response_headers['content-type']&.match?(/\bjson/) env[:body] = parse(env.body) if env.body.present? end |
#parse(body = nil) ⇒ Object (private)
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/sm/middleware/response/sm_parser.rb', line 23 def parse(body = nil) @parsed_json = body @meta_attributes = @errors = @parsed_json.delete(:errors) || {} data = parsed_threads_object || parsed_all_triage || parsed_triage || preferences || parsed_folders || () || parsed_categories || parsed_signature @parsed_json = { data:, errors: @errors, metadata: @meta_attributes } @parsed_json end |
#parsed_all_triage ⇒ Object (private)
64 65 66 67 68 69 70 71 72 |
# File 'lib/sm/middleware/response/sm_parser.rb', line 64 def parsed_all_triage if @parsed_json.is_a?(Array) && @parsed_json.any? do |k| k.key?(:associated_triage_groups) end @parsed_json[1][:triage_team] else @parsed_json[:associated_triage_groups] end end |
#parsed_all_triage_meta ⇒ Object (private)
74 75 76 77 78 79 80 |
# File 'lib/sm/middleware/response/sm_parser.rb', line 74 def if @parsed_json.is_a?(Array) && @parsed_json.any? do |k| k.key?(:associated_triage_groups) end @parsed_json[0] end end |
#parsed_categories ⇒ Object (private)
86 87 88 |
# File 'lib/sm/middleware/response/sm_parser.rb', line 86 def parsed_categories @parsed_json.key?(:message_category_type) ? @parsed_json : @parsed_json[:message_category_type] end |
#parsed_folders ⇒ Object (private)
56 57 58 |
# File 'lib/sm/middleware/response/sm_parser.rb', line 56 def parsed_folders @parsed_json.key?(:system_folder) ? @parsed_json : @parsed_json[:folder] end |
#parsed_messages ⇒ Object (private)
82 83 84 |
# File 'lib/sm/middleware/response/sm_parser.rb', line 82 def @parsed_json.key?(:recipient_id) ? @parsed_json : @parsed_json[:message] end |
#parsed_signature ⇒ Object (private)
90 91 92 |
# File 'lib/sm/middleware/response/sm_parser.rb', line 90 def parsed_signature @parsed_json.key?(:signature_name) ? @parsed_json : @parsed_json[:signature_name] end |
#parsed_threads ⇒ Object (private)
44 45 46 |
# File 'lib/sm/middleware/response/sm_parser.rb', line 44 def parsed_threads @parsed_json.is_a?(Array) && @parsed_json.each { |t| return false unless t.key?(:thread_id) } end |
#parsed_threads_object ⇒ Object (private)
52 53 54 |
# File 'lib/sm/middleware/response/sm_parser.rb', line 52 def parsed_threads_object @parsed_json.is_a?(Array) && @parsed_json.each { |t| return false unless t.key?(:thread_id) } end |
#parsed_triage ⇒ Object (private)
60 61 62 |
# File 'lib/sm/middleware/response/sm_parser.rb', line 60 def parsed_triage @parsed_json.key?(:triage_team_id) ? @parsed_json : @parsed_json[:triage_team] end |
#preferences ⇒ Object (private)
48 49 50 |
# File 'lib/sm/middleware/response/sm_parser.rb', line 48 def preferences %i[notify_me 0].any? { |k| @parsed_json.key?(k) } ? @parsed_json : nil end |
#split_errors! ⇒ Object (private)
94 95 96 |
# File 'lib/sm/middleware/response/sm_parser.rb', line 94 def split_errors! @parsed_json.delete(:errors) || {} end |
#split_meta_fields! ⇒ Object (private)
98 99 100 |
# File 'lib/sm/middleware/response/sm_parser.rb', line 98 def || {} end |