Sha256: 93aa4d9e7ecb52c82b7768546c6ae816abdde80d41aeede9476e9939298f71b6

Contents?: true

Size: 830 Bytes

Versions: 1

Compression:

Stored size: 830 Bytes

Contents

class IsoCountryCodes # :nodoc:
  VERSION = '0.1.1'

  class UnknownCodeError < StandardError; end

  class << self
    def all
      Code.all
    end

    def find(code)
      code     = code.to_s.upcase
      instance = nil

      if code.match(/^\d{2}$/)
        code = "0#{code}" # Make numeric codes three digits
      end

      if code.match(/^\d{3}$/)
        instance = all.select { |c| c.numeric == code }.first
      elsif code.match(/^[A-Z]{2}$/)
        instance = all.select { |c| c.alpha2 == code }.first
      elsif code.match(/^[A-Z]{3}$/)
        instance = all.select { |c| c.alpha3 == code }.first
      else
        instance = all.select { |c| c.name.upcase == code }.first
      end

      raise UnknownCodeError, "ISO 3166-1 code '#{code}' does not exist." if instance.nil?

      instance
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alexrabarts-iso_country_codes-0.1.1 lib/iso_country_codes/iso_country_codes.rb