11
12 if params[:address].nil?
13 @address = Address.new
14 else
15 @address = Address.find(params[:id])
16 if @address.update_attributes(params[:address])
17 flash[:notice] = ‘Address was successfully updated.’
18 end
19 end
20 end
21
22 end #cityStateSearch
Line 01 starts the definition for the new action cityStateSearch. Unlike other actions in this controller,
the cityStateSearch action is called asynchronously by the JavaScript—the client-side Ajax code.
Line 03 checks to see if the parameter value is null (or nil, as Ruby calls it). Line 05 is a regular
expression that compares the parameter string value against /\d{5}/, which we all know and love
as the regular expression for five digits. The exclamation point before the expression negates the
elsif expression. (Yes, that’s correct RoR syntax for what is otherwise known as else if.)
Lines 12-19 handle creating or updating the @address object. There’s a little subtlety in lines 5-7.
When the logic falls into these lines, it drops out of the action and returns to the browser. The end
user is none the wiser. This action skips the rest of the logic and returns right away when the zip5
is nil or is not 5 digits. As long as the user’s cursor is in the zip5 input text field on the view, then
this action gets reinvoked every two seconds. That functionality was configured earlier in the
observe_field method in the frequency parameter.
Line 21 is where you add the next section of code. Because this is development code, I use lots of
debug statements. After the code has been polished and refined several times, I’ll remove the
logger.debug statements. Also, I’m an RoR newbie, so lots of debug statements give comfort to
my style of programming, which has been accurately described elsewhere as “beat it into shape.”
Step 5: Create a valid XML request to send to the USPS Web service
At this point in the process, you’re in the server side of your RoR application, and you have a ZIP
code with five digits. Because you have a reasonable expectation that the ZIP code is legitimate, it’s
now worth the effort to call the USPS Web service. To do this, you need to create a valid request.
Jumping ahead a little, Listing 6 shows an example of a valid XML request.
Listing 6. Valid XML request
http://testing.shippingapis.com/ShippingAPITest.dll?API=CityStateLookup
&XML=<CityStateLookupRequest%20USERID=”XXXXXXXXXXXX”><ZipCode ID=
“0”><Zip5>90210</Zip5></ZipCode></CityStateLookupRequest>
To use the USPS Web Tools, you have to register with them. Registration is easy and free (see the
Resources section for more information). After I registered for the USPS Web Tools, they sent me
the name of the test server and a user ID. (In Listing 6 I did my best impersonation of a top-secret
government agent by crossing out my user ID with XXXXXXXXXXXX.) Let’s parse this request a
little more. The Web service endpoint is specified by API=CityStateLookup. In the HTML form, you
can now see why I called the input field zip5. That’s the name that the USPS request expects. This
CityStateLookup Web service accepts up to five ZIP code values in one request. To keep things |