Module: DialAZip

Included in:
Endpost
Defined in:
lib/dial_a_zip.rb

Constant Summary collapse

DIAL_A_ZIP_BASE_URL =
'http://www.dial-a-zip.com/XML-Dial-A-ZIP/DAZService.asmx'
DIAL_A_ZIP_RESPONSE_MESSAGES =
{
  '10' => 'More than one delivery address was detected',
  '11' => 'ZIP Code could not be found',
  '12' => 'The State code is invalid',
  '13' => 'The City is invalid',
  '21' => 'The address as submitted could not be found',
  '22' => 'More than one ZIP code was found',
  '25' => 'The Street address is invalid',
  '31' => 'Exact Match',
  '32' => 'More information, such as an apartment or suite number, may give a more specific address.',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dial_a_zip_passwordObject

Returns the value of attribute dial_a_zip_password.



16
17
18
# File 'lib/dial_a_zip.rb', line 16

def dial_a_zip_password
  @dial_a_zip_password
end

#dial_a_zip_userObject

Returns the value of attribute dial_a_zip_user.



16
17
18
# File 'lib/dial_a_zip.rb', line 16

def dial_a_zip_user
  @dial_a_zip_user
end

Instance Method Details

#verify_address(address) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dial_a_zip.rb', line 18

def verify_address(address)
  xml = %!
  <VERIFYADDRESS>
    <COMMAND>ZIP1</COMMAND>
    <SERIALNO>#{dial_a_zip_user}</SERIALNO>
    <USER>#{dial_a_zip_user}</USER>
    <PASSWORD>#{dial_a_zip_password}</PASSWORD>
    <ADDRESS0></ADDRESS0>
    <ADDRESS1>#{address[:full_name]}</ADDRESS1>
    <ADDRESS2>#{address[:address]}</ADDRESS2>
    <ADDRESS3>#{address[:city]}, #{address[:state]} #{address[:zipcode]}</ADDRESS3>
  </VERIFYADDRESS>!

  begin
    response = RestClient.post "#{DIAL_A_ZIP_BASE_URL}/MethodZIPValidate", :input => xml

    response_xml = Nokogiri::XML(response.body)

    return_code_node_xml = response_xml.css('Dial-A-ZIP_Response ReturnCode').first
    return_code = return_code_node_xml ? return_code_node_xml.text : nil

    addr_exists_node_xml = response_xml.css('Dial-A-ZIP_Response AddrExists').first
    addr_exists = addr_exists_node_xml ? addr_exists_node_xml.text : nil

    if return_code == '31' && addr_exists == 'TRUE'
      return {
        :full_name => response_xml.css('Dial-A-ZIP_Response AddrLine2').first.text,
        :address => response_xml.css('Dial-A-ZIP_Response AddrLine1').first.text,
        :city => response_xml.css('Dial-A-ZIP_Response City').first.text,
        :state => response_xml.css('Dial-A-ZIP_Response State').first.text,
        :zipcode => [response_xml.css('Dial-A-ZIP_Response ZIP5').first.text, response_xml.css('Dial-A-ZIP_Response Plus4').first.text].join('-'),
      }
    else
      fail DIAL_A_ZIP_RESPONSE_MESSAGES[return_code]
    end

  rescue => e
    fail e.to_s
  end
end