ip2location_ruby 8.0.3 → 8.1.0

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: 348d1fba82579c218372c4ef13483ca094ddd971d4c99ee6894533e4c80533ef
4
- data.tar.gz: de5b0eeb6f5eb9f3a80db2b2daaeb7e458f3f55843ae90709e728b952ee1bebd
3
+ metadata.gz: 613585ff34abe6d207beb9de5a02a2b5291c6fdb9f6b2c392bc450faa92c393b
4
+ data.tar.gz: cda9e736447f28ca68af0617cb4c12b6983347630aa8b2b9070fcc4aaa17161b
5
5
  SHA512:
6
- metadata.gz: d1993927999e913ebbadec26352c24fb4f5305b76edbd173b38ef8407e98facbc11c33761c3f614ed6bc035a57d15a16797cbd6f11d0122766fc063e2101a1c9
7
- data.tar.gz: 3d012bc0e917f1ac06415f82f05d824b8d3b2ccb7f02a83abdb8ed3bd84648d742f3958381414ceceec115ea01223a8f8cfb135924d6bb3737583f0aca548fac
6
+ metadata.gz: 041d86f010371a8640341a61e76d442f2511176f3369cf27f200e042e284ee62c7561feebb646e78cffa4ec84ca5a2626cff3f8b0722dec274f99780b4d8b01a
7
+ data.tar.gz: 333b9c07798a16ef3af4a60d07329a828087d132a0f81a2ccffc89b9596c0f0e4718a113612c539a1f039ccfd78e681da0deb7661a6ed44b19bfd08ad3f9a352
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 Vincent.Z ( zhu1230@gmail.com )
1
+ Copyright (c) 2019 IP2Location ( support@ip2location.com )
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -12,20 +12,39 @@ For more details, please visit:
12
12
 
13
13
  # Usage
14
14
 
15
- i2l = Ip2location.new.open(File.dirname(__FILE__) + "/assets/IP-COUNTRY-SAMPLE.bin")
16
- record = i2l.get_all('13.5.10.6')
17
- record.should_not be_nil
18
- record.country_short.should == 'US'
19
-
20
- # Contributing to ip2location
21
-
22
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
23
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
24
- * Fork the project.
25
- * Start a feature/bugfix branch.
26
- * Commit and push until you are happy with your contribution.
27
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
28
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
15
+ ```
16
+ require 'ip2location_ruby'
17
+
18
+ i2l = Ip2location.new.open("./data/IPV6-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN")
19
+ record = i2l.get_all('8.8.8.8')
20
+
21
+ print 'Country Code: ' + record['country_short'] + "\n"
22
+ print 'Country Name: ' + record['country_long'] + "\n"
23
+ print 'Region Name: ' + record['region'] + "\n"
24
+ print 'City Name: ' + record['city'] + "\n"
25
+ print 'Latitude: '
26
+ print record['latitude']
27
+ print "\n"
28
+ print 'Longitude: '
29
+ print record['longitude']
30
+ print "\n"
31
+ print 'ISP: ' + record['isp'] + "\n"
32
+ print 'Domain: ' + record['domain'] + "\n"
33
+ print 'Net Speed: ' + record['netspeed'] + "\n"
34
+ print 'Area Code: ' + record['areacode'] + "\n"
35
+ print 'IDD Code: ' + record['iddcode'] + "\n"
36
+ print 'Time Zone: ' + record['timezone'] + "\n"
37
+ print 'ZIP Code: ' + record['zipcode'] + "\n"
38
+ print 'Weather Station Code: ' + record['weatherstationname'] + "\n"
39
+ print 'Weather Station Name: ' + record['weatherstationcode'] + "\n"
40
+ print 'MCC: ' + record['mcc'] + "\n"
41
+ print 'MNC: ' + record['mnc'] + "\n"
42
+ print 'Mobile Name: ' + record['mobilebrand'] + "\n"
43
+ print 'Elevation: '
44
+ print record['elevation']
45
+ print "\n"
46
+ print 'Usage Type: ' + record['usagetype'] + "\n"
47
+ ```
29
48
 
30
49
  # Sample BIN Databases
31
50
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 8.0.3
1
+ 8.1.0
@@ -0,0 +1,31 @@
1
+ require 'ip2location_ruby'
2
+
3
+ i2l = Ip2location.new.open("./data/IP2LOCATION-LITE-DB1.IPV6.BIN")
4
+ record = i2l.get_all('8.8.8.8')
5
+
6
+ print 'Country Code: ' + record['country_short'] + "\n"
7
+ print 'Country Name: ' + record['country_long'] + "\n"
8
+ print 'Region Name: ' + record['region'] + "\n"
9
+ print 'City Name: ' + record['city'] + "\n"
10
+ print 'Latitude: '
11
+ print record['latitude']
12
+ print "\n"
13
+ print 'Longitude: '
14
+ print record['longitude']
15
+ print "\n"
16
+ print 'ISP: ' + record['isp'] + "\n"
17
+ print 'Domain: ' + record['domain'] + "\n"
18
+ print 'Net Speed: ' + record['netspeed'] + "\n"
19
+ print 'Area Code: ' + record['areacode'] + "\n"
20
+ print 'IDD Code: ' + record['iddcode'] + "\n"
21
+ print 'Time Zone: ' + record['timezone'] + "\n"
22
+ print 'ZIP Code: ' + record['zipcode'] + "\n"
23
+ print 'Weather Station Code: ' + record['weatherstationname'] + "\n"
24
+ print 'Weather Station Name: ' + record['weatherstationcode'] + "\n"
25
+ print 'MCC: ' + record['mcc'] + "\n"
26
+ print 'MNC: ' + record['mnc'] + "\n"
27
+ print 'Mobile Name: ' + record['mobilebrand'] + "\n"
28
+ print 'Elevation: '
29
+ print record['elevation']
30
+ print "\n"
31
+ print 'Usage Type: ' + record['usagetype'] + "\n"
@@ -2,16 +2,15 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: ip2location_ruby 8.0.2 ruby lib
6
5
 
7
6
  Gem::Specification.new do |s|
8
7
  s.name = "ip2location_ruby"
9
- s.version = "8.0.3"
8
+ s.version = "8.1.0"
10
9
 
11
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
11
  s.require_paths = ["lib"]
13
- s.authors = ["Vincent.Z"]
14
- s.date = "2016-07-25"
12
+ s.authors = ["ip2location"]
13
+ s.email = ["support@ip2location.com"]
15
14
  s.description = "The official IP2Location Ruby library to geolocate an IP address. You can lookup for country, region, city, latitude, longitude, zip code, time zone, ISP, domain, area code, usage type, mobile data, weather data and elevation from an IP address. Supported both IPv4 and IPv6 lookup."
16
15
  s.email = "support@ip2location.com"
17
16
  s.extra_rdoc_files = [
@@ -27,6 +26,7 @@ Gem::Specification.new do |s|
27
26
  "Rakefile",
28
27
  "VERSION",
29
28
  "ip2location_ruby.gemspec",
29
+ "example.rb",
30
30
  "lib/ip2location_ruby.rb",
31
31
  "lib/ip2location_ruby/database_config.rb",
32
32
  "lib/ip2location_ruby/i2l_float_data.rb",
@@ -38,11 +38,11 @@ Gem::Specification.new do |s|
38
38
  "spec/assets/DB24.DEMO6.BIN",
39
39
  "spec/assets/IP-COUNTRY-SAMPLE.BIN",
40
40
  "spec/ip2location_ruby_spec.rb",
41
- "spec/spec_helper.rb"
41
+ "spec/spec_helper.rb",
42
+ "rb/data/IP2LOCATION-LITE-DB1.IPV6.BIN"
42
43
  ]
43
44
  s.homepage = "https://github.com/ip2location/ip2location-ruby"
44
45
  s.licenses = ["MIT"]
45
- s.rubygems_version = "2.4.1"
46
46
  s.summary = "the ip2location ruby library"
47
47
 
48
48
  if s.respond_to?(:metadata=)
@@ -10,7 +10,10 @@ require 'ip2location_ruby/ip2location_record'
10
10
 
11
11
  class Ip2location
12
12
  attr_accessor :record_class4, :record_class6, :v4, :file, :db_index, :count, :base_addr, :ipno, :count, :record, :database, :columns, :ip_version, :ipv4databasecount, :ipv4databaseaddr, :ipv4indexbaseaddr, :ipv6databasecount, :ipv6databaseaddr, :ipv6indexbaseaddr
13
-
13
+
14
+ FIELD_NOT_SUPPORTED = 'NOT SUPPORTED'
15
+ INVALID_IP_ADDRESS = 'INVALID IP ADDRESS'
16
+
14
17
  def open(url)
15
18
  self.file = File.open(File.expand_path url, 'rb')
16
19
  i2l = Ip2locationConfig.read(file)
@@ -23,178 +26,510 @@ class Ip2location
23
26
  self.ipv6databaseaddr = i2l.ipv6databaseaddr
24
27
  self.ipv4indexbaseaddr = i2l.ipv4indexbaseaddr
25
28
  self.ipv6indexbaseaddr = i2l.ipv6indexbaseaddr
26
- self.record_class4 = (Ip2LocationRecord.init database, 4)
27
- self.record_class6 = (Ip2LocationRecord.init database, 6)
29
+ self.record_class4 = (Ip2LocationRecord.init database, 4)
30
+ self.record_class6 = (Ip2LocationRecord.init database, 6)
28
31
  self
29
32
  end
30
-
31
- def get_all(ip)
33
+
34
+ def get_record(ip)
32
35
  ipno = IPAddr.new(ip, Socket::AF_UNSPEC)
33
- self.ip_version = ipno.ipv4? ? 4 : 6
34
- self.v4 = ipno.ipv4?
35
- self.count = ipno.ipv4? ? self.ipv4databasecount + 0 : self.ipv6databasecount + 0
36
- self.base_addr = (ipno.ipv4? ? self.ipv4databaseaddr - 1 : self.ipv6databaseaddr - 1)
37
-
38
- ipnum = ipno.to_i + 0
36
+ self.ip_version, ipnum = validateip(ipno)
37
+ self.v4 = ip_version == 4 ? true : false
38
+ self.count = v4 ? self.ipv4databasecount + 0 : self.ipv6databasecount + 0
39
+ self.base_addr = (v4 ? self.ipv4databaseaddr - 1 : self.ipv6databaseaddr - 1)
39
40
  col_length = columns * 4
