Sha256: f4e6547fe844f7e32d213e006c2980af3dafd62b7477ee24cf24e5e29ba67809

Contents?: true

Size: 1.39 KB

Versions: 4

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module SolidusPaypalBraintree
  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_paypal_braintree-1.2.0 app/models/solidus_paypal_braintree/address.rb
solidus_paypal_braintree-1.1.2 app/models/solidus_paypal_braintree/address.rb
solidus_paypal_braintree-1.1.1 app/models/solidus_paypal_braintree/address.rb
solidus_paypal_braintree-1.1.0 app/models/solidus_paypal_braintree/address.rb