ip2location_ruby 8.5.0 → 8.7.0

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: f7f7cc867aa07b3595ae89b5f5fa55fab2b210236b106fa8ad10ccb5b9dd15b1
4
- data.tar.gz: 36b720b84b89cfaad323c1d695e1b5408eca69476197cdb7911cdb614856c5e9
3
+ metadata.gz: 07d9738099db961a6661f7266017f4f5cf8156cb0b0e2063bf1e2a246dfe39f4
4
+ data.tar.gz: 9f4e8c1f7c60a58687e8d922816e69e6ddbaa4f4bd06dd9b0938a194278f8188
5
5
  SHA512:
6
- metadata.gz: 7a84781d5ab63cee79729be08a0ebdc1f3866630b8f00e4aea983e654c2cf55fd66370d8006666c76880621231fd91d9ddf3f19c2efdd0d7b7fde9428f630935
7
- data.tar.gz: d6e40740c7fa84c5410239e93f54502e197a19dcc8c00b8a1d30bec76660a35bb9dfe78bced75ae7b4b3c15e29ad9f7e339a1b817e4b28cc4aff99caaf58f624
6
+ metadata.gz: 5417dcb43c59394ad2500f0e79ec109fc75bb70b0afca917c3150cd0ac6da7168e94b4381e01624fa8ee43ef05a9ed5bd148a4539c6d90ab23ed927cd2ff4cd2
7
+ data.tar.gz: 7a60ef9c3759e47b1c2699378103fbfbf3fde9e8007ecfee424739a9226cd0b9e24852e1e11b4f758f2fb67e9eeefc06845688da98d42043b02aaa4d9c8187c3
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2022 IP2Location ( support@ip2location.com )
1
+ Copyright (c) 2023 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
@@ -83,6 +83,22 @@ Below is the description of the functions available in the **IpTools** class.
83
83
  | compress_ipv6 | Compress a IPv6 to shorten the length. |
84
84
  | expand_ipv6 | Expand a shorten IPv6 to full length. |
85
85
 
86
+ ## Country
87
+ Below is the description of the functions available in the **Country** class.
88
+
89
+ | Function Name | Description |
90
+ |---|---|
91
+ | Constructor | Expect a IP2Location Country Information CSV file. This database is free for download at https://www.ip2location.com/free/country-information |
92
+ | get_country_info | Provide a ISO 3166 country code to get the country information in array. Will return a full list of countries information if country code not provided. Below is the information returned: <ul><li>country_code</li><li>country_alpha3_code</li><li>country_numeric_code</li><li>capital</li><li>country_demonym</li><li>total_area</li><li>population</li><li>idd_code</li><li>currency_code</li><li>currency_name</li><li>currency_symbol</li><li>lang_code</li><li>lang_name</li><li>cctld</li></ul> |
93
+
94
+ ## Region
95
+ Below is the description of the functions available in the **Region** class.
96
+
97
+ | Function Name | Description |
98
+ |---|---|
99
+ | Constructor | Expect a IP2Location ISO 3166-2 Subdivision Code CSV file. This database is free for download at https://www.ip2location.com/free/iso3166-2 |
100
+ | get_region_code | Provide a ISO 3166 country code and the region name to get ISO 3166-2 subdivision code for the region. |
101
+
86
102
  # Dependencies
87
103
  This library requires IP2Location BIN data file to function. You may download the BIN data file at
88
104
  * IP2Location LITE BIN Data (Free): https://lite.ip2location.com
data/VERSION CHANGED
@@ -1 +1 @@
1
- 8.5.0
1
+ 8.7.0
data/example.rb CHANGED
@@ -29,7 +29,9 @@ puts record['elevation']
29
29
  puts 'Usage Type: ' + record['usagetype']
30
30
  puts 'Address Type: ' + record['addresstype']
31
31
  puts 'Category: ' + record['category']
32
-
32
+ puts 'District: ' + record['district']
33
+ puts 'ASN: ' + record['asn']
34
+ puts 'AS: ' + record['as']
33
35
  i2l.close()
34
36
 
35
37
  # Web Service
@@ -52,3 +54,11 @@ puts iptool.ipv6_to_cidr('2002:0000:0000:1234:abcd:ffff:c0a8:0000', '2002:0000:0
52
54
  puts iptool.cidr_to_ipv6('2002::1234:abcd:ffff:c0a8:101/64')
53
55
  puts iptool.compress_ipv6('2002:0000:0000:1234:ffff:ffff:ffff:ffff')
54
56
  puts iptool.expand_ipv6('2002::1234:ffff:ffff:ffff:ffff')
57
+
58
+ # Country Class
59
+ country = Ip2locationCountry.new('./data/IP2LOCATION-COUNTRY-INFORMATION-BASIC.CSV')
60
+ puts country.get_country_info('US')
61
+
62
+ # Region Class
63
+ region = Ip2locationRegion.new('./data/IP2LOCATION-ISO3166-2.CSV')
64
+ puts region.get_region_code('US', 'California')
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "ip2location_ruby"
3
- s.version = "8.5.0"
3
+ s.version = "8.7.0"
4
4
 
5
5
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
6
  s.require_paths = ["lib"]
@@ -30,11 +30,15 @@ Gem::Specification.new do |s|
30
30
  "lib/ip2location_ruby/ip2location_config.rb",
31
31
  "lib/ip2location_ruby/ip2location_record.rb",
32
32
  "spec/assets/IP2LOCATION-LITE-DB1.IPV6.BIN",
33
+ "spec/ip2location_ruby_country_spec.rb",
33
34
  "spec/ip2location_ruby_database_spec.rb",
34
35
  "spec/ip2location_ruby_iptools_spec.rb",
36
+ "spec/ip2location_ruby_region_spec.rb",
35
37
  "spec/ip2location_ruby_webservice_spec.rb",
36
38
  "spec/spec_helper.rb",
37
- "rb/data/IP2LOCATION-LITE-DB1.IPV6.BIN"
39
+ "rb/data/IP2LOCATION-LITE-DB1.IPV6.BIN",
40
+ "rb/data/IP2LOCATION-COUNTRY-INFORMATION-BASIC.CSV",
41
+ "rb/data/IP2LOCATION-ISO3166-2.CSV"
38
42
  ]
39
43
  s.homepage = "https://github.com/ip2location/ip2location-ruby"
40
44
  s.licenses = ["MIT"]
@@ -1,26 +1,29 @@
1
1
  class DbConfig
