ip2location_ruby 8.3.0 → 8.4.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: e745f1d50bac46096d52e3b092873502999f28659108f701221225a015038865
4
- data.tar.gz: 867a13f878805690873255278b61b03d554a143108fa099ccbbac3048808e0e0
3
+ metadata.gz: c6b5f833dfd8e52321b11114f24103b44ff2cc9279360a71a0948ecb6742d2d1
4
+ data.tar.gz: 9f2b62167a00f19ac4f342d222291c108a3204b4c99324e9c21770339a73444d
5
5
  SHA512:
6
- metadata.gz: 569481b686f7ba1895982a6ccb38f77550df6406fe1e4dba889e6a425f01bf5db22c67abe58ea4e67e9d49c69cb96d23ab347b25be65f254ba8c0aebca700b84
7
- data.tar.gz: 11721c250f7ed16ffb89c681678dc03a0eb39090857d85e38400e806399e9fae429738b686422208aa87ca81dcd134a049d35a83c5d1511158e7db46cec22bed
6
+ metadata.gz: 59be638fbbba78173c4fc3ba9bc708d4a43b21024cf5e675135533bbc10093c6a6707a15e108646e0d546b130e59a63e355c9d2c2ace995b0bd59c51484c454d
7
+ data.tar.gz: 283ac65d80c92f7bd054eb82399217901b08da84b965fa6764002baa5773073f130689b0646217b98ff62ebacf672e6990787a5d287075750eb05b9518f3892f
data/Gemfile CHANGED
@@ -10,5 +10,4 @@ group :development do
10
10
  gem "rspec", "~> 2.8.0"
11
11
  gem "rdoc", "~> 3.12"
12
12
  gem "bundler", ">= 1.2.0"
13
- gem "jeweler", "~> 1.8.4"
14
13
  end
data/Gemfile.lock CHANGED
@@ -1,50 +1,10 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- addressable (2.3.5)
5
- bindata (2.1.0)
6
- builder (3.2.2)
4
+ bindata (2.4.10)
7
5
  diff-lcs (1.1.3)
8
- docile (1.1.1)
9
- faraday (0.8.8)
10
- multipart-post (~> 1.2.0)
11
- git (1.2.6)
12
- github_api (0.10.1)
13
- addressable
14
- faraday (~> 0.8.1)
15
- hashie (>= 1.2)
16
- multi_json (~> 1.4)
17
- nokogiri (~> 1.5.2)
18
- oauth2
19
- hashie (2.0.5)
20
- highline (1.6.20)
21
- httpauth (0.2.0)
22
- jeweler (1.8.8)
23
- builder
24
- bundler (~> 1.0)
25
- git (>= 1.2.5)
26
- github_api (= 0.10.1)
27
- highline (>= 1.6.15)
28
- nokogiri (>= 1.8.2)
29
- rake
30
- rdoc
31
6
  json (>= 2.3.0)
32
- jwt (0.1.8)
33
- multi_json (>= 1.5)
34
- multi_json (1.8.2)
35
- multi_xml (0.5.5)
36
- multipart-post (1.2.0)
37
- nokogiri (>= 1.8.2)
38
- oauth2 (0.9.2)
39
- faraday (~> 0.8)
40
- httpauth (~> 0.2)
41
- jwt (~> 0.1.4)
42
- multi_json (~> 1.0)
43
- multi_xml (~> 0.5)
44
- rack (~> 1.2)
45
- rack (>= 1.6.11)
46
- rake (>= 12.3.3)
47
- rdoc (3.12.2)
7
+ rdoc (>= 6.3.1)
48
8
  json (~> 1.4)
49
9
  rspec (2.8.0)
50
10
  rspec-core (~> 2.8.0)
@@ -61,6 +21,5 @@ PLATFORMS
61
21
  DEPENDENCIES
62
22
  bindata
63
23
  bundler (>= 1.2.0)
64
- jeweler (~> 1.8.4)
65
- rdoc (~> 3.12)
66
- rspec (~> 2.8.0)
24
+ rdoc (>= 6.3.1)
25
+ rspec (~> 2.8.0)
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2021 IP2Location ( support@ip2location.com )
1
+ Copyright (c) 2022 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
@@ -9,10 +9,19 @@ Supported IPv4 and IPv6 address.
9
9
  For more details, please visit:
