module Pippa
The Pippa
API.
Bundler auto-generated mixin for gem version.
Constants
- VERSION
Current version of
Pippa
Public Class Methods
Compress the zipcode CSV file to something that loads quicker.
This gem is packaged with compressed zipcode table that only works if your ruby has compatible a Marshal
version. If loading fails, it will attempt to rewrite the table in a compatible format, but if you don’t have write permission in the gem store, that will consistently fail. You can use this call with +sudo∂ irb+ to write a compatible table.
# File lib/pippa.rb, line 28 def self.compress_zipcodes dump_zips.size end
Return a list of the valid map names.
# File lib/pippa.rb, line 16 def self.map_names Map.info[:map].keys end
Run the profiler and record results. (For development.)
# File lib/pippa.rb, line 57 def self.profile require 'ruby-prof' RubyProf.start Map.write_zipcode_maps result = RubyProf.stop File.open('profile.htm', 'w') do |f| RubyProf::GraphHtmlPrinter.new(result).print(f) end end
Return a hash mapping zip codes to CSV records of zip code data. NB: The file is big, so this takes a while to return the first time called.
CSV::Row
struct format (see also ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Row.html):
#<CSV::Row zipcode:"97475" zip_code_type:"PO BOX" city:"SPRINGFIELD" state:"OR" location_type:"PRIMARY" lat:44.05 long:-123.02 location:"NA-US-OR-SPRINGFIELD" decommisioned:"false" tax_returns_filed:nil estimated_population:nil total_wages:nil>
See federalgovernmentzipcodes.us for more information on the zipcode data.
# File lib/pippa.rb, line 52 def self.zips @@zips ||= zips_from_file end
Private Class Methods
Use native Ruby facility to create a quick-loading, compatible version of CSV zipcode data.
# File lib/pippa.rb, line 483 def self.dump_zips hash = {} zips_from_csv.each do |zip, row| hash[zip] = { :lat => row[:lat], :lon => row[:long] } end File.open(ZIPCODE_DUMP_FILE_PATH, 'wb') {|f| Marshal.dump(hash, f) } hash rescue hash end
Read zipcode data from unaltered CSV file.
# File lib/pippa.rb, line 454 def self.zips_from_csv CSV::HeaderConverters[:underscore_symbol] = lambda do |s| t = s.gsub(/::/, '/') t.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') t.gsub!(/([a-z\d])([A-Z])/,'\1_\2') t.tr!("-", "_") t.downcase! t.to_sym end CSV::Converters[:custom] = lambda do |s, info| begin [:lat, :long].include?(info.header) ? Float(s) : s rescue s end end zips = {} CSV.foreach(ZIPCODE_CSV_FILE_PATH, :headers => :first_row, :header_converters => :underscore_symbol, :converters => :custom) do |row| zips[row[:zipcode]] = row if row[:lat] && row[:long] end zips end
Read a previously created dump of the zipcode database.
# File lib/pippa.rb, line 495 def self.zips_from_dump File.open(ZIPCODE_DUMP_FILE_PATH, 'rb') {|f| Marshal.load(f) } end
Read zipcode data. Tries compressed form first and then CSV. Format:
"Zipcode","ZipCodeType","City","State","LocationType","Lat","Long", "Location","Decommisioned","TaxReturnsFiled","EstimatedPopulation","TotalWages"
# File lib/pippa.rb, line 503 def self.zips_from_file zips_from_dump rescue dump_zips end