2
2
  COLUMNS = {
3
- :COUNTRY => [0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
4
- :REGION => [0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
5
- :CITY => [0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
6
- :LATITUDE => [0, 0, 0, 0, 0, 5, 5, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
7
- :LONGITUDE => [0, 0, 0, 0, 0, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
8
- :ZIPCODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 7, 7, 7, 0, 7, 0, 7, 7, 7, 0, 7, 7],
9
- :TIMEZONE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 7, 8, 8, 8, 7, 8, 0, 8, 8, 8, 0, 8, 8],
10
- :ISP => [0, 0, 3, 0, 5, 0, 7, 5, 7, 0, 8, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 0, 9, 7, 9, 9],
11
- :DOMAIN => [0, 0, 0, 0, 0, 0, 0, 6, 8, 0, 9, 0, 10, 0, 10, 0, 10, 0, 10, 8, 10, 0, 10, 8, 10, 10],
12
- :NETSPEED => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 11, 0, 11,8, 11, 0, 11, 0, 11, 0, 11, 11],
13
- :IDDCODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 12, 0, 12, 0, 12, 9, 12, 0, 12, 12],
14
- :AREACODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10 ,13 ,0, 13, 0, 13, 10, 13, 0, 13, 13],
15
- :WEATHERSTATIONCODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 14, 0, 14, 0, 14, 0, 14, 14],
16
- :WEATHERSTATIONNAME => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 15, 0, 15, 0, 15, 0, 15, 15],
17
- :MCC => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 16, 0, 16, 9, 16, 16],
18
- :MNC => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10,17, 0, 17, 10, 17, 17],
19
- :MOBILEBRAND => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11,18, 0, 18, 11, 18, 18],
20
- :ELEVATION => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 19, 0, 19, 19],
21
- :USAGETYPE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 20, 20],
22
- :ADDRESSTYPE => [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, 21],
23
- :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]
3
+ :COUNTRY => [0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
4
+ :REGION => [0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
5
+ :CITY => [0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
6
+ :LATITUDE => [0, 0, 0, 0, 0, 5, 5, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
7
+ :LONGITUDE => [0, 0, 0, 0, 0, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
8
+ :ZIPCODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 7, 7, 7, 0, 7, 0, 7, 7, 7, 0, 7, 7, 7],
9
+ :TIMEZONE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 7, 8, 8, 8, 7, 8, 0, 8, 8, 8, 0, 8, 8, 8],
10
+ :ISP => [0, 0, 3, 0, 5, 0, 7, 5, 7, 0, 8, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 0, 9, 7, 9, 9, 9],
11
+ :DOMAIN => [0, 0, 0, 0, 0, 0, 0, 6, 8, 0, 9, 0, 10, 0, 10, 0, 10, 0, 10, 8, 10, 0, 10, 8, 10, 10, 10],
12
+ :NETSPEED => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 11, 0, 11,8, 11, 0, 11, 0, 11, 0, 11, 11, 11],
13
+ :IDDCODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 12, 0, 12, 0, 12, 9, 12, 0, 12, 12, 12],
14
+ :AREACODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10 ,13 ,0, 13, 0, 13, 10, 13, 0, 13, 13, 13],
15
+ :WEATHERSTATIONCODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 14, 0, 14, 0, 14, 0, 14, 14, 14],
16
+ :WEATHERSTATIONNAME => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 15, 0, 15, 0, 15, 0, 15, 15, 15],
17
+ :MCC => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 16, 0, 16, 9, 16, 16, 16],
18
+ :MNC => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10,17, 0, 17, 10, 17, 17, 17],
19
+ :MOBILEBRAND => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11,18, 0, 18, 11, 18, 18, 18],
20
+ :ELEVATION => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 19, 0, 19, 19, 19],
21
+ :USAGETYPE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 20, 20, 20],
22
+ :ADDRESSTYPE => [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, 21, 21],
23
+ :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],
24
+ :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],
25
+ :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]
24
27
  }
25
28
 
26
29
  def self.setup_database(db_index)
@@ -3,6 +3,7 @@ require 'bindata'
3
3
  require 'ipaddr'
4
4
  require 'net/http'
5
5
  require 'json'
6
+ require 'csv'
6
7
  require 'ip2location_ruby/ip2location_config'
7
8
  require 'ip2location_ruby/database_config'
8
9
  require 'ip2location_ruby/i2l_float_data'
@@ -13,7 +14,7 @@ require 'ip2location_ruby/ip2location_record'
13
14
  class Ip2location
14
15
  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
15
16
 
16
- VERSION = '8.5.0'
17
+ VERSION = '8.7.0'
17
18
  FIELD_NOT_SUPPORTED = 'NOT SUPPORTED'
18
19
  INVALID_IP_ADDRESS = 'INVALID IP ADDRESS'
19
20
  INVALID_BIN_DATABASE = 'Incorrect IP2Location BIN file format. Please make sure that you are using the latest IP2Location BIN file.'
@@ -118,7 +119,7 @@ class Ip2location
118
119
  valid = !(IPAddr.new(ip) rescue nil).nil?
119
120
  if valid
120
121
  rec = get_record(ip)
121
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
122
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
122
123
  country_short = IPV6_ADDRESS_IN_IPV4_BIN
123
124
  country_long = IPV6_ADDRESS_IN_IPV4_BIN
124
125
  region = IPV6_ADDRESS_IN_IPV4_BIN
@@ -141,6 +142,9 @@ class Ip2location
141
142
  usagetype = IPV6_ADDRESS_IN_IPV4_BIN
142
143
  addresstype = IPV6_ADDRESS_IN_IPV4_BIN
143
144
  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
144
148
  elsif !(rec.nil?)
145
149
  country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