10
10
  [https://www.ip2location.com/developers/ruby](https://www.ip2location.com/developers/ruby)
11
11
 
12
+ # INSTALLATION
13
+ Install this package using the command as below:
14
+
15
+ ```
16
+ gem install ip2location_ruby
17
+ ```
18
+
12
19
  # Usage
13
20
  You can check the **example.rb** file to learn more about usage.
14
21
 
15
22
  ## BIN Database
23
+ An outdated BIN database was provided in the library for your testing. You are recommended to visit the [links](#dependencies) to download the latest BIN database and save it in the ```rb``` folder.
24
+
16
25
  Below is the description of the functions available in the **BIN Database** lookup.
17
26
 
18
27
  | Function Name | Description |
@@ -45,8 +54,7 @@ Below is the description of the functions available in the **BIN Database** look
45
54
  | get_elevation | Return average height of city above sea level in meters (m). |
46
55
  | get_usagetype | Return the ISP's usage type of IP address. |
47
56
  | get_addresstype | Returns the IP address type (A-Anycast, B-Broadcast, M-Multicast & U-Unicast) of IP address or domain name. |
48
- | category | Returns the IAB content taxonomy category of IP address or domain name. |
49
-
57
+ | get_category | Returns the IAB content taxonomy category of IP address or domain name. |
50
58
 
51
59
  ## Web Service
52
60
  Below is the description of the functions available in the **Web Service** lookup.
@@ -57,13 +65,29 @@ Below is the description of the functions available in the **Web Service** looku
57
65
  | lookup | Return the IP information in array.<ul><li>country_code</li><li>country_name</li><li>region_name</li><li>city_name</li><li>latitude</li><li>longitude</li><li>zip_code</li><li>time_zone</li><li>isp</li><li>domain</li><li>net_speed</li><li>idd_code</li><li>area_code</li><li>weather_station_code</li><li>weather_station_name</li><li>mcc</li><li>mnc</li><li>mobile_brand</li><li>elevation</li><li>usage_type</li><li>address_type</li><li>category</li><li>continent<ul><li>name</li><li>code</li><li>hemisphere</li><li>translations</li></ul></li><li>country<ul><li>name</li><li>alpha3_code</li><li>numeric_code</li><li>demonym</li><li>flag</li><li>capital</li><li>total_area</li><li>population</li><li>currency<ul><li>code</li><li>name</li><li>symbol</li></ul></li><li>language<ul><li>code</li><li>name</li></ul></li><li>idd_code</li><li>tld</li><li>translations</li></ul></li><li>region<ul><li>name</li><li>code</li><li>translations</li></ul></li><li>city<ul><li>name</li><li>translations</li></ul></li><li>geotargeting<ul><li>metro</li></ul></li><li>country_groupings</li><li>time_zone_info<ul><li>olson</li><li>current_time</li><li>gmt_offset</li><li>is_dst</li><li>sunrise</li><li>sunset</li></ul></li><ul> |
58
66
  | get_credit | Return remaining credit of the web service account. |
59
67
 
68
+ ## IpTools
69
+ Below is the description of the functions available in the **IpTools** class.
70
+
71
+ | Function Name | Description |
72
+ |---|---|
73
+ | is_ipv4 | Return either true or false. Verify if a string is a valid IPv4 address. |
74
+ | is_ipv6 | Return either true or false. Verify if a string is a valid IPv6 address. |
75
+ | ipv4_to_decimal | Translate IPv4 address from dotted-decimal address to decimal format. Return null on error. |
76
+ | decimal_to_ipv4 | Translate IPv4 address from decimal number to dotted-decimal address. Return null on error. |
77
+ | ipv6_to_decimal | Translate IPv6 address from hexadecimal address to decimal format. Return null on error. |
78
+ | decimal_to_ipv6 | Translate IPv6 address from decimal number into hexadecimal address. Return null on error. |
79
+ | ipv4_to_cidr | Convert IPv4 range into a list of IPv4 CIDR notation. |
80
+ | cidr_to_ipv4 | Convert IPv4 CIDR notation into a list of IPv4 addresses. |
81
+ | ipv6_to_cidr | Convert IPv6 range into a list of IPv6 CIDR notation. |
82
+ | cidr_to_ipv6 | Convert IPv6 CIDR notation into a list of IPv6 addresses. |
83
+ | compress_ipv6 | Compress a IPv6 to shorten the length. |
84
+ | expand_ipv6 | Expand a shorten IPv6 to full length. |
85
+
60
86
  # Dependencies
61
87
  This library requires IP2Location BIN data file to function. You may download the BIN data file at
62
88
  * IP2Location LITE BIN Data (Free): https://lite.ip2location.com
63
89
  * IP2Location Commercial BIN Data (Comprehensive): https://www.ip2location.com
64
90
 
65
- An outdated BIN database was provided in the library for your testing. You are recommended to visit the above links to download the latest BIN database.
66
-
67
91
  You can also sign up for [IP2Location Web Service](https://www.ip2location.com/web-service/ip2location) to lookup by IP2Location API.
68
92
 
69
93
  # IPv4 BIN vs IPv6 BIN
data/Rakefile CHANGED
@@ -11,20 +11,6 @@ rescue Bundler::BundlerError => e
11
11
  end
12
12
  require 'rake'
13
13
 
14
- require 'jeweler'
15
- Jeweler::Tasks.new do |gem|
16
- # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
- gem.name = "ip2location_ruby"
18
- gem.homepage = "https://github.com/ip2location/ip2location-ruby"
19
- gem.license = "MIT"
20
- gem.summary = %Q{the ip2location ruby library}
21
- gem.description = %Q{ip2location ruby library}
22
- gem.email = "support@ip2location.com"
23
- gem.authors = ["Vincent.Z"]
24
- # dependencies defined in Gemfile
25
- end
26
- Jeweler::RubygemsDotOrgTasks.new
27
-
28
14
  require 'rspec/core'
29
15
  require 'rspec/core/rake_task'
30
16
  RSpec::Core::RakeTask.new(:spec) do |spec|
data/VERSION CHANGED
@@ -1 +1 @@
1
- 8.3.0
1
+ 8.4.0
data/example.rb CHANGED
@@ -4,40 +4,51 @@ require 'ip2location_ruby'
4
4
  i2l = Ip2location.new.open("./data/IP2LOCATION-LITE-DB1.IPV6.BIN")
5
5
  record = i2l.get_all('8.8.8.8')
6
6
 
7
- print 'Country Code: ' + record['country_short'] + "\n"
8
- print 'Country Name: ' + record['country_long'] + "\n"
9
- print 'Region Name: ' + record['region'] + "\n"
10
- print 'City Name: ' + record['city'] + "\n"
11
- print 'Latitude: '
12
- print record['latitude']
13
- print "\n"
14
- print 'Longitude: '
15
- print record['longitude']
16
- print "\n"
17
- print 'ISP: ' + record['isp'] + "\n"
18
- print 'Domain: ' + record['domain'] + "\n"
19
- print 'Net Speed: ' + record['netspeed'] + "\n"
20
- print 'Area Code: ' + record['areacode'] + "\n"
21
- print 'IDD Code: ' + record['iddcode'] + "\n"
22
- print 'Time Zone: ' + record['timezone'] + "\n"
23
- print 'ZIP Code: ' + record['zipcode'] + "\n"
24
- print 'Weather Station Code: ' + record['weatherstationname'] + "\n"
25
- print 'Weather Station Name: ' + record['weatherstationcode'] + "\n"
26
- print 'MCC: ' + record['mcc'] + "\n"
27
- print 'MNC: ' + record['mnc'] + "\n"
28
- print 'Mobile Name: ' + record['mobilebrand'] + "\n"
29
- print 'Elevation: '
30
- print record['elevation']
31
- print "\n"
32
- print 'Usage Type: ' + record['usagetype'] + "\n"
33
- print 'Address Type: ' + record['addresstype'] + "\n"
34
- print 'Category: ' + record['category'] + "\n"
7
+ puts 'Country Code: ' + record['country_short']
8
+ puts 'Country Name: ' + record['country_long']
9
+ puts 'Region Name: ' + record['region']
10
+ puts 'City Name: ' + record['city']
11
+ puts 'Latitude: '
12
+ puts record['latitude']
13
+ puts 'Longitude: '
14
+ puts record['longitude']
15
+ puts 'ISP: ' + record['isp']
16
+ puts 'Domain: ' + record['domain']
17
+ puts 'Net Speed: ' + record['netspeed']
18
+ puts 'Area Code: ' + record['areacode']
19
+ puts 'IDD Code: ' + record['iddcode']
20
+ puts 'Time Zone: ' + record['timezone']
21
+ puts 'ZIP Code: ' + record['zipcode']
22
+ puts 'Weather Station Code: ' + record['weatherstationname']
23
+ puts 'Weather Station Name: ' + record['weatherstationcode']
24
+ puts 'MCC: ' + record['mcc']
25
+ puts 'MNC: ' + record['mnc']
26
+ puts 'Mobile Name: ' + record['mobilebrand']
27
+ puts 'Elevation: '
28
+ puts record['elevation']
29
+ puts 'Usage Type: ' + record['usagetype']
30
+ puts 'Address Type: ' + record['addresstype']
31
+ puts 'Category: ' + record['category']
35
32
 
36
33
  i2l.close()
37
34
 
38
35
  # Web Service
39
36
  ws = Ip2locationWebService.new('demo', 'WS25', true)
40
37
  record = ws.lookup('8.8.8.8', 'continent,country,region,city,geotargeting,country_groupings,time_zone_info', 'en')
41
- print record
42
- print "\n"
43
- print ws.get_credit()
38
+ puts record
39
+ puts ws.get_credit()
40
+
41
+ # IP Tools
42
+ iptool = Ip2locationIpTools.new()
43
+ puts iptool.is_ipv4('8.8.8.8')
44
+ puts iptool.is_ipv6('2001:4860:4860::8888')
45
+ puts iptool.ipv4_to_decimal('8.8.8.8')
46
+ puts iptool.decimal_to_ipv4(134744072)
47
+ puts iptool.ipv6_to_decimal('2001:4860:4860::8888')
48
+ puts iptool.decimal_to_ipv6(42541956123769884636017138956568135816)
49
+ puts iptool.ipv4_to_cidr('8.0.0.0', '8.255.255.255')
50
+ puts iptool.cidr_to_ipv4('8.0.0.0/8')
51
+ puts iptool.ipv6_to_cidr('2002:0000:0000:1234:abcd:ffff:c0a8:0000', '2002:0000:0000:1234:ffff:ffff:ffff:ffff')
52
+ puts iptool.cidr_to_ipv6('2002::1234:abcd:ffff:c0a8:101/64')
53
+ puts iptool.compress_ipv6('2002:0000:0000:1234:ffff:ffff:ffff:ffff')
54
+ puts iptool.expand_ipv6('2002::1234:ffff:ffff:ffff:ffff')
@@ -1,11 +1,6 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
5
-
6
1
  Gem::Specification.new do |s|
7
2
  s.name = "ip2location_ruby"
8
- s.version = "8.3.0"
3
+ s.version = "8.4.0"
9
4
 
10
5
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
6
  s.require_paths = ["lib"]
@@ -36,6 +31,7 @@ Gem::Specification.new do |s|
36
31
  "lib/ip2location_ruby/ip2location_record.rb",
37
32
  "spec/assets/IP2LOCATION-LITE-DB1.IPV6.BIN",
38
33
  "spec/ip2location_ruby_database_spec.rb",
34
+ "spec/ip2location_ruby_iptools_spec.rb",
39
35
  "spec/ip2location_ruby_webservice_spec.rb",
40
36
  "spec/spec_helper.rb",
41
37
  "rb/data/IP2LOCATION-LITE-DB1.IPV6.BIN"
@@ -59,22 +55,19 @@ Gem::Specification.new do |s|
59
55
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
60
56
  s.add_runtime_dependency(%q<bindata>, [">= 0"])
61
57
  s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
62
- s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
58
+ s.add_development_dependency(%q<rdoc>, [">= 6.3.1"])
63
59
  s.add_development_dependency(%q<bundler>, [">= 1.2.0"])
64
- s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
65
60
  else
66
61
  s.add_dependency(%q<bindata>, [">= 0"])
67
62
  s.add_dependency(%q<rspec>, ["~> 2.8.0"])
68
- s.add_dependency(%q<rdoc>, ["~> 3.12"])
63
+ s.add_dependency(%q<rdoc>, [">= 6.3.1"])
69
64
  s.add_dependency(%q<bundler>, [">= 1.2.0"])
70
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
71
65
  end
72
66
  else
73
67
  s.add_dependency(%q<bindata>, [">= 0"])
74
68
  s.add_dependency(%q<rspec>, ["~> 2.8.0"])
75
- s.add_dependency(%q<rdoc>, ["~> 3.12"])
69
+ s.add_dependency(%q<rdoc>, [">= 6.3.1"])
76
70
  s.add_dependency(%q<bundler>, [">= 1.2.0"])
77
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
78
71
  end
79
72
  end
80
73
 
@@ -13,7 +13,7 @@ require 'ip2location_ruby/ip2location_record'
13
13
  class Ip2location
14
14
  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
15
 
16
- VERSION = '8.3.0'
16
+ VERSION = '8.4.0'
17
17
  FIELD_NOT_SUPPORTED = 'NOT SUPPORTED'
18
18
  INVALID_IP_ADDRESS = 'INVALID IP ADDRESS'
19
19
  INVALID_BIN_DATABASE = 'Incorrect IP2Location BIN file format. Please make sure that you are using the latest IP2Location BIN file.'
@@ -29,7 +29,7 @@ class Ip2location
29
29
  self.file = File.open(File.expand_path url, 'rb')
30
30
  rescue
31
31
  self.last_err_msg = 'Ip2location.new.open() error in opening ' + url +'.'
32
- abort('Ip2location.new.open() error in opening ' + url +'.')
32
+ abort('Ip2location.new.open() error in opening ' + url + '. No such file in the /your_ip2location_ruby_library_path/rb/ folder.')
33
33
  else
34
34
  end
35
35
  i2l = Ip2locationConfig.read(file)
@@ -651,7 +651,7 @@ class Ip2location
651
651
  else
652
652
  ipv = 6
653
653
  ipnum = ip.to_i + 0
654
- #reformat ipv4 address in ipv6
654
+ #reformat ipv4 address in ipv6
655
655
  if ipnum >= 281470681743360 && ipnum <= 281474976710655
656
656
  ipv = 4
657
657
  ipnum = ipnum - 281470681743360
@@ -717,7 +717,7 @@ class Ip2locationWebService
717
717
  if parsed_response.nil?
718
718
  return false
719
719
  end
720
- if !parsed_response["response"].nil?
720
+ if parsed_response["country_code"].nil?
721
721
  raise Exception.new "Error: " + parsed_response["response"]
722
722
  end
723
723
  return parsed_response
@@ -738,4 +738,228 @@ class Ip2locationWebService
738
738
  end
739
739
  return parsed_response["response"]
740
740
  end
741
+ end
742
+
743
+ class Ip2locationIpTools
744
+ def is_ipv4(ip)
745
+ if !(IPAddr.new(ip) rescue nil).nil?
746
+ if IPAddr.new(ip).ipv4?
747
+ return true
748
+ else
749
+ return false
750
+ end
751
+ else
752
+ return false
753
+ end
754
+ end
755
+
756
+ def is_ipv6(ip)
757
+ if !(IPAddr.new(ip) rescue nil).nil?
758
+ if IPAddr.new(ip).ipv6?
759
+ return true
760
+ else
761
+ return false
762
+ end
763
+ else
764
+ return false
765
+ end
766
+ end
767
+
768
+ def ipv4_to_decimal(ip)
769
+ if !(IPAddr.new(ip) rescue nil).nil?
770
+ ip = IPAddr.new(ip)
771
+ if ip.ipv4?
772
+ return ip.to_i
773
+ else
774
+ return
775
+ end
776
+ else
777
+ return
778
+ end
779
+ end
780
+
781
+ def decimal_to_ipv4(number)
782
+ if number.is_a? Numeric
783
+ return IPAddr.new(number, Socket::AF_INET).to_s
784
+ else
785
+ return
786
+ end
787
+ end
788
+
789
+ def ipv6_to_decimal(ip)
790
+ if !(IPAddr.new(ip) rescue nil).nil?
791
+ ip = IPAddr.new(ip)
792
+ if ip.ipv6?
793
+ return ip.to_i
794
+ else
795
+ return
796
+ end
797
+ else
798
+ return
799
+ end
800
+ end
801
+
802
+ def decimal_to_ipv6(number)
803
+ if number.is_a? Numeric
804
+ return IPAddr.new(number, Socket::AF_INET6).to_s
805
+ else
806
+ return
807
+ end
808
+ end
809
+
810
+ def ipv4_to_cidr(ipfrom, ipto)
811
+ if (!(IPAddr.new(ipfrom) rescue nil).nil?) and (!(IPAddr.new(ipto) rescue nil).nil?)
812
+ ipfrom = IPAddr.new(ipfrom)
813
+ ipto = IPAddr.new(ipto)
814
+ if ipfrom.ipv4? and ipto.ipv4?
815
+ ipfrom = ipfrom.to_i
816
+ ipto = ipto.to_i
817
+ result = []
818
+ while ipto >= ipfrom do
819
+ maxSize = 32
820
+ while maxSize > 0 do
821
+ mask = (2**32 - 2**(32-(maxSize - 1)))
822
+ maskBase = ipfrom & mask
823
+ if maskBase != ipfrom
824
+ break
825
+ end
826
+ maxSize-=1
827
+ end
828
+ x = Math.log(ipto - ipfrom + 1)/Math.log(2)
829
+ maxDiff = (32 - x.floor).floor
830
+
831
+ if maxSize < maxDiff
832
+ maxSize = maxDiff
833
+ end
834
+
835
+ ip = IPAddr.new(ipfrom, Socket::AF_INET).to_s
836
+ cidr = [ip, maxSize].join('/')
837
+ result.push cidr
838
+ ipfrom += 2**(32-maxSize)
839
+ end
840
+ return result
841
+ else
842
+ return
843
+ end
844
+ else
845
+ return
846
+ end
847
+ end
848
+
849
+ def cidr_to_ipv4(cidr)
850
+ if cidr.include? "/"
851
+ cidr = IPAddr.new(cidr)
852
+ arr_tmp = cidr.to_range.to_s.split('..')
853
+ ip_arr = {}
854
+ ip_arr['ip_start'] = arr_tmp[0]
855
+ ip_arr['ip_end'] = arr_tmp[1]
856
+ return ip_arr
857
+ else
858
+ return
859
+ end
860
+ end
861
+
862
+ def ipv6_to_cidr(ipfrom_ori, ipto)
863
+ if (!(IPAddr.new(ipfrom_ori) rescue nil).nil?) and (!(IPAddr.new(ipto) rescue nil).nil?)
864
+ ipfrom = IPAddr.new(ipfrom_ori)
865
+ ipto = IPAddr.new(ipto)
866
+ if ipfrom.ipv6? and ipto.ipv6?
867
+ ipfrom = '00' + ipfrom.to_i.to_s(2)
868
+ ipto = '00' + ipto.to_i.to_s(2)
869
+ result = []
870
+ if ipfrom == ipto
871
+ cidr = ipfrom_ori + '/128'
872
+ result.push cidr
873
+ return result
874
+ end
875
+ if ipfrom > ipto
876
+ ipfrom, ipto = ipto, ipfrom
877
+ end
878
+ networks = {}
879
+ network_size = 0
880
+ while ipto > ipfrom do
881
+ if ipfrom[-1, 1] == '1'
882
+ networks[ipfrom[network_size, (128 - network_size)] + ('0' * network_size)] = 128 - network_size
883
+ n = ipfrom.rindex('0')
884
+ ipfrom = ((n == 0) ? '' : ipfrom[0, n]) + '1' + ('0' * (128 - n - 1))
885
+ end
886
+
887
+ if ipto[-1, 1] == '0'
888
+ networks[ipto[network_size, (128 - network_size)] + ('0' * network_size)] = 128 - network_size
889
+ n = ipto.rindex('1')
890
+ ipto = ((n == 0) ? '' : ipto[0, n]) + '0' + ('1' * (128 - n - 1))
891
+ end
892
+
893
+ if ipfrom > ipto
894
+ next
895
+ end
896
+
897
+ shift = 128 - [ipfrom.rindex('0'), ipto.rindex('1')].max
898
+ ipfrom = ('0' * shift) + ipfrom[0, (128 - shift)]
899
+ ipto = ('0' * shift) + ipto[0, (128 - shift)]
900
+ network_size = network_size + shift
901
+
902
+ if ipfrom == ipto
903
+ networks[ipfrom[network_size, (128 - network_size)] + ('0' * network_size)] = 128 - network_size
904
+ next
905
+ end
906
+ end
907
+
908
+ networks.each do |ip, netmask|
909
+ result.push IPAddr.new(ip.to_i(2), Socket::AF_INET6).to_s + '/' + netmask.to_s
910
+ end
911
+ return result
912
+ else
913
+ return
914
+ end
915
+ else
916
+ return
917
+ end
918
+ end
919
+
920
+ def cidr_to_ipv6(cidr)
921
+ if cidr.include? "/"
922
+ ip_start = IPAddr.new(cidr.to_s.split('/').first).to_i.to_s(16).scan(/.{4}/)
923
+ ip_start = ip_start[0] + ':' + ip_start[1] + ':' + ip_start[2] + ':' + ip_start[3] + ':' + ip_start[4] + ':' + ip_start[5] + ':' + ip_start[6] + ':' + ip_start[7]
924
+
925
+ cidr_new = IPAddr.new(cidr)
926
+ arr_tmp = cidr_new.to_range.to_s.split('..')
927
+ ip_end = IPAddr.new(arr_tmp[1]).to_i.to_s(16).scan(/.{4}/)
928
+ ip_end = ip_end[0] + ':' + ip_end[1] + ':' + ip_end[2] + ':' + ip_end[3] + ':' + ip_end[4] + ':' + ip_end[5] + ':' + ip_end[6] + ':' + ip_end[7]
929
+
930
+ ip_arr = {}
931
+ ip_arr['ip_start'] = ip_start
932
+ ip_arr['ip_end'] = ip_end
933
+ return ip_arr
934
+ else
935
+ return
936
+ end
937
+ end
938
+
939
+ def compress_ipv6(ip)
940
+ if !(IPAddr.new(ip) rescue nil).nil?
941
+ ip = IPAddr.new(ip)
942
+ if ip.ipv6?
943
+ return ip
944
+ else
945
+ return
946
+ end
947
+ else
948
+ return
949
+ end
950
+ end
951
+
952
+ def expand_ipv6(ip)
953
+ if !(IPAddr.new(ip) rescue nil).nil?
954
+ ip = IPAddr.new(ip)
955
+ if ip.ipv6?
956
+ res = ip.to_i.to_s(16).scan(/.{4}/)
957
+ return res[0] + ':' + res[1] + ':' + res[2] + ':' + res[3] + ':' + res[4] + ':' + res[5] + ':' + res[6] + ':' + res[7]
958
+ else
959
+ return
960
+ end
961
+ else
962
+ return
963
+ end
964
+ end
741
965
  end
@@ -0,0 +1,94 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Ip2location" do
4
+ it "work correctly with iptools is_ipv4" do
5
+ iptool = Ip2locationIpTools.new()
6
+ record = iptool.is_ipv4('8.8.8.8')
7
+ expect(record).to eq true
8
+ end
9
+
10
+ it "work correctly with iptools invalid is_ipv4" do
11
+ iptool = Ip2locationIpTools.new()
12
+ record = iptool.is_ipv4('8.8.8.555')
13
+ expect(record).to eq false
14
+ end
15
+
16
+ it "work correctly with iptools is_ipv6" do
17
+ iptool = Ip2locationIpTools.new()
18
+ record = iptool.is_ipv6('2001:4860:4860::8888')
19
+ expect(record).to eq true
20
+ end
21
+
22
+ it "work correctly with iptools invalid is_ipv6" do
23
+ iptool = Ip2locationIpTools.new()
24
+ record = iptool.is_ipv6('2001:4860:4860::ZZZZ')
25
+ expect(record).to eq false
26
+ end
27
+
28
+ it "work correctly with iptools ipv4_to_decimal" do
29
+ iptool = Ip2locationIpTools.new()
30
+ record = iptool.ipv4_to_decimal('8.8.8.8')
31
+ expect(record).to eq 134744072
32
+ end
33
+
34
+ it "work correctly with iptools decimal_to_ipv4" do
35
+ iptool = Ip2locationIpTools.new()
36
+ record = iptool.decimal_to_ipv4(134744072)
37
+ expect(record).to eq '8.8.8.8'
38
+ end
39
+
40
+ it "work correctly with iptools ipv6_to_decimal" do
41
+ iptool = Ip2locationIpTools.new()
42
+ record = iptool.ipv6_to_decimal('2001:4860:4860::8888')
43
+ expect(record).to eq 42541956123769884636017138956568135816
44
+ end
45
+
46
+ it "work correctly with iptools decimal_to_ipv6" do
47
+ iptool = Ip2locationIpTools.new()
48
+ record = iptool.decimal_to_ipv6(42541956123769884636017138956568135816)
49
+ expect(record).to eq '2001:4860:4860::8888'
50
+ end
51
+
52
+ it "work correctly with iptools ipv4_to_cidr" do
53
+ iptool = Ip2locationIpTools.new()
54
+ record = iptool.ipv4_to_cidr('8.0.0.0', '8.255.255.255')
55
+ expect(record).to eq ['8.0.0.0/8']
56
+ end
57
+
58
+ it "work correctly with iptools cidr_to_ipv4" do
59
+ iptool = Ip2locationIpTools.new()
60
+ record = iptool.cidr_to_ipv4('8.0.0.0/8')
61
+ expect(record).to include(
62
+ "ip_start"=>"8.0.0.0",
63
+ "ip_end"=>"8.255.255.255"
64
+ )
65
+ end
66
+
67
+ it "work correctly with iptools ipv6_to_cidr" do
68
+ iptool = Ip2locationIpTools.new()
69
+ record = iptool.ipv6_to_cidr('2002:0000:0000:1234:abcd:ffff:c0a8:0000', '2002:0000:0000:1234:ffff:ffff:ffff:ffff')
70
+ expect(record).to eq ['2002::1234:abcd:ffff:c0a8:0/109','2002::1234:abcd:ffff:c0b0:0/108','2002::1234:abcd:ffff:c0c0:0/106','2002::1234:abcd:ffff:c100:0/104','2002::1234:abcd:ffff:c200:0/103','2002::1234:abcd:ffff:c400:0/102','2002::1234:abcd:ffff:c800:0/101','2002::1234:abcd:ffff:d000:0/100','2002::1234:abcd:ffff:e000:0/99','2002:0:0:1234:abce::/79','2002:0:0:1234:abd0::/76','2002:0:0:1234:abe0::/75','2002:0:0:1234:ac00::/70','2002:0:0:1234:b000::/68','2002:0:0:1234:c000::/66']
71
+ end
72
+
73
+ it "work correctly with iptools cidr_to_ipv6" do
74
+ iptool = Ip2locationIpTools.new()
75
+ record = iptool.cidr_to_ipv6('2002::1234:abcd:ffff:c0a8:101/64')
76
+ expect(record).to include(
77
+ "ip_start"=>"2002:0000:0000:1234:abcd:ffff:c0a8:0101",
78
+ "ip_end"=>"2002:0000:0000:1234:ffff:ffff:ffff:ffff"
79
+ )
80
+ end
81
+
82
+ it "work correctly with iptools compress_ipv6" do
83
+ iptool = Ip2locationIpTools.new()
84
+ record = iptool.compress_ipv6('2002:0000:0000:1234:ffff:ffff:ffff:ffff')
85
+ expect(record).to eq '2002::1234:ffff:ffff:ffff:ffff'
86
+ end
87
+
88
+ it "work correctly with iptools expand_ipv6" do
89
+ iptool = Ip2locationIpTools.new()
90
+ record = iptool.expand_ipv6('2002::1234:ffff:ffff:ffff:ffff')
91
+ expect(record).to eq '2002:0000:0000:1234:ffff:ffff:ffff:ffff'
92
+ end
93
+
94
+ end
@@ -10,6 +10,6 @@ describe "Ip2location" do
10
10
  it "work correctly with web service get_credit" do
11
11
  ws = Ip2locationWebService.new('demo', 'WS25', true)
12
12
  record = ws.get_credit()
13
- expect(record).to_not eq 0
13
+ expect(record).to eq 0
14
14
  end
15
15
  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.3.0
4
+ version: 8.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ip2location
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-01 00:00:00.000000000 Z
11
+ date: 2022-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bindata
@@ -42,16 +42,16 @@ dependencies:
42
42
  name: rdoc
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '3.12'
47
+ version: 6.3.1
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '3.12'
54
+ version: 6.3.1
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -66,20 +66,6 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: 1.2.0
69
- - !ruby/object:Gem::Dependency
70
- name: jeweler
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: 1.8.4
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: 1.8.4
83
69
  description: The official IP2Location Ruby library to geolocate an IP address. You
84
70
  can lookup for country, region, city, latitude, longitude, zip code, time zone,
85
71
  ISP, domain, area code, usage type, mobile data, weather data and elevation from
@@ -111,6 +97,7 @@ files:
111
97
  - rb/data/IP2LOCATION-LITE-DB1.IPV6.BIN
112
98
  - spec/assets/IP2LOCATION-LITE-DB1.IPV6.BIN
113
99
  - spec/ip2location_ruby_database_spec.rb
100
+ - spec/ip2location_ruby_iptools_spec.rb
114
101
  - spec/ip2location_ruby_webservice_spec.rb
115
102
  - spec/spec_helper.rb
116
103
  homepage: https://github.com/ip2location/ip2location-ruby
@@ -121,7 +108,7 @@ metadata:
121
108
  documentation_uri: https://www.rubydoc.info/gems/ip2location_ruby
122
109
  homepage_uri: https://www.ip2location.com
123
110
  source_code_uri: https://github.com/ip2location/ip2location-ruby
124
- post_install_message:
111
+ post_install_message:
125
112
  rdoc_options: []
126
113
  require_paths:
127
114
  - lib
@@ -136,9 +123,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
123
  - !ruby/object:Gem::Version
137
124
  version: '0'
138
125
  requirements: []
139
- rubyforge_project:
140
- rubygems_version: 2.7.6.2
141
- signing_key:
126
+ rubygems_version: 3.2.33
127
+ signing_key:
142
128
  specification_version: 4
143
129
  summary: the ip2location ruby library
144
130
  test_files: []