forked from pieterclaerhout/go-geoip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
119 lines (104 loc) · 3.76 KB
/
model.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
108
109
110
111
112
113
114
115
116
117
118
119
package geoip
// Continent defines the continent information for an IP location
type Continent struct {
ISOCode string `maxminddb:"code"` // The continent code
Names map[string]string `maxminddb:"names"` // The names for this continent
}
// Country defines the country information for an IP location
type Country struct {
ISOCode string `maxminddb:"iso_code"` // The ISO country code
Names map[string]string `maxminddb:"names"` // The names for this country
}
// GPSCoordinate defines a GPS coordinate
type GPSCoordinate struct {
Latitude float64 `maxminddb:"latitude"` // The latitude for this location
Longitude float64 `maxminddb:"longitude"` // The longitude for this location
}
// Location defines all the location information we know such as timezone, lat and lon
type Location struct {
GPSCoordinate
TimeZone string `maxminddb:"time_zone"` // The timezone for this location
}
// Subdivision defines a subdivision of the country (state, province)
type Subdivision struct {
ISOCode string `maxminddb:"iso_code"` // The ISO subdivision code
Names map[string]string `maxminddb:"names"` // The names for this subdivision
}
// IPLocation defines all info we know about a location based on it's IP address
type IPLocation struct {
IPAddress string
Continent *Continent `maxminddb:"continent"`
Country *Country `maxminddb:"country"`
Location *Location `maxminddb:"location"`
Subdivisions []*Subdivision `maxminddb:"subdivisions"`
IsCached bool
}
// CountryCode returns the ISO country code for the location
func (ipLocation *IPLocation) CountryCode() string {
if ipLocation == nil || ipLocation.Country == nil {
return defaultCountryCode
}
return ipLocation.Country.ISOCode
}
// CountryName returns the country name for the location in English
func (ipLocation *IPLocation) CountryName() string {
if ipLocation == nil || ipLocation.Country == nil {
return defaultCountryName
}
return ipLocation.Country.Names["en"]
}
// ContinentCode returns the ISO country code for the location
func (ipLocation *IPLocation) ContinentCode() string {
if ipLocation == nil || ipLocation.Continent == nil {
return ""
}
return ipLocation.Continent.ISOCode
}
// ContinentName returns the country name for the location in English
func (ipLocation *IPLocation) ContinentName() string {
if ipLocation == nil || ipLocation.Continent == nil {
return ""
}
return ipLocation.Continent.Names["en"]
}
// RegionName returns the region name for the location in English
func (ipLocation *IPLocation) RegionName() string {
countryCode := ipLocation.CountryCode()
return CountryCodeToRegion(countryCode)
}
// TimeZone returns the timezone for the location
func (ipLocation *IPLocation) TimeZone() string {
if ipLocation == nil || ipLocation.Location == nil {
return defaultTimeZone
}
return ipLocation.Location.TimeZone
}
// SubdivisionCodes returns the codes for the subdivisions
func (ipLocation *IPLocation) SubdivisionCodes() []string {
if ipLocation == nil || ipLocation.Subdivisions == nil {
return []string{}
}
result := []string{}
for _, item := range ipLocation.Subdivisions {
result = append(result, item.ISOCode)
}
return result
}
// SubdivisionNames returns the names for the subdivisions
func (ipLocation *IPLocation) SubdivisionNames() []string {
if ipLocation == nil || ipLocation.Subdivisions == nil {
return []string{}
}
result := []string{}
for _, item := range ipLocation.Subdivisions {
result = append(result, item.Names["en"])
}
return result
}
// ApproximateGPSCoordinate returns the GPS coordinates for the location
func (ipLocation *IPLocation) ApproximateGPSCoordinate() *GPSCoordinate {
if ipLocation == nil || ipLocation.Location == nil {
return nil
}
return &ipLocation.Location.GPSCoordinate
}