Skip to content

Commit 5c33063

Browse files
committed
Created Ip2locationIpTools class
1 parent ec7a8bd commit 5c33063

File tree

8 files changed

+386
-39
lines changed

8 files changed

+386
-39
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) 2021 IP2Location ( support@ip2location.com )
1+
Copyright (c) 2022 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

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ Below is the description of the functions available in the **BIN Database** look
5656
| get_addresstype | Returns the IP address type (A-Anycast, B-Broadcast, M-Multicast & U-Unicast) of IP address or domain name. |
5757
| get_category | Returns the IAB content taxonomy category of IP address or domain name. |
5858

59-
6059
## Web Service
6160
Below is the description of the functions available in the **Web Service** lookup.
6261

@@ -66,6 +65,24 @@ Below is the description of the functions available in the **Web Service** looku
6665
| 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> |
6766
| get_credit | Return remaining credit of the web service account. |
6867

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+
6986
# Dependencies
7087
This library requires IP2Location BIN data file to function. You may download the BIN data file at
7188
* IP2Location LITE BIN Data (Free): https://lite.ip2location.com

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8.3.2
1+
8.4.0

example.rb

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,51 @@
44
i2l = Ip2location.new.open("./data/IP2LOCATION-LITE-DB1.IPV6.BIN")
55
record = i2l.get_all('8.8.8.8')
66

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']
3532

3633
i2l.close()
3734

3835
# Web Service
3936
ws = Ip2locationWebService.new('demo', 'WS25', true)
4037
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')

ip2location_ruby.gemspec

Lines changed: 2 additions & 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.3.2"
3+
s.version = "8.4.0"
44

55
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
66
s.require_paths = ["lib"]
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
3131
"lib/ip2location_ruby/ip2location_record.rb",
3232
"spec/assets/IP2LOCATION-LITE-DB1.IPV6.BIN",
3333
"spec/ip2location_ruby_database_spec.rb",
34+
"spec/ip2location_ruby_iptools_spec.rb",
3435
"spec/ip2location_ruby_webservice_spec.rb",
3536
"spec/spec_helper.rb",
3637
"rb/data/IP2LOCATION-LITE-DB1.IPV6.BIN"

0 commit comments

Comments
 (0)