Created
March 21, 2013 19:50
-
-
Save willywos/5216126 to your computer and use it in GitHub Desktop.
State.rb - Ruby Class to deal with the United States of AMERICA!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class State | |
#usage: | |
#State.new.state_by_abbreviation("WY") | |
#=> "Wyoming" | |
#State.new.abbreviation_by_state("Wyoming") | |
#=> "WY" | |
#State.new.abbreviations | |
#-Returns all abbreviations | |
#State.new.names | |
#-Returns all States Names | |
#State.new.valid?("WyOmInG") | |
#->true | |
#State.new.valid?("Hobbiton") | |
#->false | |
def initialize | |
end | |
def state_by_abbreviation(abbreviation) | |
return nil if abbreviation.nil? | |
return nil if abbreviation.empty? | |
states.fetch(abbreviation.upcase, nil) | |
end | |
def abbreviation_by_state(state) | |
return nil if state.nil? | |
return nil if state.empty? | |
states.key(state.capitalize) | |
end | |
def names | |
states.values | |
end | |
def abbreviations | |
states.keys | |
end | |
def valid?(state) | |
return false if state.nil? | |
return false if state.empty? | |
return false if state.length < 2 | |
if state.length == 2 | |
state_by_abbreviation(state).nil? ? false : true | |
else | |
abbreviation_by_state(state).nil? ? false : true | |
end | |
end | |
def states | |
{ | |
'AL' => 'Alabama', | |
'AK' => 'Alaska', | |
'AS' => 'American Samoa', | |
'AZ' => 'Arizona', | |
'AR' => 'Arkansas', | |
'CA' => 'California', | |
'CO' => 'Colorado', | |
'CT' => 'Connecticut', | |
'DE' => 'Delaware', | |
'DC' => 'District of Columbia', | |
'FL' => 'Florida', | |
'GA' => 'Georgia', | |
'GU' => 'Guam', | |
'HI' => 'Hawaii', | |
'ID' => 'Idaho', | |
'IL' => 'Illinois', | |
'IN' => 'Indiana', | |
'IA' => 'Iowa', | |
'KS' => 'Kansas', | |
'KY' => 'Kentucky', | |
'LA' => 'Louisiana', | |
'ME' => 'Maine', | |
'MH' => 'Marshall Islands', | |
'MD' => 'Maryland', | |
'MA' => 'Massachusetts', | |
'MI' => 'Michigan', | |
'MN' => 'Minnesota', | |
'MS' => 'Mississippi', | |
'MO' => 'Missouri', | |
'MT' => 'Montana', | |
'NE' => 'Nebraska', | |
'NV' => 'Nevada', | |
'NH' => 'New Hampshire', | |
'NJ' => 'New Jersey', | |
'NM' => 'New Mexico', | |
'NY' => 'New York', | |
'NC' => 'North Carolina', | |
'ND' => 'North Dakota', | |
'OH' => 'Ohio', | |
'OK' => 'Oklahoma', | |
'OR' => 'Oregon', | |
'PW' => 'Palau', | |
'PA' => 'Pennsylvania', | |
'PR' => 'Puerto Rico', | |
'RI' => 'Rhode Island', | |
'SC' => 'South Carolina', | |
'SD' => 'South Dakota', | |
'TN' => 'Tennessee', | |
'TX' => 'Texas', | |
'UT' => 'Utah', | |
'VT' => 'Vermont', | |
'VI' => 'Virgin Islands', | |
'VA' => 'Virginia', | |
'WA' => 'Washington', | |
'WV' => 'West Virginia', | |
'WI' => 'Wisconsin', | |
'WY' => 'Wyoming' | |
} | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "spec_helper" | |
describe State do | |
before(:all) do | |
@states = State.new | |
end | |
it "should return only abbreviations" do | |
states = @states.abbreviations | |
states.should include "AL" | |
states.should include "WY" | |
end | |
it "should return only names of states" do | |
states = @states.names | |
states.should include "Alabama" | |
states.should include "Wyoming" | |
end | |
it "should return the abbreviation based on the state name" do | |
@states.abbreviation_by_state("Wyoming").should eq "WY" | |
@states.abbreviation_by_state("wyoming").should eq "WY" | |
@states.abbreviation_by_state("WYOMING").should eq "WY" | |
@states.abbreviation_by_state("wYoMiNg").should eq "WY" | |
@states.abbreviation_by_state("").should eq nil | |
@states.abbreviation_by_state(" ").should eq nil | |
end | |
it "should return the state based on the abbreviation" do | |
@states.state_by_abbreviation("WY").should eq "Wyoming" | |
@states.state_by_abbreviation("wy").should eq "Wyoming" | |
@states.state_by_abbreviation("wY").should eq "Wyoming" | |
@states.state_by_abbreviation("Wy").should eq "Wyoming" | |
@states.state_by_abbreviation("").should eq nil | |
@states.state_by_abbreviation(" ").should eq nil | |
end | |
it "should return false if valid is passed a state with a nil value" do | |
@states.valid?(nil).should eq false | |
end | |
it "should return false if valid is passed a state with a blank or empty value" do | |
@states.valid?("").should eq false | |
end | |
it "should return false if valid is passed a state with less than 2 characters" do | |
@states.valid?("A").should eq false | |
end | |
it "should return false if abbreviation is not found" do | |
@states.valid?("BA").should eq false | |
end | |
it "should return true if abbreviation is found" do | |
@states.valid?("GA").should eq true | |
end | |
it "should return false if state is not found" do | |
@states.valid?("CRAP").should eq false | |
end | |
it "should return true if state is found" do | |
@states.valid?("Maine").should eq true | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment