Sha256: 9d468ed9b50492a01abb12d4189e4bdb55bbad5f99d4c5cd5c370f7a20ab2928

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

# coding: utf-8

module Locus
  class Place
    attr_accessor :country, :postal_code, :state_code

    def initialize(attributes = {})
      attributes.each do |key, value|
        send("#{key}=", value)
      end
    end

    # Find a place by postal code and country.
    #
    # @param postal_code [String] The postal code.
    # @param country [Symbol] Country symbol.
    #
    # @example
    #   Locus::Place.find_by_postal_code '12053', :de  # => 'BE'
    #
    # @return [Place] The {Place} or nil.
    def self.find_by_postal_code(postal_code, country = Locus.default_country)
      return nil unless state_code = state_code(postal_code, country)
      self.new(country: country,
               postal_code: postal_code,
               state_code: state_code)
    end

    private

      # Get the state code for a given postal code and country.
      #
      # @return [String] The state code or nil.
      def self.state_code(postal_code, country)
        return nil unless country = places[country]
        country[postal_code]
      end

      def self.places
        @places ||= YAML.load_file Locus.zip_path
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
locus-0.1.0 lib/locus/place.rb