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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: bb44eb7c577cea9176bf884e83a94160c8068f12
4
- data.tar.gz: 3918ebe495c93729197ccad7d6eee5f8451a74c2
2
+ SHA256:
3
+ metadata.gz: 9387f3afdae67089dd88dd309545efd8b30eee350854997f95ea601ba3c7d1fd
4
+ data.tar.gz: 59b024e2f184416fd1bb2ed4a0d88b14103bf2eea04a9120eb205dec98ef22fd
5
5
  SHA512:
6
- metadata.gz: 7b838c50be09b64555f86e365ed03337e3ec2a1017af020f0fd4dc6877daeb765dbb8e60b1ae42255b356e4bc8ea7f61114c931561974d1fe61525b95fbafee4
7
- data.tar.gz: d9d5e5d5ae02e553880a98829bec8048f5ce7eb77b6a86338b2accdd62d66c9ca5e2d2b653078a6c0df0cc5526d44717b49a5abd9b683c6c385ec0f51ad8a41f
6
+ metadata.gz: 7f918f4608b65658723695f443936430ff9b01c3f52dbeacb8b285844d51923e38945659923708f3dca63ebd180771016e6d539ae1bcf8b6adc32307000bef81
7
+ data.tar.gz: 6dc61251619fb97db4813a4f63d89cfeb254e62e976476f1494f784f019cbb3db3f14c41ca5b7780078e849b838e62c0a98cdf16e4c1bcfc574e069d80b577d2
@@ -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
- 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
+ lat = Float(lat)
25
+ lng = Float(lng)
26
+
27
+ geocode("#{lat},#{lng}", options).first
17
28
  end
18
29
 
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) }
30
+ private
23
31
 
24
- Location.new(self, lat: lat, lng: lng).name
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 url
28
- "#{BASE_URL}?key=#{api_key}"
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
- attr_reader :geo, :name
4
+ def initialize(result, _options = {})
5
+ @result = result
6
+ end
8
7
 
9
- def initialize(geo, options={})
10
- @geo = geo
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
- @lat ||= results.first['geometry']['lat'].to_f
13
+ @result['geometry']['lat'].to_f
18
14
  end
19
15
 
20
16
  def lng
21
- @lng ||= results.first['geometry']['lng'].to_f
17
+ @result['geometry']['lng'].to_f
22
18
  end
23
19
 
24
- def name
25
- @name ||= results.first['formatted']
20
+ def components
21
+ @result['components']
26
22
  end
27
23
 
28
- def coordinates
29
- [ lat, lng ]
24
+ def address
25
+ @result['formatted']
30
26
  end
31
27
 
32
- private
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
@@ -1,3 +1,3 @@
1
1
  module OpenCage
2
- VERSION = '0.1.2'
2
+ VERSION = '2.0.0'.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.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: 2015-10-27 00:00:00.000000000 Z
12
+ date: 2018-12-31 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,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://github.com/lokku/ruby-opencage-geocoder
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.4.5.1
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