Class: SM::Middleware::Response::SMParser

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

Overview

Middleware class responsible for customizing MHV SM response parsing

Instance Method Summary collapse

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 fix_attachments(message_json)
  return message_json.except(:attachments) if message_json[:attachments].blank?

  message_id = message_json[:id]
  attachments = Array.wrap(message_json[:attachments])
  # remove the outermost object name for attachment and inject message_id
  attachments = attachments.map do |attachment|
    attachment[:attachment].map { |e| e.merge(message_id:) }
  end.flatten
  message_json.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 normalize_message(object)
  return object if object.blank?

  if object.is_a?(Array)
    object.map { |a| fix_attachments(a) }
  else
    fix_attachments(object)
  end
end

#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
# 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 = split_meta_fields!
  @errors = @parsed_json.delete(:errors) || {}

  data =  parsed_threads_object ||
          parsed_all_triage ||
          parsed_triage   ||
          preferences     ||
          parsed_folders  ||
          normalize_message(parsed_messages) ||
          parsed_categories ||
          parsed_signature
  @parsed_json = {
    data:,
    errors: @errors,
    metadata: @meta_attributes
  }
  @parsed_json
end

#parsed_all_triageObject (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_metaObject (private)



74
75
76
77
78
79
80
# File 'lib/sm/middleware/response/sm_parser.rb', line 74

def parsed_all_triage_meta
  if @parsed_json.is_a?(Array) && @parsed_json.any? do |k|
    k.key?(:associated_triage_groups)
  end
    @parsed_json[0]
  end
end

#parsed_categoriesObject (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_foldersObject (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_messagesObject (private)



82
83
84
# File 'lib/sm/middleware/response/sm_parser.rb', line 82

def parsed_messages
  @parsed_json.key?(:recipient_id) ? @parsed_json : @parsed_json[:message]
end

#parsed_signatureObject (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_threadsObject (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_objectObject (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_triageObject (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

#preferencesObject (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 split_meta_fields!
  parsed_all_triage_meta || {}
end