Sha256: 7d1d522c0621b1863952217960d8da0ae8219138baf9cd3f11ecc16ba3f20d68

Contents?: true

Size: 1.91 KB

Versions: 4

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

require 'we_ship_client/client'
require 'we_ship_client/entities'
require 'we_ship_client/exceptions'

module WeShipClient
  module Interactors
    # Class to interact with the /process_orders API endpoint.
    class ProcessOrders
      # @param auth_token [String] A token generated by TokenClient
      # @param process_orders_request [Entities::ProcessOrdersRequest] The request payload
      def initialize(auth_token:, process_orders_request:)
        @auth_token = auth_token
        @process_orders_request = process_orders_request
      end

      # Sends orders to be processed.
      #
      # @raise [Exceptions::AuthenticationError] If the status code is 401
      # @raise [Exceptions::ServerError] If the request is not successful
      # @return [Entities::Responses::ProcessOrders] The response
      def call
        json_response = client.http_client.post(
          "#{client.base_url}/process_orders",
          data: process_orders_request.to_h,
          headers: {
            Authorization: "JWT #{auth_token}"
          }
        )

        handle_exception(json_response) unless json_response.status == 200
        response_hash = JSON.parse(json_response.body, symbolize_names: true)
        response_class.new(response_hash[:response])
      end

      private

      attr_accessor :auth_token, :process_orders_request

      def handle_exception(response)
        WeShipClient.logger.info(
          "[WeShipClient::Interactors::ProcessOrders] [EXCEPTION] #{response}"
        )

        case response.status
        when 401
          raise WeShipClient::Exceptions::AuthenticationError, response.body
        else
          raise WeShipClient::Exceptions::ServerError, response.body
        end
      end

      def client
        @client ||= WeShipClient::Client.new
      end

      def response_class
        WeShipClient::Entities::Responses::ProcessOrders
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
we_ship_client-1.2.0 lib/we_ship_client/interactors/process_orders.rb
we_ship_client-1.1.1 lib/we_ship_client/interactors/process_orders.rb
we_ship_client-1.1.0 lib/we_ship_client/interactors/process_orders.rb
we_ship_client-1.0.0 lib/we_ship_client/interactors/process_orders.rb