opencage-geocoder 3.2.4 → 3.4.0

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
2
  SHA256:
3
- metadata.gz: 659ab4a287039329a5825182e274109e9a29518c9e1f628eed8456dfa1dc2e2a
4
- data.tar.gz: 59b641324b8245f20232c0f28e0ae3aadcb964621501e5380ac64590212c5229
3
+ metadata.gz: f11d2a8240c73511334898f87d7b72387197388e7671ee8b79c8f73a548f1c3d
4
+ data.tar.gz: 77f7af9c4383c989cafe6b543bb58b5c55b068e1c761d514af5fa162e257f32d
5
5
  SHA512:
6
- metadata.gz: d043d2eb8b6a799c67b974346324656253fefda7f23b623a9e181ce0ebaaffbfcfd0d92ede87cf5a19e4f9d6746b64c8f2f36f1ce675f9ad5da2ba89488afbfa
7
- data.tar.gz: 0fedba1f65b1f18761981f5f73c21315f17401fab233de11fe1def6ee97b95ce2cd9cd01bb9a4096eedb47552d76c843d447031440697437c91cce6ceaeb72e0
6
+ metadata.gz: d43fc84a8bb9beba2cc2b093e30916d18f6f130db7d46ac515c96c9dd8a9d4c4976a9eababb984501fde644916f23ef380f0075648b3ace946787e03984abb68
7
+ data.tar.gz: 3a0940493d1a0a2c3407749d01efe14340e1c9f88b09cf20dca1811d279d739a6085ed9c2a955a82a205194dcfe754ec3d32c338b255a21d3391bb01d8b23e50
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OpenCage
2
4
  class Error < StandardError
3
5
  attr_reader :code
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OpenCage
2
4
  class Geocoder
3
5
  class Location
@@ -18,13 +20,13 @@ module OpenCage
18
20
  end
19
21
 
20
22
  def lat
21
- @result['geometry']['lat'].to_f
23
+ @result.dig('geometry', 'lat')&.to_f
22
24
  end
23
25
 
24
26
  alias latitude lat
25
27
 
26
28
  def lng
27
- @result['geometry']['lng'].to_f
29
+ @result.dig('geometry', 'lng')&.to_f
28
30
  end
29
31
 
30
32
  alias longitude lng
@@ -1,11 +1,28 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OpenCage
2
4
  class Geocoder
3
5
  class Request
6
+ LOCALHOST_HOSTS = %w[localhost 127.0.0.1 0.0.0.0 ::1 [::1]].freeze
7
+
4
8
  def initialize(api_key, query, options = {})
5
9
  @host = options.fetch(:host, 'api.opencagedata.com')
10
+ validate_host!(@host)
6
11
  @params = options.merge(key: api_key, q: query)
7
12
  end
8
13
 
14
+ private
15
+
16
+ def validate_host!(host)
17
+ hostname = host.to_s.downcase.gsub(/(?<=\w):\d+\z/, '')
18
+ return if hostname.end_with?('.opencagedata.com')
19
+ return if LOCALHOST_HOSTS.include?(hostname)
20
+
21
+ raise ArgumentError, "Invalid host: #{host}. Must be a subdomain of opencagedata.com or localhost."
22
+ end
23
+
24
+ public
25
+
9
26
  def url
10
27
  uri = URI::HTTPS.build(host: @host, path: '/geocode/v1/json')
11
28
  uri.query = URI.encode_www_form(@params)
@@ -1,8 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'opencage/geocoder/location'
2
4
  require 'opencage/geocoder/request'
3
5
  require 'opencage/error'
4
6
  require 'opencage/version'
5
7
  require 'open-uri'
8
+ require 'net/http'
6
9
  require 'json'
7
10
 
8
11
  module OpenCage
@@ -13,17 +16,19 @@ module OpenCage
13
16
  end
14
17
 
15
18
  def geocode(location, options = {})
19
+ raise_error("400 Not a valid location: `#{location.inspect}`") unless location.is_a?(String)
20
+
16
21
  request = Request.new(@api_key, location, options)
17
22
 
18
23
  begin
19
24
  results = fetch(request.to_s)
20
25
  rescue Errno::ECONNREFUSED
21
- raise_error("408 Failed to open TCP connection to API #{request}")
26
+ raise_error("408 Failed to open TCP connection to API #{redact_url(request)}")
22
27
  rescue Errno::ECONNRESET
23
28
  # Connection reset by peer - SSL_connect
24
- raise_error("408 Failed to open SSL connection to API #{request}")
29
+ raise_error("408 Failed to open SSL connection to API #{redact_url(request)}")
25
30
  rescue Net::OpenTimeout
26
- raise_error("408 Timeout connecting to API #{request}")
31
+ raise_error("408 Timeout connecting to API #{redact_url(request)}")
27
32
  end
28
33
 
29
34
  return [] unless results
@@ -32,8 +37,12 @@ module OpenCage
32
37
  end
33
38
 
34
39
  def reverse_geocode(lat, lng, options = {})
35
- lat = to_float(lat)
36
- lng = to_float(lng)
40
+ begin
41
+ lat = to_float(lat)
42
+ lng = to_float(lng)
43
+ rescue TypeError
44
+ raise_error("400 Not valid numeric coordinates: #{lat.inspect}, #{lng.inspect}")
45
+ end
37
46
 
38
47
  if [lat, lng].any? { |coord| !coord.is_a?(Numeric) }
39
48
  raise_error("400 Not valid numeric coordinates: #{lat.inspect}, #{lng.inspect}")
@@ -47,7 +56,7 @@ module OpenCage
47
56
  def fetch(url)
48
57
  JSON.parse(URI(url).open(headers).read)['results']
49
58
  rescue OpenURI::HTTPError => e
50
- raise_error(e)
59
+ raise_error(e.io.status.join(' '))
51
60
  end
52
61
 
53
62
  def headers
@@ -60,6 +69,10 @@ module OpenCage
60
69
  raise klass.new(message: String(error), code: code.to_i)
61
70
  end
62
71
 
72
+ def redact_url(request)
73
+ request.to_s.gsub(/key=([^&]{6})[^&]*/, 'key=\1...[REDACTED]')
74
+ end
75
+
63
76
  def to_float(coord)
64
77
  Float(coord)
65
78
  rescue ArgumentError
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OpenCage
2
- VERSION = '3.2.4'.freeze
4
+ VERSION = '3.4.0'
3
5
  end
data/lib/opencage.rb CHANGED
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'opencage/version'
2
4
  require 'opencage/geocoder'
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opencage-geocoder
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.4
4
+ version: 3.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Scully
8
8
  - Marc Tobias
9
- autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2025-04-02 00:00:00.000000000 Z
11
+ date: 2026-03-29 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: A client for the OpenCage geocoding API - https://opencagedata.com/
15
14
  email: support@opencagedata.com
@@ -28,7 +27,10 @@ licenses:
28
27
  - MIT
29
28
  metadata:
30
29
  source_code_uri: https://github.com/opencagedata/ruby-opencage-geocoder
31
- post_install_message:
30
+ changelog_uri: https://github.com/opencagedata/ruby-opencage-geocoder/blob/master/CHANGES.txt
31
+ bug_tracker_uri: https://github.com/opencagedata/ruby-opencage-geocoder/issues
32
+ homepage_uri: https://opencagedata.com/tutorials/geocode-in-ruby
33
+ rubygems_mfa_required: 'true'
32
34
  rdoc_options: []
33
35
  require_paths:
34
36
  - lib
@@ -36,15 +38,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
36
38
  requirements:
37
39
  - - ">="
38
40
  - !ruby/object:Gem::Version
39
- version: '3.0'
41
+ version: '3.2'
40
42
  required_rubygems_version: !ruby/object:Gem::Requirement
41
43
  requirements:
42
44
  - - ">="
43
45
  - !ruby/object:Gem::Version
44
46
  version: '0'
45
47
  requirements: []
46
- rubygems_version: 3.3.27
47
- signing_key:
48
+ rubygems_version: 3.6.2
48
49
  specification_version: 4
49
50
  summary: A client for the OpenCage geocoder API
50
51
  test_files: []