Sha256: 1e62a4e93aabe0f1e1219bbc015e87d351c85b73faf752d75f24115140323fd0

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module SolidusBraintree
  class Address
    delegate :address1,
      :address2,
      :city,
      :country,
      :phone,
      :state,
      :zipcode,
      to: :spree_address

    def self.split_name(name)
      if defined?(Spree::Address::Name)
        address_name = Spree::Address::Name.new(name)
        [address_name.first_name, address_name.last_name]
      else
        name.strip.split(' ', 2)
      end
    end

    def initialize(spree_address)
      @spree_address = spree_address
    end

    def to_json(*_args)
      address_hash = {
        line1: address1,
        line2: address2,
        city: city,
        postalCode: zipcode,
        countryCode: country.iso,
        phone: phone,
        recipientName: "#{firstname} #{lastname}"
      }

      if ::Spree::Config.address_requires_state && country.states_required
        address_hash[:state] = state.name
      end
      address_hash.to_json
    end

    def firstname
      if SolidusSupport.combined_first_and_last_name_in_address?
        self.class.split_name(spree_address.name).first
      else
        spree_address.firstname
      end
    end

    def lastname
      if SolidusSupport.combined_first_and_last_name_in_address?
        self.class.split_name(spree_address.name).last
      else
        spree_address.lastname
      end
    end

    private

    attr_reader :spree_address
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
solidus_braintree-3.1.0 app/models/solidus_braintree/address.rb
solidus_braintree-3.0.1 app/models/solidus_braintree/address.rb
solidus_braintree-3.0.0 app/models/solidus_braintree/address.rb
solidus_braintree-2.0.0 app/models/solidus_braintree/address.rb