(Quick Reference)

5 Example - Reference Documentation

Authors: Aaron J. Zirbes

Version: 0.1.2

5 Example

Examples

Tag examples

In order to use the tags, you need need to install the jquery plugin.

You may install an example GSP page to demonstrate the tags by running the command

grails install-address-lookup-example

Code Examples

You can create an address, look it up, and get information about the lookup by using the following code.

The StreetAddress.lookup() method uses the com.semaphorecorp.zp4.AddressService to look up the address, and return an instance of a com.semaphorecorp.zp4.Zp4Address that contains the lookup result and some error message information.

import com.semaphorecorp.zp4.*

def streetAddressInstance = new StreetAddress( address: '176 N. Mississippi River Boulevard', city: 'St. Paul', state: 'MN')

def zp4AddressIntance = streetAddressInstance.lookup()

def cleanedStreetAddressInstance = AddressIntance.toStreetAddress()

assert zp4AddressIntance.certified == 'C' assert zp4AddressIntance.errorMessage == 'Acceptable city name used' assert cleanedStreetAddressInstance.address == '176 Mississippi River Blvd N' assert cleanedStreetAddressInstance.city == 'Saint Paul' assert cleanedStreetAddressInstance.state == 'MN' assert cleanedStreetAddressInstance.zipCode == 55104 assert cleanedStreetAddressInstance.zip4 == 5613

Extending your own Address domain class

You can add some code to your BootStrap.groovy to add a meta-class to your own domain class that contains an address what follows is a simple example.

package com.mycompany

class Customer { String name String addressLine String city String stateAbbreviation String zipcode Boolean cleaned = false }

in BootStrap.groovy

com.mycompany.Customer.metaClass.cleanUp = {->
	Integer z = 0
	z = Integer.parse(delegate.zipcode)
	def a1 = new com.semaphorecorp.zp4.StreetAddress(address: delegate.addressLine,
			city: delegate.city,
			state: delegate.stateAbbreviation,
			zipCode: z )
	def za = a.lookup()
	if (za.certified == 'C') {
		def a2 = a.lookup().toStreetAddress()
		delegate.addressLine = a2.address
		delegate.city = a2.city
		delegate.stateAbbreviation = a2.state
		delegate.zipcode = "${a2.zipCode}-${a2.zip4}"
		delegate.cleaned = true
	} else {
		delegate.cleaned = false
	}
}