Skip to content

Commit c299e9b

Browse files
committed
Added support for AS domain, AS usage type and AS CIDR
1 parent af16a17 commit c299e9b

File tree

9 files changed

+126
-33
lines changed

9 files changed

+126
-33
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) 2023 - 2024 IP2Location ( support@ip2location.com )
1+
Copyright (c) 2023 - 2025 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.7.3
1+
8.8.0

docs/source/code.md

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,36 @@ Retrieve geolocation information for an IP address.
3838
3939
**RETURN FIELDS**
4040
41-
| Field Name | Description |
42-
| ---------------- | ------------------------------------------------------------ |
43-
| country_short | Two-character country code based on ISO 3166. |
44-
| country_long | Country name based on ISO 3166. |
45-
| region | Region or state name. |
46-
| city | City name. |
47-
| isp | Internet Service Provider or company\'s name. |
48-
| latitude | City latitude. Defaults to capital city latitude if city is unknown. |
49-
| longitude | City longitude. Defaults to capital city longitude if city is unknown. |
50-
| domain | Internet domain name associated with IP address range. |
51-
| zipcode | ZIP code or Postal code. [172 countries supported](https://www.ip2location.com/zip-code-coverage). |
52-
| timezone | UTC time zone (with DST supported). |
53-
| netspeed | Internet connection type. |
54-
| idd_code | The IDD prefix to call the city from another country. |
55-
| area_code | A varying length number assigned to geographic areas for calls between cities. [223 countries supported](https://www.ip2location.com/area-code-coverage). |
56-
| weather_code | The special code to identify the nearest weather observation station. |
57-
| weather_name | The name of the nearest weather observation station. |
58-
| mcc | Mobile Country Codes (MCC) as defined in ITU E.212 for use in identifying mobile stations in wireless telephone networks, particularly GSM and UMTS networks. |
59-
| mnc | Mobile Network Code (MNC) is used in combination with a Mobile Country Code(MCC) to uniquely identify a mobile phone operator or carrier. |
60-
| mobile_brand | Commercial brand associated with the mobile carrier. You may click [mobile carrier coverage](https://www.ip2location.com/mobile-carrier-coverage) to view the coverage report. |
61-
| elevation | Average height of city above sea level in meters (m). |
62-
| usage_type | Usage type classification of ISP or company. |
63-
| address_type | IP address types as defined in Internet Protocol version 4 (IPv4) and Internet Protocol version 6 (IPv6). |
64-
| category | The domain category based on [IAB Tech Lab Content Taxonomy](https://www.ip2location.com/free/iab-categories). |
65-
| district | District or county name. |
66-
| asn | Autonomous system number (ASN). BIN databases. |
67-
| as_name | Autonomous system (AS) name. |
41+
| Field Name | Description |
42+
| ---------------- | ------------------------------------------------------------ |
43+
| country_short | Two-character country code based on ISO 3166. |
44+
| country_long | Country name based on ISO 3166. |
45+
| region | Region or state name. |
46+
| city | City name. |
47+
| isp | Internet Service Provider or company\'s name. |
48+
| latitude | City latitude. Defaults to capital city latitude if city is unknown. |
49+
| longitude | City longitude. Defaults to capital city longitude if city is unknown. |
50+
| domain | Internet domain name associated with IP address range. |
51+
| zipcode | ZIP code or Postal code. [172 countries supported](https://www.ip2location.com/zip-code-coverage). |
52+
| timezone | UTC time zone (with DST supported). |
53+
| netspeed | Internet connection type. |
54+
| iddcode | The IDD prefix to call the city from another country. |
55+
| areacode | A varying length number assigned to geographic areas for calls between cities. [223 countries supported](https://www.ip2location.com/area-code-coverage). |
56+
| weatherstationcode | The special code to identify the nearest weather observation station. |
57+
| weatherstationname | The name of the nearest weather observation station. |
58+
| mcc | Mobile Country Codes (MCC) as defined in ITU E.212 for use in identifying mobile stations in wireless telephone networks, particularly GSM and UMTS networks. |
59+
| mnc | Mobile Network Code (MNC) is used in combination with a Mobile Country Code(MCC) to uniquely identify a mobile phone operator or carrier. |
60+
| mobilebrand | Commercial brand associated with the mobile carrier. You may click [mobile carrier coverage](https://www.ip2location.com/mobile-carrier-coverage) to view the coverage report. |
61+
| elevation | Average height of city above sea level in meters (m). |
62+
| usagetype | Usage type classification of ISP or company. |
63+
| addresstype | IP address types as defined in Internet Protocol version 4 (IPv4) and Internet Protocol version 6 (IPv6). |
64+
| category | The domain category based on [IAB Tech Lab Content Taxonomy](https://www.ip2location.com/free/iab-categories). |
65+
| district | District or county name. |
66+
| asn | Autonomous system number (ASN) BIN databases. |
67+
| as | Autonomous system (AS) name. |
68+
| as_domain | Domain name of the AS registrant. |
69+
| as_usagetype | Usage type of the AS registrant. |
70+
| as_cidr | CIDR range for the whole AS. |
6871
```
6972

7073
## Ip2locationIpTools Class

example.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
puts 'District: ' + record['district']
3333
puts 'ASN: ' + record['asn']
3434
puts 'AS: ' + record['as']
35+
puts 'AS Domain: ' + record['as_domain']
36+
puts 'AS Usage Type: ' + record['as_usagetype']
37+
puts 'AS CIDR: ' + record['as_cidr']
3538
i2l.close()
3639

3740
# IP Tools

ip2location_ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = "ip2location_ruby"
3-
s.version = "8.7.3"
3+
s.version = "8.8.0"
44

55
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
66
s.require_paths = ["lib"]

lib/ip2location_ruby.rb

Lines changed: 67 additions & 1 deletion
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, :record, :database, :columns, :ip_version, :ipv4databasecount, :ipv4databaseaddr, :ipv4indexbaseaddr, :ipv6databasecount, :ipv6databaseaddr, :ipv6indexbaseaddr, :databaseyear, :databasemonth, :databaseday, :last_err_msg
1616

17-
VERSION = '8.7.3'
17+
VERSION = '8.8.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.'
@@ -145,6 +145,9 @@ def get_all(ip)
145145
district = IPV6_ADDRESS_IN_IPV4_BIN
146146
asn = IPV6_ADDRESS_IN_IPV4_BIN
147147
as = IPV6_ADDRESS_IN_IPV4_BIN
148+
as_domain = IPV6_ADDRESS_IN_IPV4_BIN
149+
as_usagetype = IPV6_ADDRESS_IN_IPV4_BIN
150+
as_cidr = IPV6_ADDRESS_IN_IPV4_BIN
148151
elsif !(rec.nil?)
149152
country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
150153
country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
@@ -171,6 +174,9 @@ def get_all(ip)
171174
district = (defined?(rec.district) && rec.district != '') ? rec.district : FIELD_NOT_SUPPORTED
172175
asn = (defined?(rec.asn) && rec.asn != '') ? rec.asn : FIELD_NOT_SUPPORTED
173176
as = (defined?(rec.as) && rec.as != '') ? rec.as : FIELD_NOT_SUPPORTED
177+
as_domain = (defined?(rec.as_domain) && rec.as_domain != '') ? rec.as_domain : FIELD_NOT_SUPPORTED
178+
as_usagetype = (defined?(rec.as_usagetype) && rec.as_usagetype != '') ? rec.as_usagetype : FIELD_NOT_SUPPORTED
179+
as_cidr = (defined?(rec.as_cidr) && rec.as_cidr != '') ? rec.as_cidr : FIELD_NOT_SUPPORTED
174180
else
175181
country_short = INVALID_IP_ADDRESS
176182
country_long = INVALID_IP_ADDRESS
@@ -197,6 +203,9 @@ def get_all(ip)
197203
district = INVALID_IP_ADDRESS
198204
asn = INVALID_IP_ADDRESS
199205
as = INVALID_IP_ADDRESS
206+
as_domain = INVALID_IP_ADDRESS
207+
as_usagetype = INVALID_IP_ADDRESS
208+
as_cidr = INVALID_IP_ADDRESS
200209
end
201210
else
202211
country_short = INVALID_IP_ADDRESS
@@ -224,6 +233,9 @@ def get_all(ip)
224233
district = INVALID_IP_ADDRESS
225234
asn = INVALID_IP_ADDRESS
226235
as = INVALID_IP_ADDRESS
236+
as_domain = INVALID_IP_ADDRESS
237+
as_usagetype = INVALID_IP_ADDRESS
238+
as_cidr = INVALID_IP_ADDRESS
227239
end
228240
results = {}
229241
results['country_short'] = country_short
@@ -251,6 +263,9 @@ def get_all(ip)
251263
results['district'] = district
252264
results['asn'] = asn
253265
results['as'] = as
266+
results['as_domain'] = as_domain
267+
results['as_usagetype'] = as_usagetype
268+
results['as_cidr'] = as_cidr
254269
return results
255270
end
256271

@@ -679,6 +694,57 @@ def get_as(ip)
679694
return as
680695
end
681696

697+
def get_as_domain(ip)
698+
valid = !(IPAddr.new(ip) rescue nil).nil?
699+
if valid
700+
rec = get_record(ip)
701+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
702+
as_domain = IPV6_ADDRESS_IN_IPV4_BIN
703+
elsif !(rec.nil?)
704+
as_domain = (defined?(rec.as_domain) && rec.as_domain != '') ? rec.as_domain : FIELD_NOT_SUPPORTED
705+
else
706+
as_domain = INVALID_IP_ADDRESS
707+
end
708+
else
709+
as_domain = INVALID_IP_ADDRESS
710+
end
711+
return as_domain
712+
end
713+
714+
def get_as_usagetype(ip)
715+
valid = !(IPAddr.new(ip) rescue nil).nil?
716+
if valid
717+
rec = get_record(ip)
718+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
719+
as_usagetype = IPV6_ADDRESS_IN_IPV4_BIN
720+
elsif !(rec.nil?)
721+
as_usagetype = (defined?(rec.as_usagetype) && rec.as_usagetype != '') ? rec.as_usagetype : FIELD_NOT_SUPPORTED
722+
else
723+
as_usagetype = INVALID_IP_ADDRESS
724+
end
725+
else
726+
as_usagetype = INVALID_IP_ADDRESS
727+
end
728+
return as_usagetype
729+
end
730+
731+
def get_as_cidr(ip)
732+
valid = !(IPAddr.new(ip) rescue nil).nil?
733+
if valid
734+
rec = get_record(ip)
735+
if rec == IPV6_ADDRESS_IN_IPV4_BIN
736+
as_cidr = IPV6_ADDRESS_IN_IPV4_BIN
737+
elsif !(rec.nil?)
738+
as_cidr = (defined?(rec.as_cidr) && rec.as_cidr != '') ? rec.as_cidr : FIELD_NOT_SUPPORTED
739+
else
740+
as_cidr = INVALID_IP_ADDRESS
741+
end
742+
else
743+
as_cidr = INVALID_IP_ADDRESS
744+
end
745+
return as_cidr
746+
end
747+
682748
def bsearch(low, high, ipnum, base_addr, col_length)
683749
while low <= high do
684750
mid = (low + high) >> 1

lib/ip2location_ruby/database_config.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ class DbConfig
2323
:CATEGORY => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 22],
2424
:DISTRICT => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23],
2525
:ASN => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24],
26-
:AS => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25]
26+
:AS => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25],
27+
:AS_DOMAIN => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26],
28+
:AS_USAGETYPE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27],
29+
:AS_CIDR => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28]
2730
}
2831

2932
def self.setup_database(db_index)

spec/ip2location_ruby_database_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,24 @@
157157
expect(record).to eq 'NOT SUPPORTED'
158158
end
159159

160+
it "work correctly with get_as_domain" do
161+
i2l = Ip2location.new.open(File.dirname(__FILE__) + "/assets/IP2LOCATION-LITE-DB1.IPV6.BIN")
162+
record = i2l.get_as_domain('8.8.8.8')
163+
expect(record).to eq 'NOT SUPPORTED'
164+
end
165+
166+
it "work correctly with get_as_usagetype" do
167+
i2l = Ip2location.new.open(File.dirname(__FILE__) + "/assets/IP2LOCATION-LITE-DB1.IPV6.BIN")
168+
record = i2l.get_as_usagetype('8.8.8.8')
169+
expect(record).to eq 'NOT SUPPORTED'
170+
end
171+
172+
it "work correctly with get_as_cidr" do
173+
i2l = Ip2location.new.open(File.dirname(__FILE__) + "/assets/IP2LOCATION-LITE-DB1.IPV6.BIN")
174+
record = i2l.get_as_cidr('8.8.8.8')
175+
expect(record).to eq 'NOT SUPPORTED'
176+
end
177+
160178
it "work correctly with ipv6" do
161179
i2l = Ip2location.new.open(File.dirname(__FILE__) + "/assets/IP2LOCATION-LITE-DB1.IPV6.BIN")
162180
record = i2l.get_all('2001:4860:4860::8888')

spec/ip2location_ruby_iptools_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
iptool = Ip2locationIpTools.new()
7575
record = iptool.cidr_to_ipv6('2002::1234:abcd:ffff:c0a8:101/64')
7676
expect(record).to include(
77-
"ip_start"=>"2002:0000:0000:1234:abcd:ffff:c0a8:0101",
77+
"ip_start"=>"2002:0000:0000:1234:0000:0000:0000:0000",
7878
"ip_end"=>"2002:0000:0000:1234:ffff:ffff:ffff:ffff"
7979
)
8080
end

0 commit comments

Comments
 (0)