class SocketLabs::InjectionApi::Core::InjectionResponseParser

Public Instance Methods

determine_send_result(response_dto, response) click to toggle source

Enumerated SendResult of the payload response from the Injection Api @param [InjectionResponseDto] response_dto: the response dto @param [HttpResponse] response: the Http response @return [SendResult] the parsed result of the injection request

# File lib/socketlabs/injectionapi/core/injection_response_parser.rb, line 99
def determine_send_result(response_dto, response)

  # HttpStatusCode.OK
  if response.status_code == "200"
    result_enum = response_dto.error_code
    if result_enum.nil? || result_enum.empty?
      result_enum = SendResult.enum["UnknownError"]
    end

  # HttpStatusCode.Unauthorized
  elsif response.status_code == "500"
    result_enum = SendResult.enum["InternalError"]

  # HttpStatusCode.Unauthorized
  elsif response.status_code == "408"
    result_enum = SendResult.enum["Timeout"]

  # HttpStatusCode.Unauthorized
  else
    result_enum = SendResult.enum["InvalidAuthentication"]

  end

  result_enum

end
get_injection_response_dto(response) click to toggle source

Get the InjectionResponseDto from the HttpResponse object @param [HttpResponse] response: the Http response @return [InjectionResponseDto] the converted injection response dto

# File lib/socketlabs/injectionapi/core/injection_response_parser.rb, line 43
def get_injection_response_dto(response)

  hash_body = response.to_hash

  resp_dto = InjectionResponseDto.new

  if hash_body.key?(:ErrorCode)
    resp_dto.error_code = SendResult.enum[hash_body[:ErrorCode]]
  end

  if hash_body.key?(:TransactionReceipt)
    resp_dto.transaction_receipt = hash_body[:TransactionReceipt]
  end

  if hash_body.key?(:MessageResults)

    resp_dto.message_results = Array.new
    message_results = hash_body[:MessageResults]

    unless message_results.nil?
      message_results.each do |item|
        message_dto = MessageResultDto.new

        if item.key?(:Index)
          message_dto.index = item[:Index]
        end

        if item.key?(:AddressResults)
          address_results = item[:AddressResults]
          unless address_results.nil?
            address_results.each do |aitem|
              message_dto.address_results.push(SocketLabs::InjectionApi::AddressResult.new(aitem[:EmailAddress], aitem[:Accepted], aitem[:ErrorCode]))
            end
          end
        end

        if item.key?(:ErrorCode)
          message_dto.error_code = item[:ErrorCode]
        end

        resp_dto.message_results.push(message_dto)

      end
    end

    resp_dto

  end


end
parse(response) click to toggle source

Parse the response from the Injection Api into SendResponse @param [HttpResponse] response: the response form the Injection Api @return [SendResponse]

# File lib/socketlabs/injectionapi/core/injection_response_parser.rb, line 17
def parse(response)

  injection_response = get_injection_response_dto(response)
  result_enum = determine_send_result(injection_response, response)
  new_response = SendResponse.new(result_enum)
  new_response.transaction_receipt = injection_response.transaction_receipt

  if result_enum == SendResult.enum["Warning"]
    unless injection_response.message_results.nil? || injection_response.message_results.length == 0
      error_code = injection_response.message_results[0].error_code
      result_enum = SendResult.enum[error_code]
      new_response.result = result_enum
    end
  end

  unless injection_response.message_results.nil? || injection_response.message_results.length == 0
    new_response.address_results = injection_response.message_results[0].address_results
  end

  new_response

end