Passed
Push — master ( d7840c...df15e8 )
by Viktor
01:26
created

yageocoding.*YaGeoResponse.GetCoordinates   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
package yageocoding
2
3
import (
4
	"fmt"
5
	"net/http"
6
	"encoding/json"
7
	"errors"
8
	"strings"
9
)
10
11
type YaGeoResponse struct {
0 ignored issues
show
introduced by
exported type YaGeoResponse should have comment or be unexported
Loading history...
12
	Response struct {
13
		ObjectCollection YaGeoObjectCollection `json:"GeoObjectCollection"`
14
	} `json:"response"`
15
}
16
17
type YaGeoObjectCollection struct {
0 ignored issues
show
introduced by
exported type YaGeoObjectCollection should have comment or be unexported
Loading history...
18
	MetaData YaGeoMetaData `json:"metaDataProperty"`
19
	Member   []YaGeoMember `json:"featureMember"`
20
}
21
22
type YaGeoMetaData struct {
0 ignored issues
show
introduced by
exported type YaGeoMetaData should have comment or be unexported
Loading history...
23
	ResponseMetaData struct {
24
		Request string `json:"request"`
25
		Found   string `json:"found"`
26
		Results string `json:"results"`
27
	} `json:"GeocoderResponseMetaData"`
28
}
29
30
type YaGeoMember struct {
0 ignored issues
show
introduced by
exported type YaGeoMember should have comment or be unexported
Loading history...
31
	GeoObject struct {
32
		MetaData    YaGeoMemberMetaData `json:"metaDataProperty"`
33
		Description string              `json:"description"`
34
		Name        string              `json:"name"`
35
		Point       struct {
36
			Pos string `json:"pos"`
37
		} `json:"Point"`
38
	} `json:"GeoObject"`
39
}
40
41
type YaGeoMemberMetaData struct {
0 ignored issues
show
introduced by
exported type YaGeoMemberMetaData should have comment or be unexported
Loading history...
42
	Meta struct {
43
		Kind      string `json:"kind"`
44
		Text      string `json:"text"`
45
		Precision string `json:"precision"`
46
	} `json:"GeocoderMetaData"`
47
}
48
49
type YaGeoAddress struct {
0 ignored issues
show
introduced by
exported type YaGeoAddress should have comment or be unexported
Loading history...
50
	CountryCode string                  `json:"country_code"`
51
	PostalCode  string                  `json:"postal_code"`
52
	Formatted   string                  `json:"formatted"`
53
	Components  []YaGeoAddressComponent `json:"Components"`
54
}
55
56
type YaGeoAddressComponent struct {
0 ignored issues
show
introduced by
exported type YaGeoAddressComponent should have comment or be unexported
Loading history...
57
	Kind string `json:"kind"`
58
	Name string `json:"name"`
59
}
60
61
func (loc *YaGeoResponse) GetCoordinates() (string, string) {
0 ignored issues
show
introduced by
exported method YaGeoResponse.GetCoordinates should have comment or be unexported
Loading history...
62
	if len(loc.Response.ObjectCollection.Member) == 0 {
63
		return "0", "0"
64
	}
65
66
	str := strings.Split(loc.Response.ObjectCollection.Member[0].GeoObject.Point.Pos, " ")
67
	return str[0], str[1]
68
}
69
70
func GetData(key, location string) (result YaGeoResponse, err error) {
0 ignored issues
show
introduced by
exported function GetData should have comment or be unexported
Loading history...
71
	resp, err := http.Get(fmt.Sprintf("https://geocode-maps.yandex.ru/1.x/?format=json&geocode=%v&apikey=%v", location, key))
72
	if err != nil {
73
		return
74
	}
75
76
	err = json.NewDecoder(resp.Body).Decode(&result)
77
	if err != nil {
78
		return result, err
79
	}
80
81
	if len(result.Response.ObjectCollection.Member) == 0 {
82
		return result, errors.New("location not fount")
83
	}
84
	return
85
}
86