Sha256: f04e20b8b47b8562c3ad3dd228caf7c197c3ab4e9b49a941beec87f092eb2b4a

Contents?: true

Size: 718 Bytes

Versions: 1

Compression:

Stored size: 718 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 /^[0-9]{5}$/i
  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

1 entries across 1 versions & 1 rubygems

Version Path
zip-code-info-0.1.0 lib/zip-code-info.rb