This repository has been archived by the owner on Nov 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
verifier.go
75 lines (67 loc) · 1.9 KB
/
verifier.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
package processor
import (
"encoding/json"
"net/http"
"net/url"
)
type HTTPClient interface {
Do(*http.Request) (*http.Response, error)
}
type SmartyVerifier struct {
client HTTPClient
}
func NewSmartyVerifier(client HTTPClient) *SmartyVerifier {
return &SmartyVerifier{
client: client,
}
}
func (this *SmartyVerifier) Verify(input AddressInput) AddressOutput {
response, _ := this.client.Do(this.buildRequest(input))
candidates := this.decodeResponse(response)
return this.prepareAddressOutput(candidates)
}
func (this *SmartyVerifier) buildRequest(input AddressInput) *http.Request {
query := make(url.Values)
query.Set("street", input.Street1)
query.Set("city", input.City)
query.Set("state", input.State)
query.Set("zipcode", input.ZIPCode)
request, _ := http.NewRequest("GET", "/street-address?"+query.Encode(), nil)
return request
}
func (this *SmartyVerifier) decodeResponse(response *http.Response) (output []Candidate) {
if response != nil {
defer response.Body.Close()
json.NewDecoder(response.Body).Decode(&output)
}
return output
}
func (this *SmartyVerifier) prepareAddressOutput(candidates []Candidate) AddressOutput {
if len(candidates) == 0 {
return AddressOutput{Status: "Invalid API Response"}
}
candidate := candidates[0]
return AddressOutput{
Status: computeStatus(candidate),
DeliveryLine1: candidate.DeliveryLine1,
LastLine: candidate.LastLine,
City: candidate.Components.City,
State: candidate.Components.State,
ZIPCode: candidate.Components.ZIPCode,
}
}
func computeStatus(candidate Candidate) string {
analysis := candidate.Analysis
if !isDeliverable(analysis.Match) {
return "Invalid"
} else if analysis.Vacant == "Y" {
return "Vacant"
} else if analysis.Active != "Y" {
return "Inactive"
} else {
return "Deliverable"
}
}
func isDeliverable(value string) bool {
return value == "Y" || value == "S" || value == "D"
}