Sha256: 282534c8f4f2faae7027bb22cd1fbb58859ca1af319d390e80a5747768ca08bf

Contents?: true

Size: 733 Bytes

Versions: 2

Compression:

Stored size: 733 Bytes

Contents

require 'yaml'
require 'singleton'

class ZipCodeInfo
  include Singleton

  attr_reader :code

  def initialize
    @data = YAML.load(File.open(File.join(File.dirname(__FILE__), '..', 'data', 'data.yml')))
  end

  def scf_city_for(code = '')
    return false unless assign_and_validate_code(code)
    @data[code_to_key][:city]
  end

  def state_for(code = '')
    return false unless assign_and_validate_code(code)
    @data[code_to_key][:state]
  end


  private

  def assign_and_validate_code(code)
    @code = code.to_s
    return @code.match(/^\d{5}(-\d{4})?$/i) && @data.has_key?(code_to_key)
  end

  # To look up information we only need the first three digits of the zip code
  def code_to_key
    @code[0..2]
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
zip-code-info-0.2.1 lib/zip-code-info.rb
zip-code-info-0.2.0 lib/zip-code-info.rb