Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
module ApIzer
class State
attr_reader :to_fips_index, :to_full_index, :to_usps_index, :to_ap_index
# >> ApIzer::State.find('Alabama').usps
# AL
# >> ApIzer::State.find('AL').ap
# Ala.
STATES = [
["Alabama", "AL", "Ala.", 1],
['Alaska', "AK", "Alaska", 2],
["Arizona", "AZ", "Ariz.", 4],
["Arkansas", 'AR', "Ark.", 5],
["California", "CA", "Calif.", 6],
["Colorado", "CO", "Colo.", 8],
["Connecticut", "CT", "Conn.", 9],
["Delaware", "DE", "Del.", "10"],
["District of Columbia", "DC", "D.C.", 11],
["Florida", "FL", "Fla.", 12],
["Georgia", "GA", "Ga.", 13],
["Hawaii", "HI", "Hawaii", 15],
["Idaho", 'ID', "Idaho", 16],
["Illinois", "IL", "Ill.", 17],
["Indiana", "IN", "Ind.", 18],
["Iowa", "IA", "Iowa", 19],
["Kansas", "KS", "Kan.", 20],
["Kentucky", "KY", "Ky.", 21],
["Louisiana", "LA", "La.", 22],
["Maine", "ME", "Maine", 23],
["Maryland", "MD", "Md.", 24],
["Massachusetts", "MA", "Mass.", 25],
["Michigan", "MI", "Mich.", 26],
["Minnesota", "MN", "Minn.", 27],
["Mississippi", "MS", "Miss.", 28],
["Missouri", "MO", "Mo.", 29],
["Montana", "MT", "Mont.", 30],
["Nebraska", "NE", "Neb.", 31],
["Nevada", "NV", "Nev.", 32],
["New Hampshire", "NH", "N.H.", 33],
["New Jersey", "NJ", "N.J.", 34],
["New Mexico", "NM", "N.M.", 35],
["New York", "NY", "N.Y.", 36],
["North Carolina", "NC", "N.C.", 37],
["North Dakota", "ND", "N.D.", 38],
["Ohio", "OH", "Ohio", 39],
["Oklahoma", "OK", "Okla.", 40],
["Oregon", "OR", "Ore.", 41],
["Pennsylvania", "PA", "Pa.", 42],
["Rhode Island", "RI", "R.I.", 44],
["South Carolina", "SC", "S.C.", 45],
["South Dakota", "SD", "S.D.", 46],
["Tennessee", "TN", "Tenn.", 47],
["Texas", "TX", "Texas", 48],
["Utah", "UT", "Utah", 49],
["Vermont", "VT", "Vt.", 50],
["Virginia", "VA", "Va.", 51],
["Washington", "WA", "Wash.", 53],
["West Virginia", "WV", "W.Va.", 54],
["Wisconsin", "WI", "Wis.", 55],
["Wyoming", "WY", "Wyo.", 56],
["Puerto Rico", "PR", "P.R.", 72],
["Virgin Islands", "VI", "V.I.", 78],
["Guam", "GU","Guam", 66],
["American Samoa","AS","A.S.",60],
["Northern Mariana Islands","MP", "M.P.", 69]
]
STATEFACE = {"VA"=>"s", "ND"=>"b", "AL"=>"B", "NY"=>"h", "NE"=>"c", "MD"=>"T", "DE"=>"H", "MN"=>"W", "CO"=>"F", "RI"=>"m", "HI"=>"K", "ME"=>"U", "WY"=>"x", "MO"=>"X", "IA"=>"L", "AZ"=>"D", "OH"=>"i", "IL"=>"N", "KY"=>"Q", "OR"=>"k", "GA"=>"J", "NH"=>"d", "TN"=>"p", "TX"=>"q", "IN"=>"O", "SC"=>"n", "ID"=>"M", "NJ"=>"e", "AR"=>"C", "FL"=>"I", "MS"=>"Y", "SD"=>"o", "OK"=>"j", "CT"=>"G", "PA"=>"l", "MI"=>"V", "MT"=>"Z", "WI"=>"v", "KS"=>"P", "CA"=>"E", "VT"=>"t", "NV"=>"g", "UT"=>"r", "NM"=>"f", "MA"=>"S", "WV"=>"w", "AK"=>"A", "LA"=>"R", "NC"=>"a", "WA"=>"u"}
def initialize(s)
@to_fips_index = 3
@to_full_index = 0
@to_usps_index = 1
@to_ap_index = 2
@state = STATES.detect { |x|
x if x.include?(s)
}
end
def to_stateface
STATEFACE[@state[1]]
end
def method_missing(method, *args)
if respond_to?("#{method.to_s}_index")
return @state[send("#{method.to_s}_index")] unless
@state[send("#{method.to_s}_index")].nil?
else
super
end
end
class << self
# return a state from a string
def find(s)
State.new(s)
end
end
end
end
class Date
AP_MONTHNAMES = [nil] + %w(Jan. Feb. March April May June July Aug. Sep. Oct. Nov. Dec.)
def to_ap
zeroless_day = self.strftime("%d").gsub(/^0/, '')
ap_month = AP_MONTHNAMES[self.strftime("%m").to_i]
ap_year = self.strftime("%Y")
return "#{ap_month} #{zeroless_day}, #{ap_year}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment