Class: USPS::Response::CityAndStateLookup
- Defined in:
- lib/usps/response/city_and_state_lookup.rb
Defined Under Namespace
Classes: Result
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#data ⇒ Object
Returns all city/state data from the query results.
-
#get(zip) ⇒ Object
(also: #[])
Returns a single city/state pair given a zip5.
-
#initialize(xml) ⇒ CityAndStateLookup
constructor
A new instance of CityAndStateLookup.
-
#to_h ⇒ Object
Returns all city/state data as a pure Ruby hash (e.g. no Structs as values).
Methods inherited from Base
Constructor Details
#initialize(xml) ⇒ CityAndStateLookup
Returns a new instance of CityAndStateLookup.
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/usps/response/city_and_state_lookup.rb', line 6 def initialize(xml) @data = {} xml.search('ZipCode').each do |node| zip = node.search('Zip5').text.to_i @data[zip] = Result.new( zip, node.search('City').text, node.search('State').text ) end end |
Instance Method Details
#data ⇒ Object
Returns all city/state data from the query results
27 28 29 |
# File 'lib/usps/response/city_and_state_lookup.rb', line 27 def data @data end |
#get(zip) ⇒ Object Also known as: []
Returns a single city/state pair given a zip5
21 22 23 |
# File 'lib/usps/response/city_and_state_lookup.rb', line 21 def get(zip) @data[zip.to_i] end |
#to_h ⇒ Object
Returns all city/state data as a pure Ruby hash (e.g. no Structs as values)
32 33 34 35 36 37 38 39 |
# File 'lib/usps/response/city_and_state_lookup.rb', line 32 def to_h hash = {} @data.each_pair do |key, value| hash[key] = value.to_h end hash end |