geocoder 1.7.0 → 1.7.1

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: 86304e3364377f7c93cc2a91dba1235316b63469ee1c09261b0763db052bcded
4
- data.tar.gz: ab4d2167f60e9149bb052e98bf11df40f7b68a0f75873d7c95e638e7c84acda2
3
+ metadata.gz: 29b831bd2fcb54be9ffadc479be5c2010fb11191b1f240e0868d8ff6bdd379af
4
+ data.tar.gz: 0d9ca932a2cdcd36b0014e271a96afbc867f6b0f151b1e119c533d64b260048e
5
5
  SHA512:
6
- metadata.gz: 3d8a017acef8c3c15e48c641311b38363f28d7900047fc5bf371d669dcd083401736b30de9ccc859290234a1d44308694de338bddca4cd2706df35fe2c060dbe
7
- data.tar.gz: 372826b2a2ac13ed235eb7a78c9fdb4b214d54e28672a9ff24ae88f16ef9f4d07c867d6d09f0e90cd6e7e0e9adea975a9932356a2238a9a0eb15f987ee5d4c69
6
+ metadata.gz: aa027cbdf72f35e4c19e9413f483c245a7a8b1286ac5ad9cc9c527fd8d05e9ab4dd6a43065a60dac089ec94e2798228f823a2d517d8b0b36ef0c978a9bc59cd6
7
+ data.tar.gz: 20e927de754e571eb2587734a2027e5aeb57bb8a38a487b6a8d04504bb6b22678b4ab2c9485e0a79d0006969468254a8c094e543192a0f305b62346adb129c43
data/CHANGELOG.md CHANGED
@@ -3,6 +3,10 @@ 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.7.1 (2022 Jan 1)
7
+ -------------------
8
+ * Various bugfixes and refactorings.
9
+
6
10
  1.7.0 (2021 Oct 11)
7
11
  -------------------
8
12
  * Add support for Geoapify and Photo lookups (thanks github.com/ahukkanen).
data/README.md CHANGED
@@ -246,9 +246,12 @@ Geocoder.configure(
246
246
  units: :km,
247
247
 
248
248
  # caching (see Caching section below for details):
249
+ # warning: `cache_prefix` is deprecated, use `cache_options` instead
249
250
  cache: Redis.new,
250
- cache_prefix: "..."
251
-
251
+ cache_options: {
252
+ expiration: 2.days, # Redis ttl
253
+ prefix: "..."
254
+ }
252
255
  )
253
256
  ```
254
257
 
@@ -9,7 +9,7 @@ Geocoder.configure(
9
9
  # https_proxy: nil, # HTTPS proxy server (user:pass@host:port)
10
10
  # api_key: nil, # API key for geocoding service
11
11
  # cache: nil, # cache object (must respond to #[], #[]=, and #del)
12
- # cache_prefix: 'geocoder:', # prefix (string) to use for all cache keys
12
+ # cache_prefix: 'geocoder:', # DEPRECATED, please use cache_options[:prefix] instead
13
13
 
14
14
  # Exceptions that should not be rescued by default
15
15
  # (if you want to implement custom error handling);
@@ -19,4 +19,10 @@ Geocoder.configure(
19
19
  # Calculation options
20
20
  # units: :mi, # :km for kilometers or :mi for miles
21
21
  # distances: :linear # :spherical or :linear
22
+
23
+ # Cache configuration
24
+ # cache_options: {
25
+ # expiration: 2.days,
26
+ # prefix: 'geocoder:'
27
+ # }
22
28
  )
@@ -1,23 +1,18 @@
1
+ Dir["#{__dir__}/cache_stores/*.rb"].each {|file| require file }
2
+
1
3
  module Geocoder
2
4
  class Cache
3
5
 
4
- def initialize(store, prefix)
5
- @store = store
6
- @prefix = prefix
6
+ def initialize(store, config)
7
+ @class = (Object.const_get("Geocoder::CacheStore::#{store.class}") rescue Geocoder::CacheStore::Generic)
8
+ @store_service = @class.new(store, config)
7
9
  end
8
10
 
9
11
  ##
10
12
  # Read from the Cache.
11
13
  #
12
14
  def [](url)
13
- interpret case
14
- when store.respond_to?(:[])
15
- store[key_for(url)]
16
- when store.respond_to?(:get)
17
- store.get key_for(url)
18
- when store.respond_to?(:read)
19
- store.read key_for(url)
20
- end
15
+ interpret store_service.read(url)
21
16
  rescue => e
22
17
  warn "Geocoder cache read error: #{e}"
23
18
  end
@@ -26,14 +21,7 @@ module Geocoder
26
21
  # Write to the Cache.
27
22
  #
28
23
  def []=(url, value)
29
- case
30
- when store.respond_to?(:[]=)
31
- store[key_for(url)] = value
32
- when store.respond_to?(:set)
33
- store.set key_for(url), value
34
- when store.respond_to?(:write)
35
- store.write key_for(url), value
36
- end
24
+ store_service.write(url, value)
37
25
  rescue => e
38
26
  warn "Geocoder cache write error: #{e}"
39
27
  end
@@ -44,7 +32,7 @@ module Geocoder
44
32
  #
45
33
  def expire(url)
46
34
  if url == :all
47
- if store.respond_to?(:keys)
35
+ if store_service.respond_to?(:keys)
48
36
  urls.each{ |u| expire(u) }
49
37
  else
50
38
  raise(NoMethodError, "The Geocoder cache store must implement `#keys` for `expire(:all)` to work")