40
-
41
- if ipv4indexbaseaddr > 0 || ipv6indexbaseaddr > 0
42
- indexpos = 0
43
- case ip_version
44
- when 4
45
- ipnum1_2 = (ipnum >> 16)
46
- indexpos = ipv4indexbaseaddr + (ipnum1_2 << 3)
47
- when 6
48
- ipnum1 = (ipnum / (2**112))
49
- indexpos = ipv6indexbaseaddr + (ipnum1 << 3)
50
- end
51
- low = read32(indexpos)
52
- high = read32(indexpos + 4)
53
- return self.record = bsearch(low, high, ipnum, self.base_addr, col_length)
54
- else
55
- return self.record = bsearch(0, self.count, ipnum, self.base_addr, col_length)
56
- end
57
- end
58
-
41
+ if ipv4indexbaseaddr > 0 || ipv6indexbaseaddr > 0
42
+ indexpos = 0
43
+ case ip_version
44
+ when 4
45
+ indexpos = ipv4indexbaseaddr + ((ipnum >> 16) << 3)
46
+ realipno = ipnum
47
+ # if ipnum reach MAX_IPV4_RANGE
48
+ if realipno == 4294967295
49
+ ipnum = realipno - 1
50
+ end
51
+ when 6
52
+ indexpos = ipv6indexbaseaddr + ((ipnum >> 112) << 3)
53
+ realipno = ipnum
54
+ # if ipnum reach MAX_IPV6_RANGE
55
+ if realipno == 340282366920938463463374607431768211455
56
+ ipnum = realipno - 1
57
+ end
58
+ end
59
+ low = read32(indexpos)
60
+ high = read32(indexpos + 4)
61
+ return self.record = bsearch(low, high, ipnum, self.base_addr, col_length)
62
+ else
63
+ return self.record = bsearch(0, self.count, ipnum, self.base_addr, col_length)
64
+ end
65
+ end
66
+
67
+ def get_all(ip)
68
+ valid = !(IPAddr.new(ip) rescue nil).nil?
69
+ if valid
70
+ rec = get_record(ip)
71
+ if !(rec.nil?)
72
+ country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
73
+ country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
74
+ region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
75
+ city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
76
+ latitude = (defined?(rec.latitude) && rec.latitude != '') ? rec.latitude : FIELD_NOT_SUPPORTED
77
+ longitude = (defined?(rec.longitude) && rec.longitude != '') ? rec.longitude : FIELD_NOT_SUPPORTED
78
+ isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
79
+ domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
80
+ netspeed = (defined?(rec.netspeed) && rec.netspeed != '') ? rec.netspeed : FIELD_NOT_SUPPORTED
81
+ areacode = (defined?(rec.areacode) && rec.areacode != '') ? rec.areacode : FIELD_NOT_SUPPORTED
82
+ iddcode = (defined?(rec.iddcode) && rec.iddcode != '') ? rec.iddcode : FIELD_NOT_SUPPORTED
83
+ timezone = (defined?(rec.timezone) && rec.timezone != '') ? rec.timezone : FIELD_NOT_SUPPORTED
84
+ zipcode = (defined?(rec.zipcode) && rec.zipcode != '') ? rec.zipcode : FIELD_NOT_SUPPORTED
85
+ weatherstationname = (defined?(rec.weatherstationname) && rec.weatherstationname != '') ? rec.weatherstationname : FIELD_NOT_SUPPORTED
86
+ weatherstationcode = (defined?(rec.weatherstationcode) && rec.weatherstationcode != '') ? rec.weatherstationcode : FIELD_NOT_SUPPORTED
87
+ mcc = (defined?(rec.mcc) && rec.mcc != '') ? rec.mcc : FIELD_NOT_SUPPORTED
88
+ mnc = (defined?(rec.mnc) && rec.mnc != '') ? rec.mnc : FIELD_NOT_SUPPORTED
89
+ mobilebrand = (defined?(rec.mobilebrand) && rec.mobilebrand != '') ? rec.mobilebrand : FIELD_NOT_SUPPORTED
90
+ elevation = (defined?(rec.elevation) && rec.elevation != '') ? rec.elevation : FIELD_NOT_SUPPORTED
91
+ usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
92
+ else
93
+ country_short = INVALID_IP_ADDRESS
94
+ country_long = INVALID_IP_ADDRESS
95
+ region = INVALID_IP_ADDRESS
96
+ city = INVALID_IP_ADDRESS
97
+ latitude = INVALID_IP_ADDRESS
98
+ longitude = INVALID_IP_ADDRESS
99
+ isp = INVALID_IP_ADDRESS
100
+ domain = INVALID_IP_ADDRESS
101
+ netspeed = INVALID_IP_ADDRESS
102
+ areacode = INVALID_IP_ADDRESS
103
+ iddcode = INVALID_IP_ADDRESS
104
+ timezone = INVALID_IP_ADDRESS
105
+ zipcode = INVALID_IP_ADDRESS
106
+ weatherstationname = INVALID_IP_ADDRESS
107
+ weatherstationcode = INVALID_IP_ADDRESS
108
+ mcc = INVALID_IP_ADDRESS
109
+ mnc = INVALID_IP_ADDRESS
110
+ mobilebrand = INVALID_IP_ADDRESS
111
+ elevation = INVALID_IP_ADDRESS
112
+ usagetype = INVALID_IP_ADDRESS
113
+ end
114
+ else
115
+ country_short = INVALID_IP_ADDRESS
116
+ country_long = INVALID_IP_ADDRESS
117
+ region = INVALID_IP_ADDRESS
118
+ city = INVALID_IP_ADDRESS
119
+ latitude = INVALID_IP_ADDRESS
120
+ longitude = INVALID_IP_ADDRESS
121
+ isp = INVALID_IP_ADDRESS
122
+ domain = INVALID_IP_ADDRESS
123
+ netspeed = INVALID_IP_ADDRESS
124
+ areacode = INVALID_IP_ADDRESS
125
+ iddcode = INVALID_IP_ADDRESS
126
+ timezone = INVALID_IP_ADDRESS
127
+ zipcode = INVALID_IP_ADDRESS
128
+ weatherstationname = INVALID_IP_ADDRESS
129
+ weatherstationcode = INVALID_IP_ADDRESS
130
+ mcc = INVALID_IP_ADDRESS
131
+ mnc = INVALID_IP_ADDRESS
132
+ mobilebrand = INVALID_IP_ADDRESS
133
+ elevation = INVALID_IP_ADDRESS
134
+ usagetype = INVALID_IP_ADDRESS
135
+ end
136
+ results = {}
137
+ results['country_short'] = country_short
138
+ results['country_long'] = country_long
139
+ results['region'] = region
140
+ results['city'] = city
141
+ results['latitude'] = latitude
142
+ results['longitude'] = longitude
143
+ results['isp'] = isp
144
+ results['domain'] = domain
145
+ results['netspeed'] = netspeed
146
+ results['areacode'] = areacode
147
+ results['iddcode'] = iddcode
148
+ results['timezone'] = timezone
149
+ results['zipcode'] = zipcode
150
+ results['weatherstationname'] = weatherstationname
151
+ results['weatherstationcode'] = weatherstationcode
152
+ results['mcc'] = mcc
153
+ results['mnc'] = mnc
154
+ results['mobilebrand'] = mobilebrand
155
+ results['elevation'] = elevation
156
+ results['usagetype'] = usagetype
157
+ return results
158
+ end
159
+
59
160
  def get_country_short(ip)
60
- rec = get_all(ip)
61
- return rec.country_short
161
+ valid = !(IPAddr.new(ip) rescue nil).nil?
162
+ if valid
163
+ rec = get_record(ip)
164
+ if !(rec.nil?)
165
+ country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
166
+ else
167
+ country_short = INVALID_IP_ADDRESS
168
+ end
169
+ else
170
+ country_short = INVALID_IP_ADDRESS
171
+ end
172
+ return country_short
62
173
  end
63
-
174
+
64
175
  def get_country_long(ip)
65
- rec = get_all(ip)
66
- return rec.country_long
176
+ valid = !(IPAddr.new(ip) rescue nil).nil?
177
+ if valid
178
+ rec = get_record(ip)
179
+ if !(rec.nil?)
180
+ country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
181
+ else
182
+ country_long = INVALID_IP_ADDRESS
183
+ end
184
+ else
185
+ country_long = INVALID_IP_ADDRESS
186
+ end
187
+ return country_long
67
188
  end
68
-
189
+
69
190
  def get_region(ip)
70
- rec = get_all(ip)
71
- return rec.region
191
+ valid = !(IPAddr.new(ip) rescue nil).nil?
192
+ if valid
193
+ rec = get_record(ip)
194
+ if !(rec.nil?)
195
+ region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
196
+ else
197
+ region = INVALID_IP_ADDRESS
198
+ end
199
+ else
200
+ region = INVALID_IP_ADDRESS
201
+ end
202
+ return region
72
203
  end
73
-
204
+
74
205
  def get_city(ip)
75
- rec = get_all(ip)
76
- return rec.city
206
+ valid = !(IPAddr.new(ip) rescue nil).nil?
207
+ if valid
208
+ rec = get_record(ip)
209
+ if !(rec.nil?)
210
+ city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
211
+ else
212
+ city = INVALID_IP_ADDRESS
213
+ end
214
+ else
215
+ city = INVALID_IP_ADDRESS
216
+ end
217
+ return city
77
218
  end
78
-
219
+
79
220
  def get_latitude(ip)
80
- rec = get_all(ip)
81
- return rec.latitude
221
+ valid = !(IPAddr.new(ip) rescue nil).nil?
222
+ if valid
223
+ rec = get_record(ip)
224
+ if !(rec.nil?)
225
+ latitude = (defined?(rec.latitude) && rec.latitude != '') ? rec.latitude : FIELD_NOT_SUPPORTED
226
+ else
227
+ latitude = INVALID_IP_ADDRESS
228
+ end
229
+ else
230
+ latitude = INVALID_IP_ADDRESS
231
+ end
232
+ return latitude
82
233
  end
83
-
234
+
84
235
  def get_longitude(ip)
85
- rec = get_all(ip)
86
- return rec.longitude
236
+ valid = !(IPAddr.new(ip) rescue nil).nil?
237
+ if valid
238
+ rec = get_record(ip)
239
+ if !(rec.nil?)
240
+ longitude = (defined?(rec.longitude) && rec.longitude != '') ? rec.longitude : FIELD_NOT_SUPPORTED
241
+ else
242
+ longitude = INVALID_IP_ADDRESS
243
+ end
244
+ else
245
+ longitude = INVALID_IP_ADDRESS
246
+ end
247
+ return longitude
87
248
  end
88
-
249
+
89
250
  def get_isp(ip)
90
- rec = get_all(ip)
91
- return rec.isp
251
+ valid = !(IPAddr.new(ip) rescue nil).nil?
252
+ if valid
253
+ rec = get_record(ip)
254
+ if !(rec.nil?)
255
+ isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
256
+ else
257
+ isp = INVALID_IP_ADDRESS
258
+ end
259
+ else
260
+ isp = INVALID_IP_ADDRESS
261
+ end
262
+ return isp
92
263
  end
93
-
264
+
94
265
  def get_domain(ip)
95
- rec = get_all(ip)
96
- return rec.domain
266
+ valid = !(IPAddr.new(ip) rescue nil).nil?
267
+ if valid
268
+ rec = get_record(ip)
269
+ if !(rec.nil?)
270
+ domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
271
+ else
272
+ domain = INVALID_IP_ADDRESS
273
+ end
274
+ else
275
+ domain = INVALID_IP_ADDRESS
276
+ end
277
+ return domain
97
278
  end
98
-
279
+
99
280
  def get_zipcode(ip)
