maxminddb 0.1.19 → 0.1.20

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
  SHA1:
3
- metadata.gz: baa15e403f8935e07c5f2c2286fc14267534a4ef
4
- data.tar.gz: 1cf297075926ddac4893041e858a96733a024b32
3
+ metadata.gz: 49781843be08dc83d3cc593d9754cdf344d270c1
4
+ data.tar.gz: 5d0d83b7ba2a3cb6945db5d34fc38c579ea13a91
5
5
  SHA512:
6
- metadata.gz: 3d92adbd9707dda2dc77e702a77680fa7c7b41541be96391c0630e053c106bbe2f4a9eaf562770ca0df492f9a0c71f7cda52ece0d47b4b1e8c35abc4ef9b3b2d
7
- data.tar.gz: 2bb2d4f9477331bb72a60beb61ac9289cceff933af353933b184a2cbe3f966cb1d53b2ffba51c507c21b41090a59cb1750ff9705f841319e636b8891ce5a7a89
6
+ metadata.gz: 7519d4cce445664ee6d1de8ad297e4cefff016971878130d71b472416167506bd484cb16decbb4bbdeaaee8bef4a6dde1908940dfee3c04b65a268ae2beebac6
7
+ data.tar.gz: 0d9a52e16cb853e4139eb452f807d8aec78702b0b566d6b814560d075abe9fa856afb1fc14be1cb6ffdc839862202ad982f5e9704f50a0a26a8c35e8e5f7ad44
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ### 0.1.20 (July 9, 2018)
4
+
5
+ - Add local_ip_alias
6
+
3
7
  ### 0.1.19 (June 22, 2018)
4
8
 
5
9
  - Implement a faster default reader
data/README.md CHANGED
@@ -54,6 +54,17 @@ ret.country.name # => nil
54
54
  ret.to_hash # => {}
55
55
  ```
56
56
 
57
+ For testing or other purposes, you might wish to treat localhost IP addresses as some other address - an external one. You can do this by assigning the desired external IP address to the attribute local_ip_alias:
58
+
59
+ ```ruby
60
+ db = MaxMindDB.new('./GeoLite2-City.mmdb')
61
+ ret = db.local_ip_alias = '74.125.225.224'
62
+ ret = db.lookup('127.0.0.1')
63
+ ret.found? # => true
64
+ ret.country.name # => 'United States'
65
+ ret.to_hash.empty? # => false
66
+ ```
67
+
57
68
  It's also possible to access the database metadata.
58
69
 
59
70
  ```ruby
@@ -19,6 +19,9 @@ module MaxMindDB
19
19
 
20
20
  attr_reader :metadata
21
21
 
22
+ # An IP that is used instead of local IPs
23
+ attr_accessor :local_ip_alias
24
+
22
25
  def initialize(path, file_reader = DEFAULT_FILE_READER)
23
26
  @path = path
24
27
  @data = file_reader.call(path)
@@ -38,9 +41,13 @@ module MaxMindDB
38
41
  "#<MaxMindDB::Client: DBPath:'#{@path}'>"
39
42
  end
40
43
 
41
- def lookup(ip)
44
+ def lookup(ip_or_hostname)
45
+ if @local_ip_alias && is_local?(ip_or_hostname)
46
+ ip_or_hostname = @local_ip_alias
47
+ end
48
+
42
49
  node_no = 0
43
- addr = addr_from_ip(ip)
50
+ addr = addr_from_ip(ip_or_hostname)
44
51
  start_idx = @ip_version == 4 ? 96 : 0
45
52
  for i in start_idx ... 128
46
53
  flag = (addr >> (127 - i)) & 1
@@ -162,16 +169,20 @@ module MaxMindDB
162
169
  bytes.inject(0){|r, v| (r << 8) + v }
163
170
  end
164
171
 
165
- def addr_from_ip(ip)
166
- klass = ip.class
172
+ def addr_from_ip(ip_or_hostname)
173
+ klass = ip_or_hostname.class
167
174
 
168
- return ip if RUBY_VERSION.to_f < 2.4 && (klass == Fixnum || klass == Bignum)
169
- return ip if RUBY_VERSION.to_f >= 2.4 && klass == Integer
175
+ return ip_or_hostname if RUBY_VERSION.to_f < 2.4 && (klass == Fixnum || klass == Bignum)
176
+ return ip_or_hostname if RUBY_VERSION.to_f >= 2.4 && klass == Integer
170
177
 
171
- addr = IPAddr.new(ip)
178
+ addr = IPAddr.new(ip_or_hostname)
172
179
  addr = addr.ipv4_compat if addr.ipv4?
173
180
  addr.to_i
174
181
  end
182
+
183
+ def is_local?(ip_or_hostname)
184
+ ["127.0.0.1", "localhost", "::1", "0000::1", "0:0:0:0:0:0:0:1"].include? ip_or_hostname
185
+ end
175
186
  end
176
187
  end
177
188
 
@@ -1,3 +1,3 @@
1
1
  module MaxMindDB
2
- VERSION = "0.1.19"
2
+ VERSION = "0.1.20"
3
3
  end
@@ -83,6 +83,7 @@ describe MaxMindDB do
83
83
 
84
84
  context 'for a Canadian ipv6' do
85
85
  let(:ip) { '2607:5300:60:72ba::' }
86
+
86
87
  it 'finds data' do
87
88
  expect(city_db.lookup(ip)).to be_found
88
89
  end
@@ -124,6 +125,54 @@ describe MaxMindDB do
124
125
  end
125
126
  end
126
127
 
128
+ context 'for local ip addresses' do
129
+ let(:ip) { '127.0.0.1' }
130
+
131
+ context 'for city_db' do
132
+ before do
133
+ city_db.local_ip_alias = '74.125.225.224'
134
+ end
135
+
136
+ it 'returns a MaxMindDB::Result' do
137
+ expect(city_db.lookup(ip)).to be_kind_of(MaxMindDB::Result)
138
+ end
139
+
140
+ it 'finds data' do
141
+ expect(city_db.lookup(ip)).to be_found
142
+ end
143
+
144
+ it 'returns Mountain View as the English name' do
145
+ expect(city_db.lookup(ip).city.name).to eq('Alameda')
146
+ end
147
+
148
+ it 'returns -122.0574 as the longitude' do
149
+ expect(city_db.lookup(ip).location.longitude).to eq(-122.2788)
150
+ end
151
+
152
+ it 'returns nil for is_anonymous_proxy' do
153
+ expect(city_db.lookup(ip).traits.is_anonymous_proxy).to eq(nil)
154
+ end
155
+ end
156
+
157
+ context 'for country_db' do
158
+ before do
159
+ country_db.local_ip_alias = '74.125.225.224'
160
+ end
161
+
162
+ it 'returns United States as the English country name' do
163
+ expect(country_db.lookup(ip).country.name).to eq('United States')
164
+ end
165
+
166
+ it 'returns false for the is_in_european_union' do
167
+ expect(country_db.lookup(ip).country.is_in_european_union).to eq(nil)
168
+ end
169
+
170
+ it 'returns US as the country iso code' do
171
+ expect(country_db.lookup(ip).country.iso_code).to eq('US')
172
+ end
173
+ end
174
+ end
175
+
127
176
  context 'for 32bit record data' do
128
177
  let(:ip) { '1.0.16.1' }
129
178
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maxminddb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.19
4
+ version: 0.1.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - yhirose
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-23 00:00:00.000000000 Z
11
+ date: 2018-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler