Skip to content

Commit 1a996a4

Browse files
Added support for district, ASN and AS
1 parent ce8d3b2 commit 1a996a4

File tree

8 files changed

+140
-50
lines changed

8 files changed

+140
-50
lines changed

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2022 IP2Location ( support@ip2location.com )
1+
Copyright (c) 2023 IP2Location ( support@ip2location.com )
22

33
Permission is hereby granted, free of charge, to any person obtaining
44
a copy of this software and associated documentation files (the

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8.6.0
1+
8.7.0

example.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
puts 'Usage Type: ' + record['usagetype']
3030
puts 'Address Type: ' + record['addresstype']
3131
puts 'Category: ' + record['category']
32+
puts 'District: ' + record['district']
33+
puts 'ASN: ' + record['asn']
34+
puts 'AS: ' + record['as']
3235
i2l.close()
3336

3437
# Web Service
@@ -58,4 +61,4 @@
5861

5962
# Region Class
6063
region = Ip2locationRegion.new('./data/IP2LOCATION-ISO3166-2.CSV')
61-
puts region.get_region_code('US', 'California')
64+
puts region.get_region_code('US', 'California')

ip2location_ruby.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
Gem::Specification.new do |s|
22
s.name = "ip2location_ruby"
3-
s.version = "8.6.0"
3+
s.version = "8.7.0"
44

55
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
66
s.require_paths = ["lib"]
77
s.authors = ["ip2location"]
88
s.email = ["support@ip2location.com"]
9-
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."
9+
s.description = "The official IP2Location Ruby library to geolocate an IP address. You can lookup for country, region, district, city, latitude and longitude, ZIP/Postal code, time zone, Internet Service Provider (ISP) or company name, domain name, net speed, area code, weather station code, weather station name, mobile country code (MCC), mobile network code (MNC) and carrier brand, elevation, usage type, address type, IAB category and ASN from an IP address. Supported both IPv4 and IPv6 lookup."
1010
s.email = "support@ip2location.com"
1111
s.extra_rdoc_files = [
1212
"LICENSE.txt",

lib/ip2location_ruby.rb

Lines changed: 90 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class Ip2location
1515
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, :databaseyear, :databasemonth, :databaseday, :last_err_msg
1616

17-
VERSION = '8.6.0'
17+
VERSION = '8.7.0'
1818
FIELD_NOT_SUPPORTED = 'NOT SUPPORTED'
1919
INVALID_IP_ADDRESS = 'INVALID IP ADDRESS'
2020
INVALID_BIN_DATABASE = 'Incorrect IP2Location BIN file format. Please make sure that you are using the latest IP2Location BIN file.'
@@ -119,7 +119,7 @@ def get_all(ip)
119119
valid = !(IPAddr.new(ip) rescue nil).nil?
120120
if valid
121121
rec = get_record(ip)
122-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
122+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
123123
country_short = IPV6_ADDRESS_IN_IPV4_BIN
124124
country_long = IPV6_ADDRESS_IN_IPV4_BIN
125125
region = IPV6_ADDRESS_IN_IPV4_BIN
@@ -142,6 +142,9 @@ def get_all(ip)
142142
usagetype = IPV6_ADDRESS_IN_IPV4_BIN
143143
addresstype = IPV6_ADDRESS_IN_IPV4_BIN
144144
category = IPV6_ADDRESS_IN_IPV4_BIN
145+
district = IPV6_ADDRESS_IN_IPV4_BIN
146+
asn = IPV6_ADDRESS_IN_IPV4_BIN
147+
as = IPV6_ADDRESS_IN_IPV4_BIN
145148
elsif !(rec.nil?)
146149
country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
147150
country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
@@ -165,6 +168,9 @@ def get_all(ip)
165168
usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
166169
addresstype = (defined?(rec.addresstype) && rec.addresstype != '') ? rec.addresstype : FIELD_NOT_SUPPORTED
167170
category = (defined?(rec.category) && rec.category != '') ? rec.category : FIELD_NOT_SUPPORTED
171+
district = (defined?(rec.district) && rec.district != '') ? rec.district : FIELD_NOT_SUPPORTED
172+
asn = (defined?(rec.asn) && rec.asn != '') ? rec.asn : FIELD_NOT_SUPPORTED
173+
as = (defined?(rec.as) && rec.as != '') ? rec.as : FIELD_NOT_SUPPORTED
168174
else
169175
country_short = INVALID_IP_ADDRESS
170176
country_long = INVALID_IP_ADDRESS
@@ -188,6 +194,9 @@ def get_all(ip)
188194
usagetype = INVALID_IP_ADDRESS
189195
addresstype = INVALID_IP_ADDRESS
190196
category = INVALID_IP_ADDRESS
197+
district = INVALID_IP_ADDRESS
198+
asn = INVALID_IP_ADDRESS
199+
as = INVALID_IP_ADDRESS
191200
end
192201
else
193202
country_short = INVALID_IP_ADDRESS
@@ -212,6 +221,9 @@ def get_all(ip)
212221
usagetype = INVALID_IP_ADDRESS
213222
addresstype = INVALID_IP_ADDRESS
214223
category = INVALID_IP_ADDRESS
224+
district = INVALID_IP_ADDRESS
225+
asn = INVALID_IP_ADDRESS
226+
as = INVALID_IP_ADDRESS
215227
end
216228
results = {}
217229
results['country_short'] = country_short
@@ -236,14 +248,17 @@ def get_all(ip)
236248
results['usagetype'] = usagetype
237249
results['addresstype'] = addresstype
238250
results['category'] = category
251+
results['district'] = district
252+
results['asn'] = asn
253+
results['as'] = as
239254
return results
240255
end
241256

242257
def get_country_short(ip)
243258
valid = !(IPAddr.new(ip) rescue nil).nil?
244259
if valid
245260
rec = get_record(ip)
246-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
261+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
247262
country_short = IPV6_ADDRESS_IN_IPV4_BIN
248263
elsif !(rec.nil?)
249264
country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
@@ -260,7 +275,7 @@ def get_country_long(ip)
260275
valid = !(IPAddr.new(ip) rescue nil).nil?
261276
if valid
262277
rec = get_record(ip)
263-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
278+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
264279
country_long = IPV6_ADDRESS_IN_IPV4_BIN
265280
elsif !(rec.nil?)
266281
country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
@@ -277,7 +292,7 @@ def get_region(ip)
277292
valid = !(IPAddr.new(ip) rescue nil).nil?
278293
if valid
279294
rec = get_record(ip)
280-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
295+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
281296
region = IPV6_ADDRESS_IN_IPV4_BIN
282297
elsif !(rec.nil?)
283298
region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
@@ -294,7 +309,7 @@ def get_city(ip)
294309
valid = !(IPAddr.new(ip) rescue nil).nil?
295310
if valid
296311
rec = get_record(ip)
297-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
312+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
298313
city = IPV6_ADDRESS_IN_IPV4_BIN
299314
elsif !(rec.nil?)
300315
city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
@@ -311,7 +326,7 @@ def get_latitude(ip)
311326
valid = !(IPAddr.new(ip) rescue nil).nil?
312327
if valid
313328
rec = get_record(ip)
314-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
329+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
315330
latitude = IPV6_ADDRESS_IN_IPV4_BIN
316331
elsif !(rec.nil?)
317332
latitude = (defined?(rec.latitude) && rec.latitude != '') ? rec.latitude : FIELD_NOT_SUPPORTED
@@ -328,7 +343,7 @@ def get_longitude(ip)
328343
valid = !(IPAddr.new(ip) rescue nil).nil?
329344
if valid
330345
rec = get_record(ip)
331-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
346+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
332347
longitude = IPV6_ADDRESS_IN_IPV4_BIN
333348
elsif !(rec.nil?)
334349
longitude = (defined?(rec.longitude) && rec.longitude != '') ? rec.longitude : FIELD_NOT_SUPPORTED
@@ -345,7 +360,7 @@ def get_isp(ip)
345360
valid = !(IPAddr.new(ip) rescue nil).nil?
346361
if valid
347362
rec = get_record(ip)
348-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
363+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
349364
isp = IPV6_ADDRESS_IN_IPV4_BIN
350365
elsif !(rec.nil?)
351366
isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
@@ -362,7 +377,7 @@ def get_domain(ip)
362377
valid = !(IPAddr.new(ip) rescue nil).nil?
363378
if valid
364379
rec = get_record(ip)
365-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
380+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
366381
domain = IPV6_ADDRESS_IN_IPV4_BIN
367382
elsif !(rec.nil?)
368383
domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
@@ -379,7 +394,7 @@ def get_zipcode(ip)
379394
valid = !(IPAddr.new(ip) rescue nil).nil?
380395
if valid
381396
rec = get_record(ip)
382-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
397+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
383398
zipcode = IPV6_ADDRESS_IN_IPV4_BIN
384399
elsif !(rec.nil?)
385400
zipcode = (defined?(rec.zipcode) && rec.zipcode != '') ? rec.zipcode : FIELD_NOT_SUPPORTED
@@ -396,7 +411,7 @@ def get_timezone(ip)
396411
valid = !(IPAddr.new(ip) rescue nil).nil?
397412
if valid
398413
rec = get_record(ip)
399-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
414+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
400415
timezone = IPV6_ADDRESS_IN_IPV4_BIN
401416
elsif !(rec.nil?)
402417
timezone = (defined?(rec.timezone) && rec.timezone != '') ? rec.timezone : FIELD_NOT_SUPPORTED
@@ -413,7 +428,7 @@ def get_netspeed(ip)
413428
valid = !(IPAddr.new(ip) rescue nil).nil?
414429
if valid
415430
rec = get_record(ip)
416-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
431+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
417432
netspeed = IPV6_ADDRESS_IN_IPV4_BIN
418433
elsif !(rec.nil?)
419434
netspeed = (defined?(rec.netspeed) && rec.netspeed != '') ? rec.netspeed : FIELD_NOT_SUPPORTED
@@ -430,7 +445,7 @@ def get_iddcode(ip)
430445
valid = !(IPAddr.new(ip) rescue nil).nil?
431446
if valid
432447
rec = get_record(ip)
433-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
448+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
434449
iddcode = IPV6_ADDRESS_IN_IPV4_BIN
435450
elsif !(rec.nil?)
436451
iddcode = (defined?(rec.iddcode) && rec.iddcode != '') ? rec.iddcode : FIELD_NOT_SUPPORTED
@@ -447,7 +462,7 @@ def get_areacode(ip)
447462
valid = !(IPAddr.new(ip) rescue nil).nil?
448463
if valid
449464
rec = get_record(ip)
450-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
465+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
451466
areacode = IPV6_ADDRESS_IN_IPV4_BIN
452467
elsif !(rec.nil?)
453468
areacode = (defined?(rec.areacode) && rec.areacode != '') ? rec.areacode : FIELD_NOT_SUPPORTED
@@ -464,7 +479,7 @@ def get_weatherstationcode(ip)
464479
valid = !(IPAddr.new(ip) rescue nil).nil?
465480
if valid
466481
rec = get_record(ip)
467-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
482+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
468483
weatherstationcode = IPV6_ADDRESS_IN_IPV4_BIN
469484
elsif !(rec.nil?)
470485
weatherstationcode = (defined?(rec.weatherstationcode) && rec.weatherstationcode != '') ? rec.weatherstationcode : FIELD_NOT_SUPPORTED
@@ -481,7 +496,7 @@ def get_weatherstationname(ip)
481496
valid = !(IPAddr.new(ip) rescue nil).nil?
482497
if valid
483498
rec = get_record(ip)
484-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
499+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
485500
weatherstationname = IPV6_ADDRESS_IN_IPV4_BIN
486501
elsif !(rec.nil?)
487502
weatherstationname = (defined?(rec.weatherstationname) && rec.weatherstationname != '') ? rec.weatherstationname : FIELD_NOT_SUPPORTED
@@ -498,7 +513,7 @@ def get_mcc(ip)
498513
valid = !(IPAddr.new(ip) rescue nil).nil?
499514
if valid
500515
rec = get_record(ip)
501-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
516+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
502517
mcc = IPV6_ADDRESS_IN_IPV4_BIN
503518
elsif !(rec.nil?)
504519
mcc = (defined?(rec.mcc) && rec.mcc != '') ? rec.mcc : FIELD_NOT_SUPPORTED
@@ -515,7 +530,7 @@ def get_mnc(ip)
515530
valid = !(IPAddr.new(ip) rescue nil).nil?
516531
if valid
517532
rec = get_record(ip)
518-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
533+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
519534
mnc = IPV6_ADDRESS_IN_IPV4_BIN
520535
elsif !(rec.nil?)
521536
mnc = (defined?(rec.mnc) && rec.mnc != '') ? rec.mnc : FIELD_NOT_SUPPORTED
@@ -532,7 +547,7 @@ def get_mobilebrand(ip)
532547
valid = !(IPAddr.new(ip) rescue nil).nil?
533548
if valid
534549
rec = get_record(ip)
535-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
550+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
536551
mobilebrand = IPV6_ADDRESS_IN_IPV4_BIN
537552
elsif !(rec.nil?)
538553
mobilebrand = (defined?(rec.mobilebrand) && rec.mobilebrand != '') ? rec.mobilebrand : FIELD_NOT_SUPPORTED
@@ -549,7 +564,7 @@ def get_elevation(ip)
549564
valid = !(IPAddr.new(ip) rescue nil).nil?
550565
if valid
551566
rec = get_record(ip)
552-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
567+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
553568
elevation = IPV6_ADDRESS_IN_IPV4_BIN
554569
elsif !(rec.nil?)
555570
elevation = (defined?(rec.elevation) && rec.elevation != '') ? rec.elevation : FIELD_NOT_SUPPORTED
@@ -566,7 +581,7 @@ def get_usagetype(ip)
566581
valid = !(IPAddr.new(ip) rescue nil).nil?
567582
if valid
568583
rec = get_record(ip)
569-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
584+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
570585
usagetype = IPV6_ADDRESS_IN_IPV4_BIN
571586
elsif !(rec.nil?)
572587
usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
@@ -583,7 +598,7 @@ def get_addresstype(ip)
583598
valid = !(IPAddr.new(ip) rescue nil).nil?
584599
if valid
585600
rec = get_record(ip)
586-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
601+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
587602
addresstype = IPV6_ADDRESS_IN_IPV4_BIN
588603
elsif !(rec.nil?)
589604
addresstype = (defined?(rec.addresstype) && rec.addresstype != '') ? rec.addresstype : FIELD_NOT_SUPPORTED
@@ -600,7 +615,7 @@ def get_category(ip)
600615
valid = !(IPAddr.new(ip) rescue nil).nil?
601616
if valid
602617
rec = get_record(ip)
603-
if rec == IPV6_ADDRESS_IN_IPV4_BIN
618+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
604619
category = IPV6_ADDRESS_IN_IPV4_BIN
605620
elsif !(rec.nil?)
606621
category = (defined?(rec.category) && rec.category != '') ? rec.category : FIELD_NOT_SUPPORTED
@@ -613,6 +628,57 @@ def get_category(ip)
613628
return category
614629
end
615630

631+
def get_district(ip)
632+
valid = !(IPAddr.new(ip) rescue nil).nil?
633+
if valid
634+
rec = get_record(ip)
635+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
636+
district = IPV6_ADDRESS_IN_IPV4_BIN
637+
elsif !(rec.nil?)
638+
district = (defined?(rec.district) && rec.district != '') ? rec.district : FIELD_NOT_SUPPORTED
639+
else
640+
district = INVALID_IP_ADDRESS
641+
end
642+
else
643+
district = INVALID_IP_ADDRESS
644+
end
645+
return district
646+
end
647+
648+
def get_asn(ip)
649+
valid = !(IPAddr.new(ip) rescue nil).nil?
650+
if valid
651+
rec = get_record(ip)
652+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
653+
asn = IPV6_ADDRESS_IN_IPV4_BIN
654+
elsif !(rec.nil?)
655+
asn = (defined?(rec.asn) && rec.asn != '') ? rec.asn : FIELD_NOT_SUPPORTED
656+
else
657+
asn = INVALID_IP_ADDRESS
658+
end
659+
else
660+
asn = INVALID_IP_ADDRESS
661+
end
662+
return asn
663+
end
664+
665+
def get_as(ip)
666+
valid = !(IPAddr.new(ip) rescue nil).nil?
667+
if valid
668+
rec = get_record(ip)
669+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
670+
as = IPV6_ADDRESS_IN_IPV4_BIN
671+
elsif !(rec.nil?)
672+
as = (defined?(rec.as) && rec.as != '') ? rec.as : FIELD_NOT_SUPPORTED
673+
else
674+
as = INVALID_IP_ADDRESS
675+
end
676+
else
677+
as = INVALID_IP_ADDRESS
678+
end
679+
return as
680+
end
681+
616682
def bsearch(low, high, ipnum, base_addr, col_length)
617683
while low <= high do
618684
mid = (low + high) >> 1

0 commit comments

Comments
 (0)