Class: MPI::Services::AddPersonResponseCreator

Inherits:
Object
  • Object
show all
Includes:
SentryLogging
Defined in:
lib/mpi/services/add_person_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) ⇒ AddPersonResponseCreator

Returns a new instance of AddPersonResponseCreator.



13
14
15
16
17
# File 'lib/mpi/services/add_person_response_creator.rb', line 13

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.



11
12
13
# File 'lib/mpi/services/add_person_response_creator.rb', line 11

def error
  @error
end

#responseObject (readonly)

Returns the value of attribute response.



11
12
13
# File 'lib/mpi/services/add_person_response_creator.rb', line 11

def response
  @response
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/mpi/services/add_person_response_creator.rb', line 11

def type
  @type
end

Instance Method Details

#add_parserObject (private)



71
72
73
# File 'lib/mpi/services/add_person_response_creator.rb', line 71

def add_parser
  @add_parser ||= Responses::AddParser.new(response)
end

#create_error_responseObject (private)



43
44
45
46
# File 'lib/mpi/services/add_person_response_creator.rb', line 43

def create_error_response
  log_message_to_sentry("MPI #{type} response error", :warn, { error_message: detailed_error&.message })
  Responses::AddPersonResponse.new(status: :server_error, error: detailed_error)
end

#create_successful_responseObject (private)



34
35
36
37
38
39
40
41
# File 'lib/mpi/services/add_person_response_creator.rb', line 34

def create_successful_response
  Rails.logger.info("[MPI][Services][AddPersonResponseCreator] #{type}, " \
                    "icn=#{parsed_codes[:icn]}, " \
                    "idme_uuid=#{parsed_codes[:idme_uuid]}, " \
                    "logingov_uuid=#{parsed_codes[:logingov_uuid]}, " \
                    "transaction_id=#{parsed_codes[:transaction_id]}")
  Responses::AddPersonResponse.new(status: :ok, parsed_codes:)
end

#detailed_errorObject (private)



48
49
50
51
52
53
54
55
56
57
# File 'lib/mpi/services/add_person_response_creator.rb', line 48

def detailed_error
  @detailed_error ||=
    if error
      error
    elsif add_parser.invalid_request?
      Errors::InvalidRequestError.new(error_details)
    elsif add_parser.failed_request?
      Errors::FailedRequestError.new(error_details)
    end
end

#error_detailsObject (private)



63
64
65
# File 'lib/mpi/services/add_person_response_creator.rb', line 63

def error_details
  @error_details ||= add_parser.error_details(parsed_codes)
end

#failed_response?Boolean (private)

Returns:

  • (Boolean)


59
60
61
# File 'lib/mpi/services/add_person_response_creator.rb', line 59

def failed_response?
  error.present? || add_parser.failed_or_invalid?
end

#parsed_codesObject (private)



67
68
69
# File 'lib/mpi/services/add_person_response_creator.rb', line 67

def parsed_codes
  @parsed_codes ||= add_parser.parse
end

#performObject



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

def perform
  validations
  if failed_response?
    create_error_response
  else
    create_successful_response
  end
end

#validationsObject (private)



30
31
32
# File 'lib/mpi/services/add_person_response_creator.rb', line 30

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