Sha256: 70f0e87b896cfbd67447ecd131ff9c714b89ac7b94738ec3be1b3230eb21169c

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

require_relative '../operation'

module Desertcart
  class Operation
    class Update
      include Desertcart::Operation::Mixin

      private

      def update_in_ledger
        case response.status
        when 200
          LedgerSync::Result.Success(response)
        when 404
          not_found
        when 422
          rejected
        else
          fail(response.status)
        end
      end

      def operate
        update_in_ledger
          .and_then { success }
      end

      def response
        @response ||= client.update(
          body: serializer.serialize(resource: resource),
          path: ledger_resource_path
        )
      end

      def success
        super(
          resource: deserialized_resource,
          response: response
        )
      end

      def fail(status)
        failure(
          LedgerSync::Error::OperationError.new(
            operation: self,
            response: response,
            message: "Status code: #{status}"
          ),
          resource: @resource
        )
      end

      def not_found
        failure(
          LedgerSync::Error::OperationError::NotFoundError.new(
            operation: self,
            response: response
          ),
          resource: @resource
        )
      end

      def rejected
        failure(
          LedgerSync::Error::OperationError::UnprocessableEntityError.new(
            operation: self,
            response: response
          ),
          resource: @resource
        )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
desertcart-1.2.0 lib/desertcart/operation/update.rb