146
150
  country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
@@ -164,6 +168,9 @@ class Ip2location
164
168
  usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
165
169
  addresstype = (defined?(rec.addresstype) && rec.addresstype != '') ? rec.addresstype : FIELD_NOT_SUPPORTED
166
170
  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
167
174
  else
168
175
  country_short = INVALID_IP_ADDRESS
169
176
  country_long = INVALID_IP_ADDRESS
@@ -187,6 +194,9 @@ class Ip2location
187
194
  usagetype = INVALID_IP_ADDRESS
188
195
  addresstype = INVALID_IP_ADDRESS
189
196
  category = INVALID_IP_ADDRESS
197
+ district = INVALID_IP_ADDRESS
198
+ asn = INVALID_IP_ADDRESS
199
+ as = INVALID_IP_ADDRESS
190
200
  end
191
201
  else
192
202
  country_short = INVALID_IP_ADDRESS
@@ -211,6 +221,9 @@ class Ip2location
211
221
  usagetype = INVALID_IP_ADDRESS
212
222
  addresstype = INVALID_IP_ADDRESS
213
223
  category = INVALID_IP_ADDRESS
224
+ district = INVALID_IP_ADDRESS
225
+ asn = INVALID_IP_ADDRESS
226
+ as = INVALID_IP_ADDRESS
214
227
  end
215
228
  results = {}
216
229
  results['country_short'] = country_short
@@ -235,6 +248,9 @@ class Ip2location
235
248
  results['usagetype'] = usagetype
236
249
  results['addresstype'] = addresstype
237
250
  results['category'] = category
251
+ results['district'] = district
252
+ results['asn'] = asn
253
+ results['as'] = as
238
254
  return results
239
255
  end
240
256
 
@@ -242,7 +258,7 @@ class Ip2location
242
258
  valid = !(IPAddr.new(ip) rescue nil).nil?
243
259
  if valid
244
260
  rec = get_record(ip)
245
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
261
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
246
262
  country_short = IPV6_ADDRESS_IN_IPV4_BIN
247
263
  elsif !(rec.nil?)
248
264
  country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
@@ -259,7 +275,7 @@ class Ip2location
259
275
  valid = !(IPAddr.new(ip) rescue nil).nil?
260
276
  if valid
261
277
  rec = get_record(ip)
262
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
278
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
263
279
  country_long = IPV6_ADDRESS_IN_IPV4_BIN
264
280
  elsif !(rec.nil?)
265
281
  country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
@@ -276,7 +292,7 @@ class Ip2location
276
292
  valid = !(IPAddr.new(ip) rescue nil).nil?
277
293
  if valid
278
294
  rec = get_record(ip)
279
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
295
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
280
296
  region = IPV6_ADDRESS_IN_IPV4_BIN
281
297
  elsif !(rec.nil?)
282
298
  region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
@@ -293,7 +309,7 @@ class Ip2location
293
309
  valid = !(IPAddr.new(ip) rescue nil).nil?
294
310
  if valid
295
311
  rec = get_record(ip)
296
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
312
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
297
313
  city = IPV6_ADDRESS_IN_IPV4_BIN
298
314
  elsif !(rec.nil?)
299
315
  city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
@@ -310,7 +326,7 @@ class Ip2location
310
326
  valid = !(IPAddr.new(ip) rescue nil).nil?
311
327
  if valid
312
328
  rec = get_record(ip)
313
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
329
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
314
330
  latitude = IPV6_ADDRESS_IN_IPV4_BIN
315
331
  elsif !(rec.nil?)
316
332
  latitude = (defined?(rec.latitude) && rec.latitude != '') ? rec.latitude : FIELD_NOT_SUPPORTED
@@ -327,7 +343,7 @@ class Ip2location
327
343
  valid = !(IPAddr.new(ip) rescue nil).nil?
328
344
  if valid
329
345
  rec = get_record(ip)
330
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
346
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
331
347
  longitude = IPV6_ADDRESS_IN_IPV4_BIN
332
348
  elsif !(rec.nil?)
333
349
  longitude = (defined?(rec.longitude) && rec.longitude != '') ? rec.longitude : FIELD_NOT_SUPPORTED
