ip_lookup 1.1.0 → 1.2.2

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: fa5e7ff8053216bab3b111d02bf910ef281ba71c8a8abed3c6727810a47b7c5e
4
- data.tar.gz: 68939591bbaaac10a9dee2bbb7c5ded2d1df946e4317734c88882c4403c251a7
3
+ metadata.gz: 8d6593b39b336032a59d5271dd66cdd0eab98d761eae3f4b9ba7e5029781ee2d
4
+ data.tar.gz: 31a52ea45909d4de79db6f0d43e8d3cbb1d927c27970c6ff3ed2c4c363e033b2
5
5
  SHA512:
6
- metadata.gz: 0af25c457c827ea5424ac13aaee00902636e97d169e1f18264452e0873c7c54f247e05fec9d6d448d5f6cc8d4bb997d1a6a1cdb345274e734a39f8d01af99965
7
- data.tar.gz: 3876a9eebabc872ae120833b480986465c7fb37a3116d334ae9d9bfef63e93a3f98ada1888f68fb028caef0378d8b8c8560a0aa7479c1d762e48f590fcf54dc5
6
+ metadata.gz: bf2cbaaf566345c12f089321f89fa818d72122da8924fb8e5284b2d5edb39db35fe302b210ab69a4bb3a7e3a62044ad7f6f22b541d65798f6921d2412b1581ef
7
+ data.tar.gz: ec4966ad08a3467f51ed81abafe752b332e76fa9d69d83815ff35706c6081e7bdafe1e258b5195101c5619c1903337a441f03e8931eb79e3c8b4ef723d734899
data/README.md CHANGED
@@ -26,7 +26,6 @@ Or install it yourself as:
26
26
 
27
27
  Before using the gem the db file needs to be downloaded / updated.
28
28
 
29
-
30
29
  ```ruby
31
30
  # download / update database
32
31
  IPLookup.DB.update update_period: 30, update_uri: "http://path_to_database_GeoLite2-City.mmdb.gz"
@@ -35,12 +34,12 @@ IPLookup.DB.update update_period: 30, update_uri: "http://path_to_database_GeoLi
35
34
  - default update_period (in days): 30
36
35
  - default update_uri: http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz
37
36
 
38
- *NOTE: The gem provides a RubyGems post-install hook to download / update the db.*
37
+ *NOTE: The gem provides post-install hook to download / update the db using Gem extensions.*
39
38
 
40
39
  ### Usage
41
40
 
42
41
  ```ruby
43
- ip_lookup = IPLookup.new("213.61.214.178"), silent_exceptions: false
42
+ ip_lookup = IPLookup.new "213.61.214.178", silent_exceptions: false
44
43
 
45
44
  ip_lookup.country
46
45
  # => "de"
@@ -58,6 +57,8 @@ ip_lookup.subdivision
58
57
  *Defaults:*
59
58
  - silent_exceptions: false
60
59
 
60
+ *NOTE: If silent exceptions is set to true, IPLookup returns default values in case the IP address couln't be looked up.
61
+
61
62
  ## Development
62
63
 
63
64
  After checking out the repo, run bin/setup to install dependencies. Then, run `rake spec` to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
data/lib/ip_lookup.rb CHANGED
@@ -14,35 +14,36 @@ class IPLookup
14
14
  def initialize(ip_address, silent_exceptions: false)
15
15
  @silent_exceptions = silent_exceptions
16
16
  @result = DB.new(silent_exceptions).lookup(ip_address)
17
+ rescue IPAddr::InvalidAddressError, IPAddr::AddressFamilyError => e
18
+ raise e unless silent_exceptions
17
19
  end
18
20
 
19
21
  def country
20
- error_handler(DEFAULT_COUNTRY) do
21
- iso_code = result.country.iso_code
22
- iso_code ? iso_code.downcase : DEFAULT_COUNTRY
23
- end
22
+ return DEFAULT_COUNTRY unless result&.found?
23
+
24
+ iso_code = result.country.iso_code
25
+ iso_code ? iso_code.downcase : DEFAULT_COUNTRY
24
26
  end
25
27
 
26
28
  def timezone
27
- error_handler(DEFAULT_TIMEZONE) do
28
- timezone = result.location.time_zone
29
+ return DEFAULT_TIMEZONE unless result&.found?
29
30
 
30
- timezone ||= timezone_for result.country.iso_code if result.country.iso_code
31
+ timezone = result.location.time_zone
31
32
 
32
- timezone || DEFAULT_TIMEZONE
33
- end
33
+ timezone ||= timezone_for result.country.iso_code if result.country.iso_code
34
+ timezone || DEFAULT_TIMEZONE
34
35
  end
35
36
 
36
37
  def coordinates
37
- error_handler(DEFAULT_COORDINATES) do
38
- [result.location.latitude, result.location.longitude]
39
- end
38
+ return DEFAULT_COORDINATES unless result&.found?
39
+
40
+ [result.location.latitude, result.location.longitude]
40
41
  end
41
42
 
42
43
  def subdivision
43
- error_handler(DEFAULT_SUBDIVISION) do
44
- result.subdivisions.any? ? result.subdivisions.first.iso_code.downcase : DEFAULT_SUBDIVISION
45
- end
44
+ return DEFAULT_SUBDIVISION unless result&.found?
45
+
46
+ result.subdivisions.any? ? result.subdivisions.first.iso_code.downcase : DEFAULT_SUBDIVISION
46
47
  end
47
48
 
48
49
  private
@@ -51,12 +52,4 @@ class IPLookup
51
52
  tz = TZInfo::Country.get iso_code
52
53
  tz.zone_identifiers.first if tz.zone_identifiers.any?
53
54
  end
54
-
55
- def error_handler(default)
56
- result&.found? ? yield : default
57
- rescue IPAddr::InvalidAddressError, IPAddr::AddressFamilyError
58
- raise e unless silent_exceptions
59
-
60
- default
61
- end
62
55
  end
data/lib/ip_lookup/db.rb CHANGED
@@ -15,26 +15,25 @@ class IPLookup
15
15
  end
16
16
 
17
17
  def lookup(ip_address)
18
- db.lookup ip_address
18
+ self.class.db.lookup ip_address
19
19
  rescue Errno::ENOENT
20
20
  raise DBFileNotFoundError, "Couldn't find DB file. Please update DB before using lookup." unless silent_exceptions
21
21
  end
22
22
 
23
- def self.mutex
24
- @mutex ||= Mutex.new
25
- end
26
-
27
- private
23
+ class << self
28
24
 
29
- def db
30
- self.class.mutex.synchronize do
31
- @db ||= begin
32
- MaxMindDB.new self.class.full_db_path
25
+ def db
26
+ mutex.synchronize do
27
+ @db ||= begin
28
+ MaxMindDB.new full_db_path
29
+ end
33
30
  end
34
31
  end
35
- end
36
32
 
37
- class << self
33
+ def mutex
34
+ @mutex ||= Mutex.new
35
+ end
36
+
38
37
  def update(update_period: DEFAULT_UPDATE_PERIOD_IN_DAYS, update_uri: DEFAULT_UPDATE_URI)
39
38
  return unless update_db? update_period
40
39
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class IPLookup
4
- VERSION = "1.1.0"
4
+ VERSION = "1.2.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ip_lookup
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ole Richter
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2018-08-14 00:00:00.000000000 Z
13
+ date: 2018-08-15 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: maxminddb