ip2location_ruby 8.5.0 → 8.6.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: f7f7cc867aa07b3595ae89b5f5fa55fab2b210236b106fa8ad10ccb5b9dd15b1
4
- data.tar.gz: 36b720b84b89cfaad323c1d695e1b5408eca69476197cdb7911cdb614856c5e9
3
+ metadata.gz: 3b96ea3f6c523f75a72a00b44179ed6cfa7101bcd9adacc1edfb58b74df8885e
4
+ data.tar.gz: c3e223b2caf2b0b8e72d80586229db8addb576a8286878f3cda360194f411562
5
5
  SHA512:
6
- metadata.gz: 7a84781d5ab63cee79729be08a0ebdc1f3866630b8f00e4aea983e654c2cf55fd66370d8006666c76880621231fd91d9ddf3f19c2efdd0d7b7fde9428f630935
7
- data.tar.gz: d6e40740c7fa84c5410239e93f54502e197a19dcc8c00b8a1d30bec76660a35bb9dfe78bced75ae7b4b3c15e29ad9f7e339a1b817e4b28cc4aff99caaf58f624
6
+ metadata.gz: 8f587dfa25da7d4437c4dfb6089e5806461acc6917b4201922ac4dcf492e8d78507e003a4229afd324aee99d92851711fb1ed3f6071aa30934df7fa5dd164621
7
+ data.tar.gz: 8b8522bee147a05b3c799deaf8fbc4eacf26bf8109a4ad00e7fde806bd3462bc3909917075041a61352bfabbe922e0eee77742dfbb0793e230c998fdf58b4505
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.6.0
data/example.rb CHANGED
@@ -29,7 +29,6 @@ puts record['elevation']
29
29
  puts 'Usage Type: ' + record['usagetype']
30
30
  puts 'Address Type: ' + record['addresstype']
31
31
  puts 'Category: ' + record['category']
32
-
33
32
  i2l.close()
34
33
 
35
34
  # Web Service
@@ -52,3 +51,11 @@ puts iptool.ipv6_to_cidr('2002:0000:0000:1234:abcd:ffff:c0a8:0000', '2002:0000:0
52
51
  puts iptool.cidr_to_ipv6('2002::1234:abcd:ffff:c0a8:101/64')
53
52
  puts iptool.compress_ipv6('2002:0000:0000:1234:ffff:ffff:ffff:ffff')
54
53
  puts iptool.expand_ipv6('2002::1234:ffff:ffff:ffff:ffff')
54
+
55
+ # Country Class
56
+ country = Ip2locationCountry.new('./data/IP2LOCATION-COUNTRY-INFORMATION-BASIC.CSV')
57
+ puts country.get_country_info('US')
58
+
59
+ # Region Class
60
+ region = Ip2locationRegion.new('./data/IP2LOCATION-ISO3166-2.CSV')
61
+ 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.6.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"]
@@ -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.6.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.'
@@ -964,4 +965,127 @@ class Ip2locationIpTools
964
965
  return
965
966
  end
966
967
  end
968
+ end
969
+
970
+ class Ip2locationCountry
971
+ attr_accessor :fields, :records
972
+
973
+ def initialize(csv)
974
+ if csv == ''
975
+ abort('The CSV file "' + csv + '" is not found.')
976
+ end
977
+
978
+ begin
979
+ csvfile = File.open(File.expand_path csv, 'rb')
980
+ rescue
981
+ abort('Error in opening ' + csv + '. No such CSV file in the /your_ip2location_ruby_library_path/rb/ folder.')
982
+ else
983
+ end
984
+
985
+ begin
986
+ CSV.parse(csvfile)
987
+ rescue
988
+ abort('Unable to read "' + csv + '".')
989
+ else
990
+ line = 1
991
+ self.records = Hash.new
992
+ CSV.foreach((csvfile)) do |data|
993
+ if line == 1
994
+ if data[0] != 'country_code'
995
+ abort('Invalid country information CSV file.')
996
+ end
997
+ self.fields = data
998
+ else
999
+ self.records[data[0]] = data
1000
+ end
1001
+ line = line + 1
1002
+ end
1003
+ end
1004
+ end
1005
+
1006
+ def get_country_info(country_code = nil)
1007
+ if self.records.empty?
1008
+ abort('No record available.')
1009
+ end
1010
+
1011
+ if country_code
1012
+ if (self.records[country_code]).nil?
1013
+ return []
1014
+ end
1015
+ results = Hash.new
1016
+ for i in 0..(self.fields.length()-1)
1017
+ results[self.fields[i]] = self.records[country_code][i]
1018
+ end
1019
+ return results
1020
+ end
1021
+
1022
+ results = []
1023
+ self.records.each do |key, value|
1024
+ data = Hash.new
1025
+ for i in 0..(self.fields.length()-1)
1026
+ data[self.fields[i]] = self.records[key][i]
1027
+ end
1028
+ results = results.append(data)
1029
+ end
1030
+ return results
1031
+ end
1032
+ end
1033
+
1034
+ class Ip2locationRegion
1035
+ attr_accessor :records
1036
+
1037
+ def initialize(csv)
1038
+ if csv == ''
1039
+ abort('The CSV file "' + csv + '" is not found.')
1040
+ end
1041
+
1042
+ begin
1043
+ csvfile = File.open(File.expand_path csv, 'rb')
1044
+ rescue
1045
+ abort('Error in opening ' + csv + '. No such CSV file in the /your_ip2location_ruby_library_path/rb/ folder.')
1046
+ else
1047
+ end
1048
+
1049
+ begin
1050
+ CSV.parse(csvfile)
1051
+ rescue
1052
+ abort('Unable to read "' + csv + '".')
1053
+ else
1054
+ line = 1
1055
+ self.records = Hash.new
1056
+ CSV.foreach((csvfile)) do |data|
1057
+ if line == 1
1058
+ if data[1] != 'subdivision_name'
1059
+ abort('Invalid region information CSV file.')
1060
+ end
1061
+ else
1062
+ temp_data = Hash.new
1063
+ temp_data['code'] = data[2]
1064
+ temp_data['name'] = data[1]
1065
+ if self.records[data[0]]
1066
+ self.records[data[0]].push temp_data
1067
+ else
1068
+ self.records[data[0]] = [temp_data]
1069
+ end
1070
+ end
1071
+ line = line + 1
1072
+ end
1073
+ end
1074
+ end
1075
+
1076
+ def get_region_code(country_code, region_name)
1077
+ if self.records.empty?
1078
+ abort('No record available.')
1079
+ end
1080
+
1081
+ if (self.records[country_code]).nil?
1082
+ return
1083
+ end
1084
+
1085
+ for i in 0..(self.records[country_code].length()-1)
1086
+ if region_name.upcase == self.records[country_code][i]["name"].upcase
1087
+ return self.records[country_code][i]["code"]
1088
+ end
1089
+ end
1090
+ end
967
1091
  end
@@ -0,0 +1,250 @@
1
+ "country_code","capital","total_area"
2
+ "AD","Andorra la Vella","468"
3
+ "AE","Abu Dhabi","83600"
4
+ "AF","Kabul","652230"
5
+ "AG","Saint John's","442"
6
+ "AI","The Valley","91"
7
+ "AL","Tirana","28748"
8
+ "AM","Yerevan","29743"
9
+ "AO","Luanda","1246700"
10
+ "AQ","-","14000000"
11
+ "AR","Buenos Aires","2780400"
12
+ "AS","Pago Pago","199"
13
+ "AT","Vienna","83871"
14
+ "AU","Canberra","7741220"
15
+ "AW","Oranjestad","180"
16
+ "AX","Mariehamn","1552"
17
+ "AZ","Baku","86600"
18
+ "BA","Sarajevo","51197"
19
+ "BB","Bridgetown","430"
20
+ "BD","Dhaka","143998"
21
+ "BE","Brussels","30528"
22
+ "BF","Ouagadougou","274200"
23
+ "BG","Sofia","110879"
24
+ "BH","Manama","760"
25
+ "BI","Bujumbura","27830"
26
+ "BJ","Porto-Novo","112622"
27
+ "BL","Gustavia","21"
28
+ "BM","Hamilton","54"
29
+ "BN","Bandar Seri Begawan","5765"
30
+ "BO","Sucre","1098581"
31
+ "BQ","Kralendijk","328"
32
+ "BR","Brasilia","8514877"
33
+ "BS","Nassau","13880"
34
+ "BT","Thimphu","38394"
35
+ "BV","-","49"
36
+ "BW","Gaborone","581730"
37
+ "BY","Minsk","207600"
38
+ "BZ","Belmopan","22966"
39
+ "CA","Ottawa","9984670"
40
+ "CC","West Island","14"
41
+ "CD","Kinshasa","2344858"
42
+ "CF","Bangui","622984"
43
+ "CG","Brazzaville","342000"
44
+ "CH","Bern","41277"
45
+ "CI","Yamoussoukro","322463"
46
+ "CK","Avarua","236"
47
+ "CL","Santiago","756102"
48
+ "CM","Yaounde","475440"
49
+ "CN","Beijing","9596961"
50
+ "CO","Bogota","1138910"
51
+ "CR","San Jose","51100"
52
+ "CU","Havana","110860"
53
+ "CV","Praia","4033"
54
+ "CW","Willemstad","444"
55
+ "CX","Flying Fish Cove","135"
56
+ "CY","Nicosia","9251"
57
+ "CZ","Prague","78867"
58
+ "DE","Berlin","357022"
59
+ "DJ","Djibouti","23200"
60
+ "DK","Copenhagen","43094"
61
+ "DM","Roseau","751"
62
+ "DO","Santo Domingo","48670"
63
+ "DZ","Algiers","2381741"
64
+ "EC","Quito","283561"
65
+ "EE","Tallinn","45228"
66
+ "EG","Cairo","1001450"
67
+ "EH","Laayoune / El Aaiun","266000"
68
+ "ER","Asmara","117600"
69
+ "ES","Madrid","505370"
70
+ "ET","Addis Ababa","1104300"
71
+ "FI","Helsinki","338145"
72
+ "FJ","Suva","18274"
73
+ "FK","Stanley","12173"
74
+ "FM","Palikir","702"
75
+ "FO","Torshavn","1393"
76
+ "FR","Paris","643801"
77
+ "GA","Libreville","267667"
78
+ "GB","London","243610"
79
+ "GD","Saint George's","344"
80
+ "GE","Tbilisi","69700"
81
+ "GF","Cayenne","86504"
82
+ "GG","Saint Peter Port","78"
83
+ "GH","Accra","238533"
84
+ "GI","Gibraltar","6"
85
+ "GL","Nuuk","2166086"
86
+ "GM","Banjul","11295"
87
+ "GN","Conakry","245857"
88
+ "GP","Basse-terre","1630"
89
+ "GQ","Malabo","28051"
90
+ "GR","Athens","131957"
91
+ "GS","Grytviken","3903"
92
+ "GT","Guatemala City","108889"
93
+ "GU","Hagatna","544"
94
+ "GW","Bissau","36125"
95
+ "GY","Georgetown","214969"
96
+ "HK","-","1104"
97
+ "HM","-","412"
98
+ "HN","Tegucigalpa","112090"
99
+ "HR","Zagreb","56594"
100
+ "HT","Port-au-prince","27750"
101
+ "HU","Budapest","93028"
102
+ "ID","Jakarta","1904569"
103
+ "IE","Dublin","70273"
104
+ "IL","Jerusalem","20770"
105
+ "IM","Douglas","572"
106
+ "IN","New Delhi","3287263"
107
+ "IO","Diego Garcia","54400"
108
+ "IQ","Baghdad","438317"
109
+ "IR","Tehran","1648195"
110
+ "IS","Reykjavik","103000"
111
+ "IT","Roma","301340"
112
+ "JE","Saint Helier","116"
113
+ "JM","Kingston","10991"
114
+ "JO","Amman","89342"
115
+ "JP","Tokyo","377915"
116
+ "KE","Nairobi","580367"
117
+ "KG","Bishkek","199951"
118
+ "KH","Phnom Penh","181035"
119
+ "KI","Tarawa","811"
120
+ "KM","Moroni","2235"
121
+ "KN","Basseterre","261"
122
+ "KP","Pyongyang","120538"
123
+ "KR","Seoul","99720"
124
+ "KW","Kuwait","17818"
125
+ "KY","George Town","264"
126
+ "KZ","Astana","2724900"
127
+ "LA","Vientiane","236800"
128
+ "LB","Beirut","10400"
129
+ "LC","Castries","616"
130
+ "LI","Vaduz","160"
131
+ "LK","Sri Jayewardenepura Kotte","65610"
132
+ "LR","Monrovia","111369"
133
+ "LS","Maseru","30355"
134
+ "LT","Vilnius","65300"
135
+ "LU","Luxembourg","2586"
136
+ "LV","Riga","64589"
137
+ "LY","Tripoli","1759540"
138
+ "MA","Rabat","446550"
139
+ "MC","Monaco","2"
140
+ "MD","Chisinau","33851"
141
+ "ME","Podgorica","13812"
142
+ "MF","Marigot","54"
143
+ "MG","Antananarivo","587041"
144
+ "MH","Majuro","181"
145
+ "MK","Skopje","25713"
146
+ "ML","Bamako","1240192"
147
+ "MM","Pyinmana","676578"
148
+ "MN","Ulaanbaatar","1564116"
149
+ "MO","-","31.3"
150
+ "MP","Saipan","464"
151
+ "MQ","Fort-de-france","1128"
152
+ "MR","Nouakchott","1030700"
153
+ "MS","Plymouth","102"
154
+ "MT","Valletta","316"
155
+ "MU","Port Louis","2040"
156
+ "MV","Male","298"
157
+ "MW","Lilongwe","118484"
158
+ "MX","Mexico City","1964375"
159
+ "MY","Kuala Lumpur","329847"
160
+ "MZ","Maputo","799380"
161
+ "NA","Windhoek","824292"
162
+ "NC","Noumea","18575"
163
+ "NE","Niamey","1186408"
164
+ "NF","Kingston","36"
165
+ "NG","Abuja","923768"
166
+ "NI","Managua","130370"
167
+ "NL","Amsterdam","41543"
168
+ "NO","Oslo","323802"
169
+ "NP","Kathmandu","147181"
170
+ "NR","Yaren","21"
171
+ "NU","Alofi","260"
172
+ "NZ","Wellington","267710"
173
+ "OM","Muscat","309500"
174
+ "PA","Panama","75420"
175
+ "PE","Lima","1285216"
176
+ "PF","Papeete","4167"
177
+ "PG","Port Moresby","462840"
178
+ "PH","Manila","300000"
179
+ "PK","Islamabad","796095"
180
+ "PL","Warsaw","312685"
181
+ "PM","Saint-pierre","242"
182
+ "PN","Adamstown","47"
183
+ "PR","San Juan","13790"
184
+ "PS","-","5860"
185
+ "PT","Lisbon","92090"
186
+ "PW","Melekeok - Palau State Capital","459"
187
+ "PY","Asuncion","406752"
188
+ "QA","Doha","11586"
189
+ "RE","Saint-denis","2512"
190
+ "RO","Bucharest","238391"
191
+ "RS","Belgrade","77474"
192
+ "RU","Moscow","17098242"
193
+ "RW","Kigali","26338"
194
+ "SA","Riyadh","2149690"
195
+ "SB","Honiara","28896"
196
+ "SC","Victoria","455"
197
+ "SD","Khartoum","1861484"
198
+ "SE","Stockholm","450295"
199
+ "SG","Singapore","697"
200
+ "SH","Jamestown","122"
201
+ "SI","Ljubljana","20273"
202
+ "SJ","Longyearbyen","62045"
203
+ "SK","Bratislava","49035"
204
+ "SL","Freetown","71740"
205
+ "SM","San Marino","61"
206
+ "SN","Dakar","196722"
207
+ "SO","Mogadishu","637657"
208
+ "SR","Paramaribo","163820"
209
+ "SS","Juba","644329"
210
+ "ST","Sao Tome","964"
211
+ "SV","San Salvador","21041"
212
+ "SX","Philipsburg","34"
213
+ "SY","Damascus","185180"
214
+ "SZ","Mbabane","17364"
215
+ "TC","Cockburn Town","948"
216
+ "TD","N'djamena","1284000"
217
+ "TF","Port-aux-francais","439781"
218
+ "TG","Lome","56785"
219
+ "TH","Bangkok","513120"
220
+ "TJ","Dushanbe","143100"
221
+ "TK","-","12"
222
+ "TL","Dili","14874"
223
+ "TM","Ashgabat","488100"
224
+ "TN","Tunis","163610"
225
+ "TO","Nuku'alofa","747"
226
+ "TR","Ankara","783562"
227
+ "TT","Port of Spain","5128"
228
+ "TV","Funafuti","26"
229
+ "TW","Taipei","35980"
230
+ "TZ","Dodoma","947300"
231
+ "UA","Kiev","603550"
232
+ "UG","Kampala","241038"
233
+ "UM","-","34.2"
234
+ "US","Washington, D.C.","9826675"
235
+ "UY","Montevideo","176215"
236
+ "UZ","Tashkent","447400"
237
+ "VA","Vatican City","0.44"
238
+ "VC","Kingstown","389"
239
+ "VE","Caracas","912050"
240
+ "VG","Road Town","151"
241
+ "VI","Charlotte Amalie","1910"
242
+ "VN","Ha Noi","331210"
243
+ "VU","Port-vila","12189"
244
+ "WF","Mata'utu","142"
245
+ "WS","Apia","2831"
246
+ "YE","Sanaa","527968"
247
+ "YT","Mamoudzou","374"
248
+ "ZA","Cape Town","1219090"
249
+ "ZM","Lusaka","752618"
250
+ "ZW","Harare","390757"