@@ -344,7 +360,7 @@ class Ip2location
344
360
  valid = !(IPAddr.new(ip) rescue nil).nil?
345
361
  if valid
346
362
  rec = get_record(ip)
347
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
363
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
348
364
  isp = IPV6_ADDRESS_IN_IPV4_BIN
349
365
  elsif !(rec.nil?)
350
366
  isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
@@ -361,7 +377,7 @@ class Ip2location
361
377
  valid = !(IPAddr.new(ip) rescue nil).nil?
362
378
  if valid
363
379
  rec = get_record(ip)
364
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
380
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
365
381
  domain = IPV6_ADDRESS_IN_IPV4_BIN
366
382
  elsif !(rec.nil?)
367
383
  domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
@@ -378,7 +394,7 @@ class Ip2location
378
394
  valid = !(IPAddr.new(ip) rescue nil).nil?
379
395
  if valid
380
396
  rec = get_record(ip)
381
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
397
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
382
398
  zipcode = IPV6_ADDRESS_IN_IPV4_BIN
383
399
  elsif !(rec.nil?)
384
400
  zipcode = (defined?(rec.zipcode) && rec.zipcode != '') ? rec.zipcode : FIELD_NOT_SUPPORTED
@@ -395,7 +411,7 @@ class Ip2location
395
411
  valid = !(IPAddr.new(ip) rescue nil).nil?
396
412
  if valid
397
413
  rec = get_record(ip)
398
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
414
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
399
415
  timezone = IPV6_ADDRESS_IN_IPV4_BIN
400
416
  elsif !(rec.nil?)
401
417
  timezone = (defined?(rec.timezone) && rec.timezone != '') ? rec.timezone : FIELD_NOT_SUPPORTED
@@ -412,7 +428,7 @@ class Ip2location
412
428
  valid = !(IPAddr.new(ip) rescue nil).nil?
413
429
  if valid
414
430
  rec = get_record(ip)
415
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
431
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
416
432
  netspeed = IPV6_ADDRESS_IN_IPV4_BIN
417
433
  elsif !(rec.nil?)
418
434
  netspeed = (defined?(rec.netspeed) && rec.netspeed != '') ? rec.netspeed : FIELD_NOT_SUPPORTED
@@ -429,7 +445,7 @@ class Ip2location
429
445
  valid = !(IPAddr.new(ip) rescue nil).nil?
430
446
  if valid
431
447
  rec = get_record(ip)
432
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
448
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
433
449
  iddcode = IPV6_ADDRESS_IN_IPV4_BIN
434
450
  elsif !(rec.nil?)
435
451
  iddcode = (defined?(rec.iddcode) && rec.iddcode != '') ? rec.iddcode : FIELD_NOT_SUPPORTED
@@ -446,7 +462,7 @@ class Ip2location
446
462
  valid = !(IPAddr.new(ip) rescue nil).nil?
447
463
  if valid
448
464
  rec = get_record(ip)
449
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
465
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
450
466
  areacode = IPV6_ADDRESS_IN_IPV4_BIN
451
467
  elsif !(rec.nil?)
452
468
  areacode = (defined?(rec.areacode) && rec.areacode != '') ? rec.areacode : FIELD_NOT_SUPPORTED
@@ -463,7 +479,7 @@ class Ip2location
463
479
  valid = !(IPAddr.new(ip) rescue nil).nil?
464
480
  if valid
465
481
  rec = get_record(ip)
466
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
482
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
467
483
  weatherstationcode = IPV6_ADDRESS_IN_IPV4_BIN
468
484
  elsif !(rec.nil?)
469
485
  weatherstationcode = (defined?(rec.weatherstationcode) && rec.weatherstationcode != '') ? rec.weatherstationcode : FIELD_NOT_SUPPORTED
@@ -480,7 +496,7 @@ class Ip2location
480
496
  valid = !(IPAddr.new(ip) rescue nil).nil?
481
497
  if valid
482
498
  rec = get_record(ip)
483
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
499
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
484
500
  weatherstationname = IPV6_ADDRESS_IN_IPV4_BIN
