geon 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/geon.rb +4 -1
- data/lib/geon/geo_object.rb +28 -0
- data/lib/geon/yandex.rb +40 -0
- data/lib/geon/yandex_api.rb +36 -0
- data/spec/samples/yandex/empty.json +22 -0
- data/spec/samples/yandex/yandex_geocode1.json +1 -0
- data/spec/yandex_real_spec.rb +15 -0
- data/spec/yandex_spec.rb +19 -0
- metadata +11 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d54d0a320ac8a495594bb14fff9be7e51f0a769
|
4
|
+
data.tar.gz: 6c9dffd024c17ca6bdcdebef8652feb28f7009b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 063d97b648c6dcb978150be90528be3e84d4434dd36e1c8fd861599f1a578d1e8f8e7305171472782bee2d3e383fa0cabc6e64eea264dd5f02ed9a045dfd4dc3
|
7
|
+
data.tar.gz: 39e077b6f1f27e561dafef3f72bc0cfe1a2d75b1029674960195e2effee55ef4d5652691b16e47d6859303b47cd5ef801dd1493b5b040d91c6057902330b7900
|
data/lib/geon.rb
CHANGED
@@ -1,13 +1,16 @@
|
|
1
1
|
module Geon
|
2
|
-
VERSION = '0.1.
|
2
|
+
VERSION = '0.1.2'
|
3
3
|
end
|
4
4
|
|
5
5
|
require 'net/http'
|
6
6
|
|
7
|
+
require 'geon/geo_object'
|
7
8
|
require 'geon/http_loader'
|
8
9
|
require 'geon/invalid_key_error'
|
9
10
|
require 'geon/wikimapia_error'
|
10
11
|
require 'geon/wikimapia_api'
|
11
12
|
require 'geon/wikimapia'
|
13
|
+
require 'geon/yandex_api'
|
14
|
+
require 'geon/yandex'
|
12
15
|
|
13
16
|
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module Geon
|
4
|
+
class GeoObject < ::BasicObject
|
5
|
+
|
6
|
+
attr_accessor :coord, :type, :address, :detail
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@coord = [0, 0]
|
10
|
+
@type = :house
|
11
|
+
@address = ''
|
12
|
+
@detail = {}
|
13
|
+
end
|
14
|
+
|
15
|
+
def longitude
|
16
|
+
@coord[1]
|
17
|
+
end
|
18
|
+
|
19
|
+
def latitude
|
20
|
+
@coord[0]
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_hash
|
24
|
+
{coord: @coord, type: @type, address: @address, detail: @detail}
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
data/lib/geon/yandex.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
module Geon
|
2
|
+
class Yandex
|
3
|
+
HOST = 'geocode-maps.yandex.ru'
|
4
|
+
|
5
|
+
def initialize(args = {})
|
6
|
+
loader = args[:loader] ? args[:loader] : HttpLoader.new(HOST)
|
7
|
+
@api = YandexApi.new(loader)
|
8
|
+
end
|
9
|
+
|
10
|
+
def forward(lat, long)
|
11
|
+
raw = @api.geocode({lat: lat, long: long})
|
12
|
+
geo_object = raw_geo_object raw
|
13
|
+
result = GeoObject.new
|
14
|
+
|
15
|
+
result.address = geo_object['metaDataProperty']['GeocoderMetaData']['text']
|
16
|
+
raw_coord = geo_object['Point']['pos'].split
|
17
|
+
result.coord = [raw_coord[1].to_f, raw_coord[0].to_f]
|
18
|
+
|
19
|
+
result
|
20
|
+
end
|
21
|
+
|
22
|
+
def reverse(query)
|
23
|
+
raw = @api.geocode({query: query})
|
24
|
+
geo_object = raw_geo_object raw
|
25
|
+
result = GeoObject.new
|
26
|
+
|
27
|
+
result.address = geo_object['metaDataProperty']['GeocoderMetaData']['text']
|
28
|
+
raw_coord = geo_object['Point']['pos'].split
|
29
|
+
result.coord = [raw_coord[1].to_f, raw_coord[0].to_f]
|
30
|
+
|
31
|
+
raw
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def raw_geo_object(response)
|
37
|
+
response['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Geon
|
2
|
+
class YandexApi
|
3
|
+
PATH = '/1.x/'
|
4
|
+
|
5
|
+
# @param [Geon::HttpLoader] downloader
|
6
|
+
# @param [Hash] param
|
7
|
+
def initialize(downloader, param = {})
|
8
|
+
@downloader = downloader
|
9
|
+
|
10
|
+
@general_param = {
|
11
|
+
results: 1,
|
12
|
+
format: 'json',
|
13
|
+
kind: 'house',
|
14
|
+
}.merge(param)
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
def geocode(args = {})
|
19
|
+
param = @general_param
|
20
|
+
if args[:lat] and args[:long]
|
21
|
+
param[:geocode] = "#{args[:long]},#{args[:lat]}"
|
22
|
+
end
|
23
|
+
|
24
|
+
if args[:query]
|
25
|
+
param[:geocode] = args[:query]
|
26
|
+
end
|
27
|
+
|
28
|
+
response = @downloader.get PATH , param
|
29
|
+
result = JSON.parse(CGI::unescape response)
|
30
|
+
|
31
|
+
result
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
{"response": {
|
2
|
+
"GeoObjectCollection": {
|
3
|
+
"metaDataProperty": {
|
4
|
+
"GeocoderResponseMetaData": {
|
5
|
+
"request": "55.757962,37.611006",
|
6
|
+
"found": "0",
|
7
|
+
"results": "1",
|
8
|
+
"boundedBy": {
|
9
|
+
"Envelope": {
|
10
|
+
"lowerCorner": "55.755465 37.608506",
|
11
|
+
"upperCorner": "55.760460 37.613508"
|
12
|
+
}
|
13
|
+
},
|
14
|
+
"Point": {
|
15
|
+
"pos": "55.757962 37.611006"
|
16
|
+
},
|
17
|
+
"kind": "house"
|
18
|
+
}
|
19
|
+
},
|
20
|
+
"featureMember": []
|
21
|
+
}
|
22
|
+
}}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"response":{"GeoObjectCollection":{"metaDataProperty":{"GeocoderResponseMetaData":{"request":"Москва, Тверская улица, дом 7","found":"5","results":"1"}},"featureMember":[{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"kind":"house","text":"Россия, Москва, Тверская улица, 7","precision":"exact","AddressDetails":{"Country":{"AddressLine":"Москва, Тверская улица, 7","CountryNameCode":"RU","CountryName":"Россия","Locality":{"LocalityName":"Москва","Thoroughfare":{"ThoroughfareName":"Тверская улица","Premise":{"PremiseNumber":"7"}}}}}}},"description":"Москва, Россия","name":"Тверская улица, 7","boundedBy":{"Envelope":{"lowerCorner":"37.608958 55.756807","upperCorner":"37.613054 55.759117"}},"Point":{"pos":"37.611006 55.757962"}}}]}}}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
describe Geon::Yandex, 'Yandex real geocode' do
|
2
|
+
|
3
|
+
it 'Разбор json ответа с одним результатом' do
|
4
|
+
|
5
|
+
yandex = Geon::Yandex.new
|
6
|
+
|
7
|
+
result = yandex.forward(55.757962, 37.611006)
|
8
|
+
|
9
|
+
expected = Geon::GeoObject.new
|
10
|
+
expected.address = 'Россия, Москва, Тверская улица, 7'
|
11
|
+
expected.coord = [55.757962, 37.611006] #long, lat
|
12
|
+
|
13
|
+
expected.to_hash.should eq result.to_hash
|
14
|
+
end
|
15
|
+
end
|
data/spec/yandex_spec.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
|
2
|
+
describe Geon::Yandex, 'Yandex geocode' do
|
3
|
+
|
4
|
+
it 'simple request' do
|
5
|
+
|
6
|
+
loader = double("Geon::HttpLoader")
|
7
|
+
loader.stub(:get) { sample('yandex/yandex_geocode1.json') }
|
8
|
+
|
9
|
+
yandex = Geon::Yandex.new({loader: loader})
|
10
|
+
|
11
|
+
result = yandex.forward(55.757962, 37.611006)
|
12
|
+
|
13
|
+
expected = Geon::GeoObject.new
|
14
|
+
expected.address = 'Россия, Москва, Тверская улица, 7'
|
15
|
+
expected.coord = [55.757962, 37.611006] #long, lat
|
16
|
+
|
17
|
+
expected.to_hash.should eq result.to_hash
|
18
|
+
end
|
19
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: geon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Suslov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Geo data provider
|
14
14
|
email:
|
@@ -21,11 +21,14 @@ files:
|
|
21
21
|
- Gemfile
|
22
22
|
- geon.gemspec
|
23
23
|
- lib/geon.rb
|
24
|
+
- lib/geon/geo_object.rb
|
24
25
|
- lib/geon/http_loader.rb
|
25
26
|
- lib/geon/invalid_key_error.rb
|
26
27
|
- lib/geon/wikimapia.rb
|
27
28
|
- lib/geon/wikimapia_api.rb
|
28
29
|
- lib/geon/wikimapia_error.rb
|
30
|
+
- lib/geon/yandex.rb
|
31
|
+
- lib/geon/yandex_api.rb
|
29
32
|
- spec/helper.rb
|
30
33
|
- spec/samples/wikimapia/error__wrong_key.json
|
31
34
|
- spec/samples/wikimapia/place.getbyarea__good.json
|
@@ -33,9 +36,13 @@ files:
|
|
33
36
|
- spec/samples/wikimapia/place.getbyid__good.xml
|
34
37
|
- spec/samples/wikimapia/place.getnearest__good.json
|
35
38
|
- spec/samples/wikimapia/place.getnearest__good.xml
|
39
|
+
- spec/samples/yandex/empty.json
|
40
|
+
- spec/samples/yandex/yandex_geocode1.json
|
36
41
|
- spec/wikimapia_api_spec.rb
|
37
42
|
- spec/wikimapia_real_spec.rb
|
38
43
|
- spec/wikimapia_spec.rb
|
44
|
+
- spec/yandex_real_spec.rb
|
45
|
+
- spec/yandex_spec.rb
|
39
46
|
homepage: https://github.com/AlmazKo/geon
|
40
47
|
licenses:
|
41
48
|
- MIT
|
@@ -64,3 +71,5 @@ test_files:
|
|
64
71
|
- spec/wikimapia_api_spec.rb
|
65
72
|
- spec/wikimapia_real_spec.rb
|
66
73
|
- spec/wikimapia_spec.rb
|
74
|
+
- spec/yandex_real_spec.rb
|
75
|
+
- spec/yandex_spec.rb
|