Module: UnitedStates
- Defined in:
- lib/united_states.rb,
lib/united_states/version.rb,
lib/united_states/state/name.rb,
lib/united_states/state/designation.rb,
lib/united_states/state/postal_code.rb
Overview
Top-level namespace for this gem.
Defined Under Namespace
Modules: State Classes: NoDesignationFoundError
Constant Summary collapse
- VERSION =
'1.2.2'.freeze
Class Method Summary collapse
-
.[](name_or_postal_code) ⇒ UnitedStates::State::Desgination
The State Desgination matching the provided name or postal code.
-
.all ⇒ Array<UnitedStates::State::Designation>
A collection of all U.S.
-
.array_from_hashes(*hashes) ⇒ Array<UnitedStates::State::Designation>
A collection of state designations made from the given hash attributes.
-
.array_from_yaml(yaml) ⇒ Array<UnitedStates::State::Designation>
A collection of state designations made from the given hash attributes.
-
.array_from_yaml_file(path:) ⇒ Array<UnitedStates::State::Designation>
A collection of state designations made from the YAML file.
-
.config_path ⇒ String
The path to the Designations yaml file.
-
.find_by_name(name) ⇒ UnitedStates::State::Desgination
The State Desgination matching the provided name.
-
.find_by_postal_code(postal_code) ⇒ UnitedStates::State::Designation
The state Designation matching the provided postal code.
-
.names ⇒ Array<UnitedStates::State::Name>
A collection of all U.S.
-
.postal_codes ⇒ Array<UnitedStates::State::PostalCode>
A collection of all U.S.
Class Method Details
.[](name_or_postal_code) ⇒ UnitedStates::State::Desgination
Returns the State Desgination matching the provided name or postal code.
29 30 31 32 33 34 |
# File 'lib/united_states.rb', line 29 def self.[](name_or_postal_code) name_or_postal_code = name_or_postal_code.to_s invalid_postal_code = name_or_postal_code.length != 2 return find_by_name(name_or_postal_code) if invalid_postal_code find_by_postal_code(name_or_postal_code) end |
.all ⇒ Array<UnitedStates::State::Designation>
Returns a collection of all U.S. State Designations.
38 39 40 |
# File 'lib/united_states.rb', line 38 def self.all array_from_yaml_file(path: config_path) end |
.array_from_hashes(*hashes) ⇒ Array<UnitedStates::State::Designation>
Returns a collection of state designations made from the given hash attributes.
50 51 52 53 54 |
# File 'lib/united_states.rb', line 50 def self.array_from_hashes(*hashes) hashes.map do |hash| UnitedStates::State::Designation.from_hash(hash) end end |
.array_from_yaml(yaml) ⇒ Array<UnitedStates::State::Designation>
Returns a collection of state designations made from the given hash attributes.
70 71 72 73 74 75 76 |
# File 'lib/united_states.rb', line 70 def self.array_from_yaml(yaml) return [] unless YAML.safe_load(yaml) YAML.safe_load(yaml).map do |key, value| UnitedStates::State::Designation.new( name: key, postal_code: value && value.fetch('postal_code', '')) end end |
.array_from_yaml_file(path:) ⇒ Array<UnitedStates::State::Designation>
Returns a collection of state designations made from the YAML file.
91 92 93 94 95 96 |
# File 'lib/united_states.rb', line 91 def self.array_from_yaml_file(path:) pathname = Pathname.new(path) return array_from_yaml(pathname.read) if pathname.exist? raise "\"#{path}\" does not exist.\n"\ 'Please supply a path to a YAML file.' end |
.config_path ⇒ String
Returns the path to the Designations yaml file.
100 101 102 |
# File 'lib/united_states.rb', line 100 def self.config_path Pathname.new(__FILE__).parent.join('united_states/designations.yml') end |
.find_by_name(name) ⇒ UnitedStates::State::Desgination
Returns the State Desgination matching the provided name.
112 113 114 115 116 |
# File 'lib/united_states.rb', line 112 def self.find_by_name(name) name = UnitedStates::State::Name.new(name) all.find { |designation| designation.name == name } || raise( NoDesignationFoundError, "No State named, \"#{name},\" was found.") end |
.find_by_postal_code(postal_code) ⇒ UnitedStates::State::Designation
Returns the state Designation matching the provided postal code.
126 127 128 129 130 131 |
# File 'lib/united_states.rb', line 126 def self.find_by_postal_code(postal_code) postal_code = UnitedStates::State::PostalCode.new(postal_code) all.find { |designation| designation.postal_code == postal_code } || raise( NoDesignationFoundError, "No State with postal code, \"#{postal_code},\" was found.") end |
.names ⇒ Array<UnitedStates::State::Name>
Returns a collection of all U.S. State Names.
135 136 137 |
# File 'lib/united_states.rb', line 135 def self.names all.map(&:name) end |
.postal_codes ⇒ Array<UnitedStates::State::PostalCode>
Returns a collection of all U.S. State postal codes.
141 142 143 |
# File 'lib/united_states.rb', line 141 def self.postal_codes all.map(&:postal_code) end |