485
501
  elsif !(rec.nil?)
486
502
  weatherstationname = (defined?(rec.weatherstationname) && rec.weatherstationname != '') ? rec.weatherstationname : FIELD_NOT_SUPPORTED
@@ -497,7 +513,7 @@ class Ip2location
497
513
  valid = !(IPAddr.new(ip) rescue nil).nil?
498
514
  if valid
499
515
  rec = get_record(ip)
500
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
516
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
501
517
  mcc = IPV6_ADDRESS_IN_IPV4_BIN
502
518
  elsif !(rec.nil?)
503
519
  mcc = (defined?(rec.mcc) && rec.mcc != '') ? rec.mcc : FIELD_NOT_SUPPORTED
@@ -514,7 +530,7 @@ class Ip2location
514
530
  valid = !(IPAddr.new(ip) rescue nil).nil?
515
531
  if valid
516
532
  rec = get_record(ip)
517
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
533
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
518
534
  mnc = IPV6_ADDRESS_IN_IPV4_BIN
519
535
  elsif !(rec.nil?)
520
536
  mnc = (defined?(rec.mnc) && rec.mnc != '') ? rec.mnc : FIELD_NOT_SUPPORTED
@@ -531,7 +547,7 @@ class Ip2location
531
547
  valid = !(IPAddr.new(ip) rescue nil).nil?
532
548
  if valid
533
549
  rec = get_record(ip)
534
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
550
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
535
551
  mobilebrand = IPV6_ADDRESS_IN_IPV4_BIN
536
552
  elsif !(rec.nil?)
537
553
  mobilebrand = (defined?(rec.mobilebrand) && rec.mobilebrand != '') ? rec.mobilebrand : FIELD_NOT_SUPPORTED
@@ -548,7 +564,7 @@ class Ip2location
548
564
  valid = !(IPAddr.new(ip) rescue nil).nil?
549
565
  if valid
550
566
  rec = get_record(ip)
551
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
567
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
552
568
  elevation = IPV6_ADDRESS_IN_IPV4_BIN
553
569
  elsif !(rec.nil?)
554
570
  elevation = (defined?(rec.elevation) && rec.elevation != '') ? rec.elevation : FIELD_NOT_SUPPORTED
@@ -565,7 +581,7 @@ class Ip2location
565
581
  valid = !(IPAddr.new(ip) rescue nil).nil?
566
582
  if valid
567
583
  rec = get_record(ip)
568
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
584
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
569
585
  usagetype = IPV6_ADDRESS_IN_IPV4_BIN
570
586
  elsif !(rec.nil?)
571
587
  usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
@@ -582,7 +598,7 @@ class Ip2location
582
598
  valid = !(IPAddr.new(ip) rescue nil).nil?
583
599
  if valid
584
600
  rec = get_record(ip)
585
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
601
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
586
602
  addresstype = IPV6_ADDRESS_IN_IPV4_BIN
587
603
  elsif !(rec.nil?)
588
604
  addresstype = (defined?(rec.addresstype) && rec.addresstype != '') ? rec.addresstype : FIELD_NOT_SUPPORTED
@@ -599,7 +615,7 @@ class Ip2location
599
615
  valid = !(IPAddr.new(ip) rescue nil).nil?
600
616
  if valid
601
617
  rec = get_record(ip)
602
- if rec == IPV6_ADDRESS_IN_IPV4_BIN
618
+ if rec == IPV6_ADDRESS_IN_IPV4_BIN
603
619
  category = IPV6_ADDRESS_IN_IPV4_BIN
604
620
  elsif !(rec.nil?)
605
621
  category = (defined?(rec.category) && rec.category != '') ? rec.category : FIELD_NOT_SUPPORTED
@@ -612,6 +628,57 @@ class Ip2location
612
628
  return category
613
629
  end
614
630
 
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
+
615
682
  def bsearch(low, high, ipnum, base_addr, col_length)
616
683
  while low <= high do
617
684
  mid = (low + high) >> 1
@@ -964,4 +1031,127 @@ class Ip2locationIpTools
964
1031
  return