@@ -57,33 +45,21 @@ module Geocoder
57
45
 
58
46
  private # ----------------------------------------------------------------
59
47
 
60
- def prefix; @prefix; end
61
- def store; @store; end
62
-
63
- ##
64
- # Cache key for a given URL.
65
- #
66
- def key_for(url)
67
- if url.match(/^#{prefix}/)
68
- url
69
- else
70
- [prefix, url].join
71
- end
72
- end
48
+ def store_service; @store_service; end
73
49
 
74
50
  ##
75
51
  # Array of keys with the currently configured prefix
76
52
  # that have non-nil values.
77
53
  #
78
54
  def keys
79
- store.keys.select{ |k| k.match(/^#{prefix}/) and self[k] }
55
+ store_service.keys
80
56
  end
81
57
 
82
58
  ##
83
59
  # Array of cached URLs.
84
60
  #
85
61
  def urls
86
- keys.map{ |k| k[/^#{prefix}(.*)/, 1] }
62
+ store_service.urls
87
63
  end
88
64
 
89
65
  ##
@@ -95,8 +71,7 @@ module Geocoder
95
71
  end
96
72
 
97
73
  def expire_single_url(url)
98
- key = key_for(url)
99
- store.respond_to?(:del) ? store.del(key) : store.delete(key)
74
+ store_service.remove(url)
100
75
  end
101
76
  end
102
77
  end
@@ -0,0 +1,40 @@
1
+ module Geocoder::CacheStore
2
+ class Base
3
+ def initialize(store, options)
4
+ @store = store
5
+ @config = options
6
+ @prefix = config[:prefix]
7
+ end
8
+
9
+ ##
10
+ # Array of keys with the currently configured prefix
11
+ # that have non-nil values.
12
+ def keys
13
+ store.keys.select { |k| k.match(/^#{prefix}/) and self[k] }
14
+ end
15
+
16
+ ##
17
+ # Array of cached URLs.
18
+ #
19
+ def urls
20
+ keys
21
+ end
22
+
23
+ protected # ----------------------------------------------------------------
24
+
25
+ def prefix; @prefix; end
26
+ def store; @store; end
27
+ def config; @config; end
28
+
29
+ ##
30
+ # Cache key for a given URL.
31
+ #
32
+ def key_for(url)
33
+ if url.match(/^#{prefix}/)
34
+ url
35
+ else
36
+ [prefix, url].join
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,33 @@
1
+ module Geocoder::CacheStore
2
+ class Generic < Base
3
+ def write(url, value)
4
+ case
5
+ when store.respond_to?(:[]=)
6
+ store[key_for(url)] = value
7
+ when store.respond_to?(:set)
8
+ store.set key_for(url), value
9
+ when store.respond_to?(:write)
10
+ store.write key_for(url), value
11
+ end
12
+ end
13
+
14
+ def read(url)
15
+ case
16
+ when store.respond_to?(:[])
17
+ store[key_for(url)]
18
+ when store.respond_to?(:get)
19
+ store.get key_for(url)
20
+ when store.respond_to?(:read)
21
+ store.read key_for(url)
22
+ end
23
+ end
24
+
25
+ def keys
26
+ store.keys
27
+ end
28
+
29
+ def remove(key)
30
+ store.delete(key)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ module Geocoder::CacheStore
2
+ class Redis < Base
3
+ def initialize(store, options)
4
+ super
5
+ @expiration = options[:expiration]
6
+ end
7
+
8
+ def write(url, value, expire = @expiration)
9
+ if expire.present?
10
+ store.set key_for(url), value, ex: expire
11
+ else
12
+ store.set key_for(url), value
13
+ end
14
+ end
15
+
16
+ def read(url)
17
+ store.get key_for(url)
18
+ end
19
+
20
+ def keys
21
+ store.keys("#{prefix}*")
22
+ end
23
+
24
+ def remove(key)
25
+ store.del(key)
26
+ end
27
+
28
+ private # ----------------------------------------------------------------
29
+
30
+ def expire; @expiration; end
31
+ end
32
+ end
@@ -68,7 +68,8 @@ module Geocoder
68
68
  :distances,
69
69
  :basic_auth,
70
70
  :logger,
71
- :kernel_logger_level
71
+ :kernel_logger_level,
72
+ :cache_options
72
73
  ]
73
74
 
74
75
  attr_accessor :data
@@ -108,7 +109,7 @@ module Geocoder
108
109
  @data[:https_proxy] = nil # HTTPS proxy server (user:pass@host:port)
109
110
  @data[:api_key] = nil # API key for geocoding service
110
111
  @data[:cache] = nil # cache object (must respond to #[], #[]=, and #keys)
111
- @data[:cache_prefix] = "geocoder:" # prefix (string) to use for all cache keys
112
+ @data[:cache_prefix] = "geocoder:" # - DEPRECATED - prefix (string) to use for all cache keys
112
113
  @data[:basic_auth] = {} # user and password for basic auth ({:user => "user", :password => "password"})
113
114
  @data[:logger] = :kernel # :kernel or Logger instance
114
115
  @data[:kernel_logger_level] = ::Logger::WARN # log level, if kernel logger is used
@@ -121,6 +122,9 @@ module Geocoder
121
122
  # calculation options
122
123
  @data[:units] = :mi # :mi or :km
123
124
  @data[:distances] = :linear # :linear or :spherical
125
+
126
+ # explicit cache service options
127
+ @data[:cache_options] = {}
124
128
  end
125
129
 
126
130
  instance_eval(OPTIONS.map do |option|
@@ -4,12 +4,21 @@ require 'geocoder/results/amazon_location_service'
4
4
  module Geocoder::Lookup
5
5
  class AmazonLocationService < Base
6
6
  def results(query)
7
- params = { **global_index_name, **query.options }
8
- if query.reverse_geocode?
9
- resp = client.search_place_index_for_position(**{ **params, position: query.coordinates.reverse })
7
+ params = query.options.dup
8
+
9
+ # index_name is required
10
+ # Aws::ParamValidator raises ArgumentError on missing required keys
11
+ params.merge!(index_name: configuration[:index_name])
12
+
13
+ # Aws::ParamValidator raises ArgumentError on unexpected keys
14
+ params.delete(:lookup)
15
+
16
+ resp = if query.reverse_geocode?
17
+ client.search_place_index_for_position(params.merge(position: query.coordinates.reverse))
10
18
  else
11
- resp = client.search_place_index_for_text(**{ **params, text: query.text })
19
+ client.search_place_index_for_text(params.merge(text: query.text))
12
20
  end
21
+
13
22
  resp.results.map(&:place)
14
23
  end
15
24
 
@@ -41,13 +50,5 @@ module Geocoder::Lookup
41
50
  )
42
51
  end
43
52
  end
44
-
45
- def global_index_name
46
- if configuration[:index_name]
47
- { index_name: configuration[:index_name] }
48
- else
49
- {}
50
- end
51
- end
52
53
  end
53
54
  end
@@ -83,8 +83,14 @@ module Geocoder
83
83
  # The working Cache object.
84
84
  #
85
85
  def cache
86
- if @cache.nil? and store = configuration.cache
87
- @cache = Cache.new(store, configuration.cache_prefix)
86
+ if @cache.nil? && (store = configuration.cache)
87
+ cache_options = configuration.cache_options
88
+ cache_prefix = (configuration.cache_prefix rescue false)
89
+ if cache_prefix
90
+ cache_options[:prefix] ||= configuration.cache_prefix
91
+ warn '[Geocoder] cache_prefix is deprecated, please change to cache_options.prefix instead'
92
+ end
93
+ @cache = Cache.new(store, cache_options)
88
94
  end
89
95
  @cache
90
96
  end
@@ -47,7 +47,7 @@ module Geocoder::Lookup
47
47
  end
48
48
 
49
49
  def host
50
- "api.ipdata.co"
50
+ configuration[:host] || "api.ipdata.co"
51
51
  end
52
52
 
53
53
  def check_response_for_errors!(response)
@@ -1,3 +1,3 @@
1
1
  module Geocoder
2
- VERSION = "1.7.0"
2
+ VERSION = "1.7.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.7.0
4
+ version: 1.7.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: 2021-10-11 00:00:00.000000000 Z
11
+ date: 2022-01-01 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,
@@ -39,6 +39,9 @@ files:
39
39
  - lib/generators/geocoder/migration_version.rb
40
40
  - lib/geocoder.rb
41
41
  - lib/geocoder/cache.rb
42
+ - lib/geocoder/cache_stores/base.rb
43
+ - lib/geocoder/cache_stores/generic.rb
44
+ - lib/geocoder/cache_stores/redis.rb
42
45
  - lib/geocoder/calculations.rb
43
46
  - lib/geocoder/cli.rb
44
47
  - lib/geocoder/configuration.rb