Class: USPS::Response::CityAndStateLookup

Inherits:
Base
  • Object
show all
Defined in:
lib/usps/response/city_and_state_lookup.rb

Defined Under Namespace

Classes: Result

Instance Attribute Summary

Attributes inherited from Base

#raw

Instance Method Summary collapse

Methods inherited from Base

parse

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

#dataObject

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_hObject

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