Sha256: 55bb3abd5e2c729770ad010321e8fc68c196eae0e6924b899db8bbbf91c5c3f1

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module FriendlyShipping
  module Services
    class RL
      class ParseInvoiceResponse
        extend Dry::Monads::Result::Mixin

        class << self
          # @param [FriendlyShipping::Request] request
          # @param [FriendlyShipping::Response] response
          # @return [Result<ApiResult<ShipmentDocument>>]
          def call(request:, response:)
            parsed_json = JSON.parse(response.body)
            data = (parsed_json['Documents'].find { _1["Type"] == "Invoice" } || {})["Data"]

            document = ShipmentDocument.new(binary: data, format: :pdf, document_type: :invoice)
            if document.valid?
              Success(
                ApiResult.new(
                  document,
                  original_request: request,
                  original_response: response
                )
              )
            else
              errors = if parsed_json["Messages"].present?
                         parsed_json["Messages"]
                       else
                         parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }]).
                           map { _1['ErrorMessage'] }
                       end

              Failure(
                ApiResult.new(
                  errors,
                  original_request: request,
                  original_response: response
                )
              )
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
friendly_shipping-0.10.1 lib/friendly_shipping/services/rl/parse_invoice_response.rb
friendly_shipping-0.10.0 lib/friendly_shipping/services/rl/parse_invoice_response.rb