100
- rec = get_all(ip)
101
- return rec.zipcode
281
+ valid = !(IPAddr.new(ip) rescue nil).nil?
282
+ if valid
283
+ rec = get_record(ip)
284
+ if !(rec.nil?)
285
+ zipcode = (defined?(rec.zipcode) && rec.zipcode != '') ? rec.zipcode : FIELD_NOT_SUPPORTED
286
+ else
287
+ zipcode = INVALID_IP_ADDRESS
288
+ end
289
+ else
290
+ zipcode = INVALID_IP_ADDRESS
291
+ end
292
+ return zipcode
102
293
  end
103
-
294
+
104
295
  def get_timezone(ip)
105
- rec = get_all(ip)
106
- return rec.timezone
296
+ valid = !(IPAddr.new(ip) rescue nil).nil?
297
+ if valid
298
+ rec = get_record(ip)
299
+ if !(rec.nil?)
300
+ timezone = (defined?(rec.timezone) && rec.timezone != '') ? rec.timezone : FIELD_NOT_SUPPORTED
301
+ else
302
+ timezone = INVALID_IP_ADDRESS
303
+ end
304
+ else
305
+ timezone = INVALID_IP_ADDRESS
306
+ end
307
+ return timezone
107
308
  end
108
-
309
+
109
310
  def get_netspeed(ip)
110
- rec = get_all(ip)
111
- return rec.netspeed
311
+ valid = !(IPAddr.new(ip) rescue nil).nil?
312
+ if valid
313
+ rec = get_record(ip)
314
+ if !(rec.nil?)
315
+ netspeed = (defined?(rec.netspeed) && rec.netspeed != '') ? rec.netspeed : FIELD_NOT_SUPPORTED
316
+ else
317
+ netspeed = INVALID_IP_ADDRESS
318
+ end
319
+ else
320
+ netspeed = INVALID_IP_ADDRESS
321
+ end
322
+ return netspeed
112
323
  end
113
-
324
+
114
325
  def get_iddcode(ip)
115
- rec = get_all(ip)
116
- return rec.iddcode
326
+ valid = !(IPAddr.new(ip) rescue nil).nil?
327
+ if valid
328
+ rec = get_record(ip)
329
+ if !(rec.nil?)
330
+ iddcode = (defined?(rec.iddcode) && rec.iddcode != '') ? rec.iddcode : FIELD_NOT_SUPPORTED
331
+ else
332
+ iddcode = INVALID_IP_ADDRESS
333
+ end
334
+ else
335
+ iddcode = INVALID_IP_ADDRESS
336
+ end
337
+ return iddcode
117
338
  end
118
-
339
+
119
340
  def get_areacode(ip)
120
- rec = get_all(ip)
121
- return rec.areacode
341
+ valid = !(IPAddr.new(ip) rescue nil).nil?
342
+ if valid
343
+ rec = get_record(ip)
344
+ if !(rec.nil?)
345
+ areacode = (defined?(rec.areacode) && rec.areacode != '') ? rec.areacode : FIELD_NOT_SUPPORTED
346
+ else
347
+ areacode = INVALID_IP_ADDRESS
348
+ end
349
+ else
350
+ areacode = INVALID_IP_ADDRESS
351
+ end
352
+ return areacode
122
353
  end
123
-
354
+
124
355
  def get_weatherstationcode(ip)
125
- rec = get_all(ip)
126
- return rec.weatherstationcode
356
+ valid = !(IPAddr.new(ip) rescue nil).nil?
357
+ if valid
358
+ rec = get_record(ip)
359
+ if !(rec.nil?)
360
+ weatherstationcode = (defined?(rec.weatherstationcode) && rec.weatherstationcode != '') ? rec.weatherstationcode : FIELD_NOT_SUPPORTED
361
+ else
362
+ weatherstationcode = INVALID_IP_ADDRESS
363
+ end
364
+ else
365
+ weatherstationcode = INVALID_IP_ADDRESS
366
+ end
367
+ return weatherstationcode
127
368
  end
128
-
369
+
129
370
  def get_weatherstationname(ip)
130
- rec = get_all(ip)
131
- return rec.weatherstationname
371
+ valid = !(IPAddr.new(ip) rescue nil).nil?
372
+ if valid
373
+ rec = get_record(ip)
374
+ if !(rec.nil?)
375
+ weatherstationname = (defined?(rec.weatherstationname) && rec.weatherstationname != '') ? rec.weatherstationname : FIELD_NOT_SUPPORTED
376
+ else
377
+ weatherstationname = INVALID_IP_ADDRESS
378
+ end
379
+ else
380
+ weatherstationname = INVALID_IP_ADDRESS
381
+ end
382
+ return weatherstationname
132
383
  end
