Sha256: de563c4092e429647196e47f3b2058ef919e49c6745d0321f5ec70cf1f1f5790

Contents?: true

Size: 1.39 KB

Versions: 10

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module SolidusBolt
  module Accounts
    class AddAddressService < SolidusBolt::BaseService
      attr_reader :order, :address, :access_token

      def initialize(order:, address:, access_token:)
        @order = order
        @address = address
        @access_token = access_token
        super
      end

      def call
        return if address_already_uploaded?

        add_address
      end

      private

      def bolt_addresses
        @bolt_addresses ||= SolidusBolt::Accounts::DetailService.call(access_token: access_token)['addresses']
      end

      def address_already_uploaded?
        bolt_addresses.any? do |bolt_address|
          bolt_address['street_address1'] == address.address1 &&
          bolt_address['locality'] == address.city &&
          bolt_address['region'] == address.state&.abbr &&
          bolt_address['postal_code'] == address.zipcode
        end
      end

      def add_address
        handle_result(HTTParty.post("#{api_base_url}/#{api_version}/account/addresses", build_options))
      end

      def address_body
        address.bolt_address(order.email).to_json
      end

      def build_options
        {
          body: address_body,
          headers: {
            'Authorization' => "Bearer #{access_token}",
            'Content-Type' => 'application/json'
          }.merge(authentication_header)
        }
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
solidus_bolt-0.7.2 app/services/solidus_bolt/accounts/add_address_service.rb
solidus_bolt-0.7.1 app/services/solidus_bolt/accounts/add_address_service.rb
solidus_bolt-0.7.0 app/services/solidus_bolt/accounts/add_address_service.rb
solidus_bolt-0.6.0 app/services/solidus_bolt/accounts/add_address_service.rb
solidus_bolt-0.5.0 app/services/solidus_bolt/accounts/add_address_service.rb
solidus_bolt-0.4.0 app/services/solidus_bolt/accounts/add_address_service.rb
solidus_bolt-0.3.0 app/services/solidus_bolt/accounts/add_address_service.rb
solidus_bolt-0.2.0 app/services/solidus_bolt/accounts/add_address_service.rb
solidus_bolt-0.1.0 app/services/solidus_bolt/accounts/add_address_service.rb
solidus_bolt-0.0.1 app/services/solidus_bolt/accounts/add_address_service.rb