opencage-geocoder 0.1.2 → 2.0.0
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 +24 -13
- data/lib/opencage/geocoder/location.rb +13 -45
- data/lib/opencage/geocoder/request.rb +20 -0
- data/lib/opencage/version.rb +1 -1
- metadata +10 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9387f3afdae67089dd88dd309545efd8b30eee350854997f95ea601ba3c7d1fd
|
4
|
+
data.tar.gz: 59b024e2f184416fd1bb2ed4a0d88b14103bf2eea04a9120eb205dec98ef22fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f918f4608b65658723695f443936430ff9b01c3f52dbeacb8b285844d51923e38945659923708f3dca63ebd180771016e6d539ae1bcf8b6adc32307000bef81
|
7
|
+
data.tar.gz: 6dc61251619fb97db4813a4f63d89cfeb254e62e976476f1494f784f019cbb3db3f14c41ca5b7780078e849b838e62c0a98cdf16e4c1bcfc574e069d80b577d2
|
data/lib/opencage/geocoder.rb
CHANGED
@@ -1,31 +1,42 @@
|
|
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
|
+
lat = Float(lat)
|
25
|
+
lng = Float(lng)
|
26
|
+
|
27
|
+
geocode("#{lat},#{lng}", options).first
|
17
28
|
end
|
18
29
|
|
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) }
|
30
|
+
private
|
23
31
|
|
24
|
-
|
32
|
+
def fetch(url)
|
33
|
+
JSON.parse(URI(url).open.read)['results']
|
34
|
+
rescue OpenURI::HTTPError => error
|
35
|
+
raise GeocodingError, error_message(error)
|
25
36
|
end
|
26
37
|
|
27
|
-
def
|
28
|
-
|
38
|
+
def error_message(error)
|
39
|
+
String(error).start_with?('403') ? 'invalid API key' : error
|
29
40
|
end
|
30
41
|
end
|
31
42
|
end
|
@@ -1,64 +1,32 @@
|
|
1
|
-
require 'open-uri'
|
2
|
-
require 'json'
|
3
|
-
|
4
1
|
module OpenCage
|
5
2
|
class Geocoder
|
6
3
|
class Location
|
7
|
-
|
4
|
+
def initialize(result, _options = {})
|
5
|
+
@result = result
|
6
|
+
end
|
8
7
|
|
9
|
-
def
|
10
|
-
@
|
11
|
-
@lat = options[:lat]
|
12
|
-
@lng = options[:lng]
|
13
|
-
@name = options[:name]
|
8
|
+
def raw
|
9
|
+
@result
|
14
10
|
end
|
15
11
|
|
16
12
|
def lat
|
17
|
-
@
|
13
|
+
@result['geometry']['lat'].to_f
|
18
14
|
end
|
19
15
|
|
20
16
|
def lng
|
21
|
-
@
|
17
|
+
@result['geometry']['lng'].to_f
|
22
18
|
end
|
23
19
|
|
24
|
-
def
|
25
|
-
@
|
20
|
+
def components
|
21
|
+
@result['components']
|
26
22
|
end
|
27
23
|
|
28
|
-
def
|
29
|
-
[
|
24
|
+
def address
|
25
|
+
@result['formatted']
|
30
26
|
end
|
31
27
|
|
32
|
-
|
33
|
-
|
34
|
-
def results
|
35
|
-
@results ||= Array(fetch).tap do |results|
|
36
|
-
raise GeocodingError.new('location not found') if results.empty?
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def fetch
|
41
|
-
JSON.parse(open(url).read)['results']
|
42
|
-
rescue OpenURI::HTTPError => error
|
43
|
-
raise GeocodingError.new(error_message(error))
|
44
|
-
end
|
45
|
-
|
46
|
-
def url
|
47
|
-
uri = URI.parse(geo.url)
|
48
|
-
uri.query = [uri.query, "q=#{query}"].join('&')
|
49
|
-
uri
|
50
|
-
end
|
51
|
-
|
52
|
-
def query
|
53
|
-
if @lat && @lng && !@name
|
54
|
-
"#{lat},#{lng}"
|
55
|
-
elsif @name
|
56
|
-
URI::encode(@name)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def error_message(error)
|
61
|
-
(String(error) =~ /\A403/) ? 'invalid API key' : error
|
28
|
+
def coordinates
|
29
|
+
[lat, lng]
|
62
30
|
end
|
63
31
|
end
|
64
32
|
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: 0.
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Scully
|
8
|
+
- Marc Tobias
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2018-12-31 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,11 +20,13 @@ 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:
|
28
|
+
metadata:
|
29
|
+
source_code_uri: https://github.com/opencagedata/ruby-opencage-geocoder
|
27
30
|
post_install_message:
|
28
31
|
rdoc_options: []
|
29
32
|
require_paths:
|
@@ -40,7 +43,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
43
|
version: '0'
|
41
44
|
requirements: []
|
42
45
|
rubyforge_project:
|
43
|
-
rubygems_version: 2.
|
46
|
+
rubygems_version: 2.7.8
|
44
47
|
signing_key:
|
45
48
|
specification_version: 4
|
46
49
|
summary: A client for the OpenCage Data geocoder API
|