133
-
384
+
134
385
  def get_mcc(ip)
135
- rec = get_all(ip)
136
- return rec.mcc
386
+ valid = !(IPAddr.new(ip) rescue nil).nil?
387
+ if valid
388
+ rec = get_record(ip)
389
+ if !(rec.nil?)
390
+ mcc = (defined?(rec.mcc) && rec.mcc != '') ? rec.mcc : FIELD_NOT_SUPPORTED
391
+ else
392
+ mcc = INVALID_IP_ADDRESS
393
+ end
394
+ else
395
+ mcc = INVALID_IP_ADDRESS
396
+ end
397
+ return mcc
137
398
  end
138
-
399
+
139
400
  def get_mnc(ip)
140
- rec = get_all(ip)
141
- return rec.mnc
401
+ valid = !(IPAddr.new(ip) rescue nil).nil?
402
+ if valid
403
+ rec = get_record(ip)
404
+ if !(rec.nil?)
405
+ mnc = (defined?(mnc) && rec.mnc != '') ? rec.mnc : FIELD_NOT_SUPPORTED
406
+ else
407
+ mnc = INVALID_IP_ADDRESS
408
+ end
409
+ else
410
+ mnc = INVALID_IP_ADDRESS
411
+ end
412
+ return mnc
142
413
  end
143
-
414
+
144
415
  def get_mobilebrand(ip)
145
- rec = get_all(ip)
146
- return rec.mobilebrand
416
+ valid = !(IPAddr.new(ip) rescue nil).nil?
417
+ if valid
418
+ rec = get_record(ip)
419
+ if !(rec.nil?)
420
+ mobilebrand = (defined?(rec.mobilebrand) && rec.mobilebrand != '') ? rec.mobilebrand : FIELD_NOT_SUPPORTED
421
+ else
422
+ mobilebrand = INVALID_IP_ADDRESS
423
+ end
424
+ else
425
+ mobilebrand = INVALID_IP_ADDRESS
426
+ end
427
+ return mobilebrand
147
428
  end
148
-
429
+
149
430
  def get_elevation(ip)
150
- rec = get_all(ip)
151
- return rec.elevation
431
+ valid = !(IPAddr.new(ip) rescue nil).nil?
432
+ if valid
433
+ rec = get_record(ip)
434
+ if !(rec.nil?)
435
+ elevation = (defined?(rec.elevation) && rec.elevation != '') ? rec.elevation : FIELD_NOT_SUPPORTED
436
+ else
437
+ elevation = INVALID_IP_ADDRESS
438
+ end
439
+ else
440
+ elevation = INVALID_IP_ADDRESS
441
+ end
442
+ return elevation
152
443
  end
153
-
444
+
154
445
  def get_usagetype(ip)
155
- rec = get_all(ip)
156
- return rec.usagetype
446
+ valid = !(IPAddr.new(ip) rescue nil).nil?
447
+ if valid
448
+ rec = get_record(ip)
449
+ if !(rec.nil?)
450
+ usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
451
+ else
452
+ usagetype = INVALID_IP_ADDRESS
453
+ end
454
+ else
455
+ usagetype = INVALID_IP_ADDRESS
456
+ end
457
+ return usagetype
157
458
  end
158
-
459
+
159
460
  def bsearch(low, high, ipnum, base_addr, col_length)
160
461
  while low <= high do
161
- mid = (low + high) >> 1
162
- ip_from, ip_to = get_from_to(mid, base_addr, col_length)
163
- if ipnum >= ip_from && ipnum < ip_to
164
- from_base = ( base_addr + mid * (col_length + (self.v4 ? 0 : 12)))
165
- file.seek(from_base)
166
- if v4
167
- return self.record_class4.read(file)
168
- else
169
- return self.record_class6.read(file)
170
- end
171
- else
172
- if ipnum < ip_from
173
- high = mid - 1
174
- else
175
- low = mid + 1
176
- end
177
- end
178
- end
179
- end
180
-
462
+ mid = (low + high) >> 1
463
+ ip_from, ip_to = get_from_to(mid, base_addr, col_length)
464
+ if ipnum >= ip_from && ipnum < ip_to
465
+ from_base = ( base_addr + mid * (col_length + (self.v4 ? 0 : 12)))
466
+ file.seek(from_base)
467
+ if v4
468
+ return self.record_class4.read(file)
469
+ else
470
+ return self.record_class6.read(file)
471
+ end
472
+ else
473
+ if ipnum < ip_from
474
+ high = mid - 1
475
+ else
476
+ low = mid + 1
477
+ end
478
+ end
479
+ end
480
+ end
481
+
181
482
  def get_from_to(mid, base_addr, col_length)
