Class: MPI::Services::FindProfileResponseCreator

Inherits:
Object
  • Object
show all
Includes:
SentryLogging
Defined in:
lib/mpi/services/find_profile_response_creator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata

Constructor Details

#initialize(type:, response: nil, error: nil) ⇒ FindProfileResponseCreator

Returns a new instance of FindProfileResponseCreator.



14
15
16
17
18
# File 'lib/mpi/services/find_profile_response_creator.rb', line 14

def initialize(type:, response: nil, error: nil)
  @type = type
  @response = response
  @error = error
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



12
13
14
# File 'lib/mpi/services/find_profile_response_creator.rb', line 12

def error
  @error
end

#responseObject (readonly)

Returns the value of attribute response.



12
13
14
# File 'lib/mpi/services/find_profile_response_creator.rb', line 12

def response
  @response
end

#typeObject (readonly)

Returns the value of attribute type.



12
13
14
# File 'lib/mpi/services/find_profile_response_creator.rb', line 12

def type
  @type
end

Instance Method Details

#create_error_responseObject (private)



40
41
42
43
# File 'lib/mpi/services/find_profile_response_creator.rb', line 40

def create_error_response
  log_error_response
  Responses::FindProfileResponse.new(status: error_status, error: detailed_error)
end

#create_successful_responseObject (private)



35
36
37
38
# File 'lib/mpi/services/find_profile_response_creator.rb', line 35

def create_successful_response
  info_log("icn=#{parsed_profile.icn}, transaction_id=#{parsed_profile.transaction_id}")
  Responses::FindProfileResponse.new(status: :ok, profile: parsed_profile)
end

#detailed_errorObject (private)



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mpi/services/find_profile_response_creator.rb', line 57

def detailed_error
  @detailed_error ||=
    if error
      error
    elsif profile_parser.multiple_match?
      Errors::DuplicateRecords.new(error_details)
    elsif profile_parser.failed_request?
      Errors::FailedRequestError.new(error_details)
    elsif profile_parser.invalid_request? || profile_parser.unknown_error?
      Errors::RecordNotFound.new(error_details)
    end
end

#error_detailsObject (private)



82
83
84
# File 'lib/mpi/services/find_profile_response_creator.rb', line 82

def error_details
  @error_details ||= profile_parser.error_details
end

#error_statusObject (private)



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mpi/services/find_profile_response_creator.rb', line 70

def error_status
  @error_status ||=
    if error.present? || profile_parser.failed_request?
      :server_error
    elsif profile_parser.multiple_match? ||
          profile_parser.invalid_request? ||
          profile_parser.no_match? ||
          profile_parser.unknown_error?
      :not_found
    end
end

#info_log(message) ⇒ Object (private)



53
54
55
# File 'lib/mpi/services/find_profile_response_creator.rb', line 53

def info_log(message)
  Rails.logger.info("[MPI][Services][FindProfileResponseCreator] #{type} #{message}")
end

#log_error_responseObject (private)



45
46
47
48
49
50
51
# File 'lib/mpi/services/find_profile_response_creator.rb', line 45

def log_error_response
  if error || profile_parser.multiple_match? || profile_parser.failed_request?
    log_message_to_sentry("MPI #{type} response error", :warn, { error_message: detailed_error&.message })
  elsif profile_parser.invalid_request? || profile_parser.no_match? || profile_parser.unknown_error?
    info_log("Record Not Found, transaction_id=#{parsed_profile.transaction_id}")
  end
end

#parsed_profileObject (private)



86
87
88
# File 'lib/mpi/services/find_profile_response_creator.rb', line 86

def parsed_profile
  @parsed_profile ||= profile_parser.parse
end

#performObject



20
21
22
23
24
25
26
27
# File 'lib/mpi/services/find_profile_response_creator.rb', line 20

def perform
  validations
  if error_status
    create_error_response
  else
    create_successful_response
  end
end

#profile_parserObject (private)



90
91
92
# File 'lib/mpi/services/find_profile_response_creator.rb', line 90

def profile_parser
  @profile_parser ||= Responses::ProfileParser.new(response)
end

#validationsObject (private)



31
32
33
# File 'lib/mpi/services/find_profile_response_creator.rb', line 31

def validations
  raise Errors::InvalidResponseParamsError unless response.present? ^ error.present?
end