forked from mattevans/abode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabode.go
107 lines (94 loc) · 3.24 KB
/
abode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package abode
import (
"context"
"errors"
"fmt"
"strings"
"github.com/snikch/api/config"
"googlemaps.github.io/maps"
)
var client *maps.Client
var addressLineDelimeter = ","
// Address represents a response Address from abode.
type Address struct {
AddressLine1 *string `json:"address_line1"`
AddressLine2 *string `json:"address_line2"`
AddressCity *string `json:"address_city"`
AddressState *string `json:"address_state"`
AddressCountry *string `json:"address_country"`
AddressCountryCode *string `json:"address_country_code"`
AddressZip *string `json:"address_zip"`
AddressLat *float64 `json:"address_lat"`
AddressLng *float64 `json:"address_lng"`
FormattedAddress *string `json:"formatted_address"`
}
// initClient will initalize a Google Maps API client.
func initClient() error {
var err error
key := config.PrivateString("GOOGLE_MAPS_API_KEY")
if key == "" {
return errors.New("Please configure a `GOOGLE_MAPS_API_KEY`")
}
client, err = maps.NewClient(maps.WithAPIKey(key))
return err
}
// Explode takes a one-line address string, explodes it and returns an *Address
func Explode(address string) (*Address, error) {
if client == nil {
err := initClient()
if err != nil {
return nil, err
}
}
// Build the API request.
req := &maps.GeocodingRequest{
Address: address,
}
// Execute the request.
resp, err := client.Geocode(context.Background(), req)
if len(resp) < 1 {
return nil, err
}
if err != nil {
return nil, err
}
// Using the first/closest match in our response, grab the values we need.
components := resp[0].AddressComponents
formattedAddress := resp[0].FormattedAddress
lat := resp[0].Geometry.Location.Lat
lng := resp[0].Geometry.Location.Lng
// Construct the return *Address{}
response := &Address{
AddressLine1: compose(addressLine1Composition, "", components, false),
AddressLine2: compose(addressLine2Composition, addressLineDelimeter, components, false),
AddressCity: compose(addressCityComposition, addressLineDelimeter, components, false),
AddressState: compose(addressStateComposition, addressLineDelimeter, components, false),
AddressCountry: compose(addressCountryComposition, addressLineDelimeter, components, false),
AddressCountryCode: compose(addressCountryCodeComposition, addressLineDelimeter, components, true),
AddressZip: compose(addressPostalCodeComposition, addressLineDelimeter, components, false),
AddressLat: &lat,
AddressLng: &lng,
FormattedAddress: &formattedAddress,
}
return response, err
}
func compose(composition []string, delimeter string, components []maps.AddressComponent, useShortName bool) *string {
var str string
for _, element := range composition {
component := getComponentByType(components, element)
if useShortName {
if component != nil && !strings.Contains(str, component.ShortName) {
str = fmt.Sprintf("%s %s%s", str, component.ShortName, delimeter)
}
} else {
if component != nil && !strings.Contains(str, component.LongName) {
str = fmt.Sprintf("%s %s%s", str, component.LongName, delimeter)
}
}
}
if str == "" {
return nil
}
str = strings.TrimPrefix(strings.TrimSuffix(str, delimeter), " ")
return &str
}