182
483
  from_base = ( base_addr + mid * (col_length + (v4 ? 0 : 12)))
183
484
  file.seek(from_base)
184
- ip_from = v4 ? file.read(4).unpack('V').first : readipv6(file)
485
+ ip_from = v4 ? file.read(4).unpack('V').first : readipv6(file)
185
486
  file.seek(from_base + col_length + (v4 ? 0 : 12))
186
- ip_to = v4 ? file.read(4).unpack('V').first : readipv6(file)
487
+ ip_to = v4 ? file.read(4).unpack('V').first : readipv6(file)
187
488
  [ip_from, ip_to]
188
489
  end
189
-
490
+
491
+ def validateip(ip)
492
+ if ip.ipv4?
493
+ ipv = 4
494
+ ipnum = ip.to_i + 0
495
+ else
496
+ ipv = 6
497
+ ipnum = ip.to_i + 0
498
+ #reformat ipv4 address in ipv6
499
+ if ipnum >= 281470681743360 && ipnum <= 281474976710655
500
+ ipv = 4
501
+ ipnum = ipnum - 281470681743360
502
+ end
503
+ #reformat 6to4 address to ipv4 address 2002:: to 2002:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
504
+ if ipnum >= 42545680458834377588178886921629466624 && ipnum <= 42550872755692912415807417417958686719
505
+ ipv = 4
506
+ #bitshift right 80 bits
507
+ ipnum = ipnum >> 80
508
+ #bitwise modulus to get the last 32 bit
509
+ ipnum = ipnum % 4294967296
510
+ end
511
+ #reformat Teredo address to ipv4 address 2001:0000:: to 2001:0000:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:
512
+ if ipnum >= 42540488161975842760550356425300246528 && ipnum <= 42540488241204005274814694018844196863
513
+ ipv = 4
514
+ #bitwise not to invert binary
515
+ ipnum = ~ipnum
516
+ #bitwise modulus to get the last 32 bit
517
+ ipnum = ipnum % 4294967296
518
+ end
519
+ end
520
+ [ipv, ipnum]
521
+ end
522
+
190
523
  def read32(indexp)
191
- file.seek(indexp - 1)
192
- return file.read(4).unpack('V').first
524
+ file.seek(indexp - 1)
525
+ return file.read(4).unpack('V').first
193
526
  end
194
-
527
+
195
528
  def readipv6(filer)
196
- parts = filer.read(16).unpack('V*')
197
- return parts[0] + parts[1] * 4294967296 + parts[2] * 4294967296**2 + parts[3] * 4294967296**3
529
+ parts = filer.read(16).unpack('V*')
530
+ return parts[0] + parts[1] * 4294967296 + parts[2] * 4294967296**2 + parts[3] * 4294967296**3
198
531
  end
199
-
532
+
533
+ private :get_record, :bsearch, :get_from_to, :read32, :readipv6
534
+
200
535
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ip2location_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.0.3
4
+ version: 8.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - Vincent.Z
7
+ - ip2location
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-25 00:00:00.000000000 Z
11
+ date: 2019-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bindata
@@ -127,6 +127,7 @@ files:
127
127
  - README.md
128
128
  - Rakefile
129
129
  - VERSION
130
+ - example.rb
130
131
  - ip2location_ruby.gemspec
131
132
  - lib/ip2location_ruby.rb
132
133
  - lib/ip2location_ruby/database_config.rb
@@ -135,6 +136,7 @@ files:
135
136
  - lib/ip2location_ruby/i2l_string_data.rb
136
137
  - lib/ip2location_ruby/ip2location_config.rb
137
138
  - lib/ip2location_ruby/ip2location_record.rb
139
+ - rb/data/IP2LOCATION-LITE-DB1.IPV6.BIN
138
140
  - spec/assets/DB24.DEMO4.BIN
139
141
  - spec/assets/DB24.DEMO6.BIN
140
142
  - spec/assets/IP-COUNTRY-SAMPLE.BIN
@@ -163,8 +165,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
165
  - !ruby/object:Gem::Version
164
166
  version: '0'
165
167
  requirements: []
166
- rubyforge_project:
167
- rubygems_version: 2.7.7
168
+ rubygems_version: 3.0.3
168
169
  signing_key:
169
170
  specification_version: 4
170
171
  summary: the ip2location ruby library