Sha256: 9728cb2abcb16e8418dfcb0c09d381f0c2136431836d75615a323ec2db6a3224

Contents?: true

Size: 675 Bytes

Versions: 17

Compression:

Stored size: 675 Bytes

Contents

# frozen_string_literal: true

module SolidusPaypalCommercePlatform
  class StateGuesser
    def initialize(state_name, country)
      @state_name = state_name
      @country = country
    end

    def guess
      carmen_state = state_list.find{ |s| s.name == @state_name || s.code == @state_name }
      return if carmen_state.blank?

      guessed_state = spree_state(carmen_state.name)
      guessed_state || spree_state(carmen_state.parent.name)
    end

    private

    def state_list
      Carmen::Country.coded(@country.iso).subregions.map{ |s| [s, s.subregions] }.flatten
    end

    def spree_state(name)
      ::Spree::State.find_by(name: name)
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
solidus_paypal_commerce_platform-1.0.3 app/models/solidus_paypal_commerce_platform/state_guesser.rb
solidus_paypal_commerce_platform-1.0.2 app/models/solidus_paypal_commerce_platform/state_guesser.rb
solidus_paypal_commerce_platform-0.7.1 app/models/solidus_paypal_commerce_platform/state_guesser.rb
solidus_paypal_commerce_platform-1.0.1 app/models/solidus_paypal_commerce_platform/state_guesser.rb
solidus_paypal_commerce_platform-1.0.0 app/models/solidus_paypal_commerce_platform/state_guesser.rb
solidus_paypal_commerce_platform-0.7.0 app/models/solidus_paypal_commerce_platform/state_guesser.rb
solidus_paypal_commerce_platform-1.0.0.beta3 app/models/solidus_paypal_commerce_platform/state_guesser.rb
solidus_paypal_commerce_platform-1.0.0.beta2 app/models/solidus_paypal_commerce_platform/state_guesser.rb
solidus_paypal_commerce_platform-1.0.0.beta1 app/models/solidus_paypal_commerce_platform/state_guesser.rb
solidus_paypal_commerce_platform-0.6.0 app/models/solidus_paypal_commerce_platform/state_guesser.rb
solidus_paypal_commerce_platform-0.5.0 app/models/solidus_paypal_commerce_platform/state_guesser.rb
solidus_paypal_commerce_platform-0.4.0 app/models/solidus_paypal_commerce_platform/state_guesser.rb
solidus_paypal_commerce_platform-0.3.2 app/models/solidus_paypal_commerce_platform/state_guesser.rb
solidus_paypal_commerce_platform-0.3.1 app/models/solidus_paypal_commerce_platform/state_guesser.rb
solidus_paypal_commerce_platform-0.3.0 app/models/solidus_paypal_commerce_platform/state_guesser.rb
solidus_paypal_commerce_platform-0.2.2 app/models/solidus_paypal_commerce_platform/state_guesser.rb
solidus_paypal_commerce_platform-0.2.1 app/models/solidus_paypal_commerce_platform/state_guesser.rb