geo

package
v0.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2018 License: CC0-1.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// USPSURI is that base address for the USPS API
	USPSURI = "https://secure.shippingapis.com/ShippingAPI.dll"

	// VerifyAPI is the name of that API that handles address verifications
	VerifyAPI = "Verify"

	// USPSErrorCodes contains USPS code mapping to eApp error codes
	USPSErrorCodes = map[string]string{
		"-2147219400":     "error.geocode.city",
		"-2147219401":     "error.geocode.notfound",
		"-2147219403":     "error.geocode.multiple",
		"Generic":         "error.geocode.generic",
		"Default Address": "error.geocode.defaultAddress",
		"Partial":         "error.geocode.partial",
		"System":          "error.geocode.system",
		"80040B19":        "error.geocode.system.xml",
	}
)

Functions

This section is empty.

Types

type ErrUSPSSystem

type ErrUSPSSystem struct {
	Message string
}

ErrUSPSSystem is the expected structure of the USPS system.

func (ErrUSPSSystem) Error

func (e ErrUSPSSystem) Error() string

Error returns the USPS error message.

type Geocoder

type Geocoder interface {
	Validate(Values) (Results, error)
}

Geocoder is an interface for geocoding implementations

var (
	// Geocode is the geocoder to be used by the application. It points to an interface so that the
	// underlying implementation can be easily swapped out
	Geocode Geocoder

	// ErrNoResultsFound is a generic error when results are not found but a request was valid
	ErrNoResultsFound = errors.New("No geolocation results were found")
)

type Result

type Result struct {
	Street    string
	Street2   string
	City      string
	State     string
	County    string
	Country   string
	Zipcode   string
	Formatted string
	Partial   bool
	Error     string
}

Result represents geocoded information that has been transformed from the original source. All Geocoders should convert their location information into a Result struct

func (Result) String

func (r Result) String() string

String returns a friendly representation of a Result

type Results

type Results []Result

Results contains a list of found Result. It contains helper methods to determine if partial matches were found

func (Results) Empty

func (r Results) Empty() bool

Empty determines if any results are available

func (Results) HasErrors

func (r Results) HasErrors() bool

HasErrors checks if any of the results contains error information

func (Results) HasPartial

func (r Results) HasPartial() bool

HasPartial determines if any of the matches is partial

type USPSAddress

type USPSAddress struct {
	// XMLName refers to the name to give the XML tag
	XMLName xml.Name `xml:"Address"`
	// Up to 5 address verifications can be included per transaction.
	// <Address ID="0"></Address><Address ID="1"></Address>
	ID int64 `xml:"ID,attr"`

	// Maximum characters allowed: 38
	FirmName string `xml:"FirmName,omitempty"`

	// Address Line 1 is used to provide an apartment or suite number, if applicable.  Maximum characters allowed: 38
	Address1 string `xml:"Address1"`

	// Street address. Maximum characters allowed: 38
	Address2 string `xml:"Address2"`

	// Maximum characters allowed: 15.  Either <City> and <State> or <Zip5> are required.
	City string `xml:"City"`

	// Maximum characters allowed: 2. Either <City> and <State> or <Zip5> are required.
	State string `xml:"State"`

	// Maximum characters allowed: 28. For Puerto Rico addresses only.
	Urbanization string `xml:"Urbanization"`

	// Maximum characters allowed: 5. Either <City> and <State> or <Zip5> are required.
	Zip5 string `xml:"Zip5"`

	// Input tag exactly as presented, not all caps.  Maximum characters allowed:
	Zip4 string `xml:"Zip4"`

	// Stores error information for an address. USPS returns errors within the address block if they
	// exist. We set this to a pointer so that we can check for <nil>
	Error *USPSError `xml:"Error"`

	// Stores additional information that is not necessarily an error. Some instances may include where an apartment
	// number used does not correspond to a valid base address
	ReturnText string `xml:"ReturnText,omitempty"`
}

USPSAddress represents the structure for the <Address> element information in a USPS request and response. With a successful response, the following is returned

<Address>
  <Address2></Address2>
  <City></City>
  <State></State>
  <Zip5></Zip5>
  <Zip4></Zip4>
</Address>

When there's an error with the address information, the <Error> block is nested within the <Address /> element like the following <AddressValidateResponse>

<Address>
    <Error>
        <Number>-2147219401</Number>
        <Source>clsAMS</Source>
        <Description>Address Not Found.  </Description>
        <HelpFile/>
        <HelpContext/>
    </Error>
</Address>

</AddressValidateResponse>

Therefore, we include an Error field in the Address struct. We make it a pointer so that we can check for <nil> if the value is not populated (no error has occurred).

func (*USPSAddress) FromGeoValues

func (address *USPSAddress) FromGeoValues(geoValues Values)

FromGeoValues populates a USPSAddress using Values

func (*USPSAddress) ToResult

func (address *USPSAddress) ToResult(geoValues Values) (result Result)

ToResult generates a Result struct and determines whether it is a partial match. A partial match occurs when there's a mismatch in values between each corresponding field

type USPSAddressValidateRequest

type USPSAddressValidateRequest struct {
	XMLName xml.Name `xml:"AddressValidateRequest"`
	UserID  string   `xml:"USERID,attr"`
	Address USPSAddress
}

USPSAddressValidateRequest contains the information necessary to execute an address validation webservice request The USERID refers to the api key that must be sent with each request

func (USPSAddressValidateRequest) ToXMLString

func (r USPSAddressValidateRequest) ToXMLString() string

ToXMLString creates a string representation of the xml request

type USPSAddressValidateResponse

type USPSAddressValidateResponse struct {
	XMLName xml.Name    `xml:"AddressValidateResponse"`
	Address USPSAddress `xml:"Address"`
}

USPSAddressValidateResponse contains the information returned from a successful webservice request

type USPSError

type USPSError struct {
	// The error number generated by the Web Tools server.
	Number string `xml:"Number"`

	// The component and interface that generated the error on the Web Tools server.
	Source string `xml:"Source"`

	// The error description
	Description string `xml:"Description"`

	// [reserved for future use according to USPS docs]
	HelpFile string `xml:"HelpFile"`

	// [reserved for future use according to USPS docs]
	HelpContext string `xml:"HelpContext"`
}

USPSError is the structure for responses resulting in an error

type USPSErrorResponse

type USPSErrorResponse struct {
	XMLName xml.Name `xml:"Error"`
	USPSError
}

USPSErrorResponse stores a system level error

type USPSGeocoder

type USPSGeocoder struct {
	// contains filtered or unexported fields
}

USPSGeocoder geocodes address information using the United States Post Office webservice API docs can be found https://www.usps.com/business/web-tools-apis/address-information-api.htm

func NewTestUSPSGeocoder

func NewTestUSPSGeocoder(userID string, baseURI string) *USPSGeocoder

NewTestUSPSGeocoder is used for mocking purposes. We can instantiate a new instance and set the URL to that of an http test mock server

func NewUSPSGeocoder

func NewUSPSGeocoder(userID string) *USPSGeocoder

NewUSPSGeocoder creates a new instance of USPS Geocoder

func (USPSGeocoder) Validate

func (g USPSGeocoder) Validate(geoValues Values) (Results, error)

Validate takes values to be geocoded and executes a web service call

type Values

type Values struct {
	Street  string
	Street2 string
	City    string
	State   string
	Zipcode string
	County  string
	Country string
}

Values stores generic geospatial related query parameters

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL