geocoder 1.8.0 → 1.8.1

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
2
  SHA256:
3
- metadata.gz: 3a0d33121c080f317bcfcad5c3547491676b1edcef61e33cb3a090e91cccdeee
4
- data.tar.gz: 066ff69c5443b18d9f5b4e1af94481b588bf18560050e0da49129b3630f5047b
3
+ metadata.gz: 35c6a6d57b8ff3818e9d1b616558a8bbdc3b4b51db2b2c7dadb720eccafd27b4
4
+ data.tar.gz: 8e8fe1ba7cb9da58131a965d3d85589084f4d5dd70409990f49b89b83923f4e3
5
5
  SHA512:
6
- metadata.gz: 3e47b46a3c299023708b9a2444315d0e9b2788cf682e7e72e1d2ae8d0be40c9d6b7cf25793f20c946f6941662c6512d86bea29bd48e81485546facf00f9cdea3
7
- data.tar.gz: 0bd121ae2aa3b8e2232e343d70557a7c9201f06dc47a8ae59d5b28e8610c8627ed6beefb251020537979de472e35d0174ab5fad20994e1414914a28f3d91ba1c
6
+ metadata.gz: 0bbf04cc8488a533038783e2d250706fb2d2439de8ea1e5bf4b89950d1dd51d5e9393454540a23cb4e7fbc33b58f54fec1c71c14c98d6fbe8bb8e5caad786989
7
+ data.tar.gz: 92b8f5933a421b47cdededea9c8e8df8d2ec6f45cb25c8726b343a773b315929529fe821cae08868576ae2e8c5cdbc5fd1782bd2bfc8c56f36afdc1514f52b58
data/CHANGELOG.md CHANGED
@@ -3,6 +3,12 @@ Changelog
3
3
 
4
4
  Major changes to Geocoder for each release. Please see the Git log for complete list of changes.
5
5
 
6
+ 1.8.1 (2022 Sep 23)
7
+ -------------------
8
+ * Add support for IPBase lookup (thanks github.com/jonallured).
9
+ * Test cleanup (thanks github.com/jonallured).
10
+ * Prevent errors when existing constant name shadows a lookup class (thanks github.com/avram-twitch).
11
+
6
12
  1.8.0 (2022 May 17)
7
13
  -------------------
8
14
  * Add support for 2GIS lookup (thanks github.com/ggrikgg).
data/README.md CHANGED
@@ -747,7 +747,7 @@ If your application requires quick geocoding responses you will probably need to
747
747
 
748
748
  For IP address lookups in Rails applications, it is generally NOT a good idea to run `request.location` during a synchronous page load without understanding the speed/behavior of your configured lookup. If the lookup becomes slow, so will your website.
749
749
 
750
- For the most part, the speed of geocoding requests has little to do with the Geocoder gem. Please take the time to learn about your configured lookup (links to documentation are provided above) before posting performance-related issues.
750
+ For the most part, the speed of geocoding requests has little to do with the Geocoder gem. Please take the time to learn about your configured lookup before posting performance-related issues.
751
751
 
752
752
  ### Unexpected Responses from Geocoding Services
753
753
 
@@ -14,7 +14,7 @@ module Geocoder
14
14
  def [](url)
15
15
  interpret store_service.read(url)
16
16
  rescue => e
17
- warn "Geocoder cache read error: #{e}"
17
+ Geocoder.log(:warn, "Geocoder cache read error: #{e}")
18
18
  end
19
19
 
20
20
  ##
@@ -23,7 +23,7 @@ module Geocoder
23
23
  def []=(url, value)
24
24
  store_service.write(url, value)
25
25
  rescue => e
26
- warn "Geocoder cache write error: #{e}"
26
+ Geocoder.log(:warn, "Geocoder cache write error: #{e}")
27
27
  end
28
28
 
29
29
  ##
@@ -77,6 +77,10 @@ module Geocoder
77
77
  instance.set_defaults
78
78
  end
79
79
 
80
+ def self.initialize
81
+ instance.send(:initialize)
82
+ end
83
+
80
84
  OPTIONS.each do |o|
81
85
  define_method o do
82
86
  @data[o]
@@ -92,7 +92,8 @@ module Geocoder
92
92
  :ipstack,
93
93
  :ip2location,
94
94
  :ipgeolocation,
95
- :ipqualityscore
95
+ :ipqualityscore,
96
+ :ipbase
96
97
  ]
97
98
  end
98
99
 
@@ -137,9 +138,9 @@ module Geocoder
137
138
  # Safely instantiate Lookup
138
139
  #
139
140
  def instantiate_lookup(name)
140
- class_name = "Geocoder::Lookup::#{classify_name(name)}"
141
+ class_name = classify_name(name)
141
142
  begin
142
- Geocoder::Lookup.const_get(class_name)
143
+ Geocoder::Lookup.const_get(class_name, inherit=false)
143
144
  rescue NameError
144
145
  require "geocoder/lookups/#{name}"
145
146
  end
@@ -32,10 +32,10 @@ module Geocoder::Lookup
32
32
  return doc['geocodes'] unless doc['geocodes'].blank?
33
33
  when ['0', 'INVALID_USER_KEY']
34
34
  raise_error(Geocoder::InvalidApiKey, "invalid api key") ||
35
- warn("#{self.name} Geocoding API error: invalid api key.")
35
+ Geocoder.log(:warn, "#{self.name} Geocoding API error: invalid api key.")
36
36
  else
37
37
  raise_error(Geocoder::Error, "server error.") ||
38
- warn("#{self.name} Geocoding API error: server error - [#{doc['info']}]")
38
+ Geocoder.log(:warn, "#{self.name} Geocoding API error: server error - [#{doc['info']}]")
39
39
  end
40
40
  return []
41
41
  end
@@ -22,7 +22,13 @@ module Geocoder
22
22
  private
23
23
 
24
24
  def base_query_url(query)
25
- method = query.reverse_geocode? ? 'reverse' : 'search'
25
+ method = if query.reverse_geocode?
26
+ 'reverse'
27
+ elsif query.options[:autocomplete]
28
+ 'autocomplete'
29
+ else
30
+ 'search'
31
+ end
26
32
  "https://api.geoapify.com/v1/geocode/#{method}?"
27
33
  end
28
34
 
@@ -56,7 +56,7 @@ module Geocoder
56
56
  else
57
57
  result = []
58
58
  raise_error(Geocoder::Error) ||
59
- warn("Geportail.lu Geocoding API error")
59
+ Geocoder.log(:warn, "Geportail.lu Geocoding API error")
60
60
  end
61
61
  result
62
62
  end
@@ -0,0 +1,49 @@
1
+ require 'geocoder/lookups/base'
2
+ require 'geocoder/results/ipbase'
3
+
4
+ module Geocoder::Lookup
5
+ class Ipbase < Base
6
+
7
+ def name
8
+ "ipbase.com"
9
+ end
10
+
11
+ def supported_protocols
12
+ [:https]
13
+ end
14
+
15
+ private # ---------------------------------------------------------------
16
+
17
+ def base_query_url(query)
18
+ "https://api.ipbase.com/v2/info?"
19
+ end
20
+
21
+ def query_url_params(query)
22
+ {
23
+ :ip => query.sanitized_text,
24
+ :apikey => configuration.api_key
25
+ }
26
+ end
27
+
28
+ def results(query)
29
+ # don't look up a loopback or private address, just return the stored result
30
+ return [reserved_result(query.text)] if query.internal_ip_address?
31
+ doc = fetch_data(query) || {}
32
+ doc.fetch("data", {})["location"] ? [doc] : []
33
+ end
34
+
35
+ def reserved_result(ip)
36
+ {
37
+ "data" => {
38
+ "ip" => ip,
39
+ "location" => {
40
+ "city" => { "name" => "" },
41
+ "country" => { "alpha2" => "RD", "name" => "Reserved" },
42
+ "region" => { "alpha2" => "", "name" => "" },
43
+ "zip" => ""
44
+ }
45
+ }
46
+ }
47
+ end
48
+ end
49
+ end
@@ -18,6 +18,7 @@ module Geocoder
18
18
  end
19
19
 
20
20
  def self.read_stub(query_text)
21
+ @default_stub ||= nil
21
22
  stubs.fetch(query_text) {
22
23
  return @default_stub unless @default_stub.nil?
23
24
  raise ArgumentError, "unknown stub request #{query_text}"
@@ -24,11 +24,11 @@ module Geocoder::Lookup
24
24
 
25
25
  def results(query)
26
26
  return [] unless doc = fetch_data(query)
27
- if err = doc['error']
28
- if err["status"] == 401 and err["message"] == "invalid key"
27
+ if [400, 403].include? doc['statusCode']
28
+ if doc['statusCode'] == 403 and doc['message'] == 'Invalid key'
29
29
  raise_error(Geocoder::InvalidApiKey) || Geocoder.log(:warn, "Invalid API key.")
30
30
  else
31
- Geocoder.log(:warn, "Yandex Geocoding API error: #{err['status']} (#{err['message']}).")
31
+ Geocoder.log(:warn, "Yandex Geocoding API error: #{doc['statusCode']} (#{doc['message']}).")
32
32
  end
33
33
  return []
34
34
  end
@@ -0,0 +1,40 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder::Result
4
+ class Ipbase < Base
5
+ def ip
6
+ @data["data"]['ip']
7
+ end
8
+
9
+ def country_code
10
+ @data["data"]["location"]["country"]["alpha2"]
11
+ end
12
+
13
+ def country
14
+ @data["data"]["location"]["country"]["name"]
15
+ end
16
+
17
+ def state_code
18
+ @data["data"]["location"]["region"]["alpha2"]
19
+ end
20
+
21
+ def state
22
+ @data["data"]["location"]["region"]["name"]
23
+ end
24
+
25
+ def city
26
+ @data["data"]["location"]["city"]["name"]
27
+ end
28
+
29
+ def postal_code
30
+ @data["data"]["location"]["zip"]
31
+ end
32
+
33
+ def coordinates
34
+ [
35
+ @data["data"]["location"]["latitude"].to_f,
36
+ @data["data"]["location"]["longitude"].to_f
37
+ ]
38
+ end
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module Geocoder
2
- VERSION = "1.8.0"
2
+ VERSION = "1.8.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geocoder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.0
4
+ version: 1.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Reisner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-17 00:00:00.000000000 Z
11
+ date: 2022-09-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Object geocoding (by street or IP address), reverse geocoding (coordinates
14
14
  to street address), distance queries for ActiveRecord and Mongoid, result caching,
@@ -75,6 +75,7 @@ files:
75
75
  - lib/geocoder/lookups/here.rb
76
76
  - lib/geocoder/lookups/ip2location.rb
77
77
  - lib/geocoder/lookups/ipapi_com.rb
78
+ - lib/geocoder/lookups/ipbase.rb
78
79
  - lib/geocoder/lookups/ipdata_co.rb
79
80
  - lib/geocoder/lookups/ipgeolocation.rb
80
81
  - lib/geocoder/lookups/ipinfo_io.rb
@@ -138,6 +139,7 @@ files:
138
139
  - lib/geocoder/results/here.rb
139
140
  - lib/geocoder/results/ip2location.rb
140
141
  - lib/geocoder/results/ipapi_com.rb
142
+ - lib/geocoder/results/ipbase.rb
141
143
  - lib/geocoder/results/ipdata_co.rb
142
144
  - lib/geocoder/results/ipgeolocation.rb
143
145
  - lib/geocoder/results/ipinfo_io.rb
@@ -201,7 +203,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
201
203
  - !ruby/object:Gem::Version
202
204
  version: '0'
203
205
  requirements: []
204
- rubygems_version: 3.1.2
206
+ rubygems_version: 3.1.6
205
207
  signing_key:
206
208
  specification_version: 4
207
209
  summary: Complete geocoding solution for Ruby.