Sha256: 084867c97091ef842f73aeedd1020bebe95ce013c66544e446fa9e79da003491

Contents?: true

Size: 673 Bytes

Versions: 2

Compression:

Stored size: 673 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

2 entries across 2 versions & 1 rubygems

Version Path
solidus_paypal_commerce_platform-0.2.0 app/models/solidus_paypal_commerce_platform/state_guesser.rb
solidus_paypal_commerce_platform-0.1.0 app/models/solidus_paypal_commerce_platform/state_guesser.rb