opencage-geocoder 0.1.2 → 2.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/opencage/geocoder.rb +32 -13
- data/lib/opencage/geocoder/location.rb +20 -40
- data/lib/opencage/geocoder/request.rb +20 -0
- data/lib/opencage/version.rb +1 -1
- metadata +14 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b8be287da65d2934bc08b6eb4d698c57bd258e99f12fc20785d0581afcd6137f
|
4
|
+
data.tar.gz: 839691909669db6d059b0a5411b5f2a87c53d064ac100b902eee020aefb40798
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce06b91a45a853bb28912dea17b23f4ca860ac11fb904226a910c6af4bbcbb330a4d8539a554beb1c87dc32228286fa7a628674c6dbec513fe8ca9085daa8c0a
|
7
|
+
data.tar.gz: 1526237436579aef61667d50a7616172d30a124398b920d7b0ca72a47f9c8bcf7a3c73950becdbd6a6ecf198256131d1093f1b58f24a1f2b4c2e50583a183834
|
data/lib/opencage/geocoder.rb
CHANGED
@@ -1,31 +1,50 @@
|
|
1
1
|
require 'opencage/geocoder/location'
|
2
|
+
require 'opencage/geocoder/request'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'json'
|
2
5
|
|
3
6
|
module OpenCage
|
4
7
|
class Geocoder
|
5
8
|
GeocodingError = Class.new(StandardError)
|
6
9
|
|
7
|
-
|
10
|
+
def initialize(default_options = {})
|
11
|
+
@api_key = default_options.fetch(:api_key) { raise GeocodingError, 'missing API key' }
|
12
|
+
end
|
13
|
+
|
14
|
+
def geocode(location, options = {})
|
15
|
+
request = Request.new(@api_key, location, options)
|
8
16
|
|
9
|
-
|
17
|
+
results = fetch(request.to_s)
|
18
|
+
return [] unless results
|
10
19
|
|
11
|
-
|
12
|
-
@api_key = options.fetch(:api_key) { raise GeocodingError.new('missing API key') }
|
20
|
+
results.map { |r| Location.new(r) }
|
13
21
|
end
|
14
22
|
|
15
|
-
def
|
16
|
-
|
23
|
+
def reverse_geocode(lat, lng, options = {})
|
24
|
+
if [lat, lng].any? { |coord| !coord.is_a?(Numeric) }
|
25
|
+
raise GeocodingError, "not valid numeric coordinates: #{lat.inspect}, #{lng.inspect}"
|
26
|
+
end
|
27
|
+
|
28
|
+
geocode("#{lat},#{lng}", options).first
|
17
29
|
end
|
18
30
|
|
19
|
-
|
20
|
-
# Accept input as numbers, strings or an array. Raise an error
|
21
|
-
# for anything that cannot be interpreted as a pair of floats.
|
22
|
-
lat, lng = coordinates.flatten.compact.map { |coord| Float(coord) }
|
31
|
+
private
|
23
32
|
|
24
|
-
|
33
|
+
def fetch(url)
|
34
|
+
JSON.parse(URI(url).open.read)['results']
|
35
|
+
rescue OpenURI::HTTPError => e
|
36
|
+
raise GeocodingError, error_message(e)
|
25
37
|
end
|
26
38
|
|
27
|
-
def
|
28
|
-
|
39
|
+
def error_message(error)
|
40
|
+
case String(error)
|
41
|
+
when /^403/
|
42
|
+
'invalid API key'
|
43
|
+
when /^402/
|
44
|
+
'out of quota'
|
45
|
+
else
|
46
|
+
error
|
47
|
+
end
|
29
48
|
end
|
30
49
|
end
|
31
50
|
end
|
@@ -1,64 +1,44 @@
|
|
1
|
-
require 'open-uri'
|
2
|
-
require 'json'
|
3
|
-
|
4
1
|
module OpenCage
|
5
2
|
class Geocoder
|
6
3
|
class Location
|
7
|
-
|
8
|
-
|
9
|
-
def initialize(geo, options={})
|
10
|
-
@geo = geo
|
11
|
-
@lat = options[:lat]
|
12
|
-
@lng = options[:lng]
|
13
|
-
@name = options[:name]
|
4
|
+
def initialize(result, _options = {})
|
5
|
+
@result = result
|
14
6
|
end
|
15
7
|
|
16
|
-
def
|
17
|
-
@
|
8
|
+
def raw
|
9
|
+
@result
|
18
10
|
end
|
19
11
|
|
20
|
-
def
|
21
|
-
@
|
22
|
-
end
|
23
|
-
|
24
|
-
def name
|
25
|
-
@name ||= results.first['formatted']
|
12
|
+
def address
|
13
|
+
@result['formatted']
|
26
14
|
end
|
27
15
|
|
28
16
|
def coordinates
|
29
|
-
[
|
17
|
+
[lat, lng]
|
30
18
|
end
|
31
19
|
|
32
|
-
|
20
|
+
def lat
|
21
|
+
@result['geometry']['lat'].to_f
|
22
|
+
end
|
33
23
|
|
34
|
-
def
|
35
|
-
@
|
36
|
-
raise GeocodingError.new('location not found') if results.empty?
|
37
|
-
end
|
24
|
+
def lng
|
25
|
+
@result['geometry']['lng'].to_f
|
38
26
|
end
|
39
27
|
|
40
|
-
def
|
41
|
-
|
42
|
-
rescue OpenURI::HTTPError => error
|
43
|
-
raise GeocodingError.new(error_message(error))
|
28
|
+
def components
|
29
|
+
@result['components']
|
44
30
|
end
|
45
31
|
|
46
|
-
def
|
47
|
-
|
48
|
-
uri.query = [uri.query, "q=#{query}"].join('&')
|
49
|
-
uri
|
32
|
+
def annotations
|
33
|
+
@result['annotations']
|
50
34
|
end
|
51
35
|
|
52
|
-
def
|
53
|
-
|
54
|
-
"#{lat},#{lng}"
|
55
|
-
elsif @name
|
56
|
-
URI::encode(@name)
|
57
|
-
end
|
36
|
+
def confidence
|
37
|
+
@result['confidence']
|
58
38
|
end
|
59
39
|
|
60
|
-
def
|
61
|
-
|
40
|
+
def bounds
|
41
|
+
@result['bounds']
|
62
42
|
end
|
63
43
|
end
|
64
44
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module OpenCage
|
2
|
+
class Geocoder
|
3
|
+
class Request
|
4
|
+
def initialize(api_key, query, options = {})
|
5
|
+
@host = options.fetch(:host, 'api.opencagedata.com')
|
6
|
+
@params = options.merge(key: api_key, q: query)
|
7
|
+
end
|
8
|
+
|
9
|
+
def url
|
10
|
+
uri = URI::HTTPS.build(host: @host, path: '/geocode/v1/json')
|
11
|
+
uri.query = URI.encode_www_form(@params)
|
12
|
+
uri
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_s
|
16
|
+
url.to_s
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/opencage/version.rb
CHANGED
metadata
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opencage-geocoder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Scully
|
8
|
-
|
8
|
+
- Marc Tobias
|
9
|
+
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2021-02-15 00:00:00.000000000 Z
|
12
13
|
dependencies: []
|
13
|
-
description: A client for the OpenCage Data
|
14
|
-
email:
|
14
|
+
description: A client for the OpenCage Data geocoding API - https://opencagedata.com/
|
15
|
+
email: support@opencagedata.com
|
15
16
|
executables: []
|
16
17
|
extensions: []
|
17
18
|
extra_rdoc_files: []
|
@@ -19,12 +20,14 @@ files:
|
|
19
20
|
- lib/opencage.rb
|
20
21
|
- lib/opencage/geocoder.rb
|
21
22
|
- lib/opencage/geocoder/location.rb
|
23
|
+
- lib/opencage/geocoder/request.rb
|
22
24
|
- lib/opencage/version.rb
|
23
|
-
homepage: https://
|
25
|
+
homepage: https://opencagedata.com/tutorials/geocode-in-ruby
|
24
26
|
licenses:
|
25
27
|
- MIT
|
26
|
-
metadata:
|
27
|
-
|
28
|
+
metadata:
|
29
|
+
source_code_uri: https://github.com/opencagedata/ruby-opencage-geocoder
|
30
|
+
post_install_message:
|
28
31
|
rdoc_options: []
|
29
32
|
require_paths:
|
30
33
|
- lib
|
@@ -32,16 +35,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
32
35
|
requirements:
|
33
36
|
- - ">="
|
34
37
|
- !ruby/object:Gem::Version
|
35
|
-
version: '
|
38
|
+
version: '2.4'
|
36
39
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
40
|
requirements:
|
38
41
|
- - ">="
|
39
42
|
- !ruby/object:Gem::Version
|
40
43
|
version: '0'
|
41
44
|
requirements: []
|
42
|
-
|
43
|
-
|
44
|
-
signing_key:
|
45
|
+
rubygems_version: 3.0.3
|
46
|
+
signing_key:
|
45
47
|
specification_version: 4
|
46
48
|
summary: A client for the OpenCage Data geocoder API
|
47
49
|
test_files: []
|