opencage-geocoder 0.1.2 → 2.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: bb44eb7c577cea9176bf884e83a94160c8068f12
4
- data.tar.gz: 3918ebe495c93729197ccad7d6eee5f8451a74c2
2
+ SHA256:
3
+ metadata.gz: b8be287da65d2934bc08b6eb4d698c57bd258e99f12fc20785d0581afcd6137f
4
+ data.tar.gz: 839691909669db6d059b0a5411b5f2a87c53d064ac100b902eee020aefb40798
5
5
  SHA512:
6
- metadata.gz: 7b838c50be09b64555f86e365ed03337e3ec2a1017af020f0fd4dc6877daeb765dbb8e60b1ae42255b356e4bc8ea7f61114c931561974d1fe61525b95fbafee4
7
- data.tar.gz: d9d5e5d5ae02e553880a98829bec8048f5ce7eb77b6a86338b2accdd62d66c9ca5e2d2b653078a6c0df0cc5526d44717b49a5abd9b683c6c385ec0f51ad8a41f
6
+ metadata.gz: ce06b91a45a853bb28912dea17b23f4ca860ac11fb904226a910c6af4bbcbb330a4d8539a554beb1c87dc32228286fa7a628674c6dbec513fe8ca9085daa8c0a
7
+ data.tar.gz: 1526237436579aef61667d50a7616172d30a124398b920d7b0ca72a47f9c8bcf7a3c73950becdbd6a6ecf198256131d1093f1b58f24a1f2b4c2e50583a183834
@@ -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
- BASE_URL = "http://api.opencagedata.com/geocode/v1/json"
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
- attr_reader :api_key
17
+ results = fetch(request.to_s)
18
+ return [] unless results
10
19
 
11
- def initialize(options={})
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 geocode(location)
16
- Location.new(self, name: location).coordinates
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
- def reverse_geocode(*coordinates)
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
- Location.new(self, lat: lat, lng: lng).name
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 url
28
- "#{BASE_URL}?key=#{api_key}"
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
- attr_reader :geo, :name
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 lat
17
- @lat ||= results.first['geometry']['lat'].to_f
8
+ def raw
9
+ @result
18
10
  end
19
11
 
20
- def lng
21
- @lng ||= results.first['geometry']['lng'].to_f
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
- [ lat, lng ]
17
+ [lat, lng]
30
18
  end
31
19
 
32
- private
20
+ def lat
21
+ @result['geometry']['lat'].to_f
22
+ end
33
23
 
34
- def results
35
- @results ||= Array(fetch).tap do |results|
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 fetch
41
- JSON.parse(open(url).read)['results']
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 url
47
- uri = URI.parse(geo.url)
48
- uri.query = [uri.query, "q=#{query}"].join('&')
49
- uri
32
+ def annotations
33
+ @result['annotations']
50
34
  end
51
35
 
52
- def query
53
- if @lat && @lng && !@name
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 error_message(error)
61
- (String(error) =~ /\A403/) ? 'invalid API key' : error
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
@@ -1,3 +1,3 @@
1
1
  module OpenCage
2
- VERSION = '0.1.2'
2
+ VERSION = '2.1.4'.freeze
3
3
  end
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.1.2
4
+ version: 2.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Scully
8
- autorequire:
8
+ - Marc Tobias
9
+ autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2015-10-27 00:00:00.000000000 Z
12
+ date: 2021-02-15 00:00:00.000000000 Z
12
13
  dependencies: []
13
- description: A client for the OpenCage Data geocoder API - http://geocoder.opencagedata.com/
14
- email: dev@lokku.com
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://github.com/lokku/ruby-opencage-geocoder
25
+ homepage: https://opencagedata.com/tutorials/geocode-in-ruby
24
26
  licenses:
25
27
  - MIT
26
- metadata: {}
27
- post_install_message:
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: '0'
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
- rubyforge_project:
43
- rubygems_version: 2.4.5.1
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: []