965
1032
  end
966
1033
  end
1034
+ end
1035
+
1036
+ class Ip2locationCountry
1037
+ attr_accessor :fields, :records
1038
+
1039
+ def initialize(csv)
1040
+ if csv == ''
1041
+ abort('The CSV file "' + csv + '" is not found.')
1042
+ end
1043
+
1044
+ begin
1045
+ csvfile = File.open(File.expand_path csv, 'rb')
1046
+ rescue
1047
+ abort('Error in opening ' + csv + '. No such CSV file in the /your_ip2location_ruby_library_path/rb/ folder.')
1048
+ else
1049
+ end
1050
+
1051
+ begin
1052
+ CSV.parse(csvfile)
1053
+ rescue
1054
+ abort('Unable to read "' + csv + '".')
1055
+ else
1056
+ line = 1
1057
+ self.records = Hash.new
1058
+ CSV.foreach((csvfile)) do |data|
1059
+ if line == 1
1060
+ if data[0] != 'country_code'
1061
+ abort('Invalid country information CSV file.')
1062
+ end
1063
+ self.fields = data
1064
+ else
1065
+ self.records[data[0]] = data
1066
+ end
1067
+ line = line + 1
1068
+ end
1069
+ end
1070
+ end
1071
+
1072
+ def get_country_info(country_code = nil)
1073
+ if self.records.empty?
1074
+ abort('No record available.')
1075
+ end
1076
+
1077
+ if country_code
1078
+ if (self.records[country_code]).nil?
1079
+ return []
1080
+ end
1081
+ results = Hash.new
1082
+ for i in 0..(self.fields.length()-1)
1083
+ results[self.fields[i]] = self.records[country_code][i]
1084
+ end
1085
+ return results
1086
+ end
1087
+
1088
+ results = []
1089
+ self.records.each do |key, value|
1090
+ data = Hash.new
1091
+ for i in 0..(self.fields.length()-1)
1092
+ data[self.fields[i]] = self.records[key][i]
1093
+ end
1094
+ results = results.append(data)
1095
+ end
1096
+ return results
1097
+ end
1098
+ end
1099
+
1100
+ class Ip2locationRegion
1101
+ attr_accessor :records
1102
+
1103
+ def initialize(csv)
1104
+ if csv == ''
1105
+ abort('The CSV file "' + csv + '" is not found.')
1106
+ end
1107
+
1108
+ begin
1109
+ csvfile = File.open(File.expand_path csv, 'rb')
1110
+ rescue
1111
+ abort('Error in opening ' + csv + '. No such CSV file in the /your_ip2location_ruby_library_path/rb/ folder.')
1112
+ else
1113
+ end
1114
+
1115
+ begin
1116
+ CSV.parse(csvfile)
1117
+ rescue
1118
+ abort('Unable to read "' + csv + '".')
1119
+ else
1120
+ line = 1
1121
+ self.records = Hash.new
1122
+ CSV.foreach((csvfile)) do |data|
1123
+ if line == 1
1124
+ if data[1] != 'subdivision_name'
1125
+ abort('Invalid region information CSV file.')
1126
+ end
1127
+ else
1128
+ temp_data = Hash.new
1129
+ temp_data['code'] = data[2]
1130
+ temp_data['name'] = data[1]
1131
+ if self.records[data[0]]
1132
+ self.records[data[0]].push temp_data
1133
+ else
1134
+ self.records[data[0]] = [temp_data]
1135
+ end
1136
+ end
1137
+ line = line + 1
1138
+ end
1139
+ end
1140
+ end
1141
+
1142
+ def get_region_code(country_code, region_name)
1143
+ if self.records.empty?
1144
+ abort('No record available.')
1145
+ end
1146
+
1147
+ if (self.records[country_code]).nil?
1148
+ return
1149
+ end
1150
+
1151
+ for i in 0..(self.records[country_code].length()-1)
1152
+ if region_name.upcase == self.records[country_code][i]["name"].upcase
1153
+ return self.records[country_code][i]["code"]
1154
+ end
1155
+ end
1156
+ end
967
1157
  end