ip2location_ruby 0.3.2 → 7.0.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
  SHA1:
3
- metadata.gz: d572eaea90429a8a3eff55e48f0986a58ef371db
4
- data.tar.gz: 4e4de29d32aa884a910948d5844a2bf05fcd0ed5
3
+ metadata.gz: 184b7cf7691c597f947ed45601a088bc55929d80
4
+ data.tar.gz: 1b9f7048300d59e28550bfe0fc63e531faac7e2f
5
5
  SHA512:
6
- metadata.gz: e4b287394f962b7a620fbb770408b0d50af2dafaaff80d946395f29a073dae97b2dbbf6e2981e48de93ec8d9eb156ec933900c76c509e4596f66141ef46c0071
7
- data.tar.gz: 06ecd794eba9697f7f3035de7579e8021e76a7415cacea7be9c78307d64d9ba9e8943d8b5a4a698e6f003150e69533485983da0a53b23bc6d9f7bd6dee082f0e
6
+ metadata.gz: bc59483ab9481fb41466cfaa3b11f1cda97c859dd085d3ba178244d75e73a69b4c062b14225f00cf206827502e84720e53744e15e434721a4938115e939b790a
7
+ data.tar.gz: 41845e31fca4badeffdede70df4a6f25a93cf6a030f52a034d989504c9ca488067115c748536420997dcc78b601e7d832fd80501687bdad1d54768d097060c57
data/Gemfile.lock CHANGED
@@ -3,7 +3,7 @@ GEM
3
3
  specs:
4
4
  addressable (2.3.5)
5
5
  awesome_print (1.2.0)
6
- bindata (1.8.0)
6
+ bindata (2.1.0)
7
7
  builder (3.2.2)
8
8
  diff-lcs (1.1.3)
9
9
  docile (1.1.1)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 7.0.0
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: ip2location_ruby 0.3.2 ruby lib
5
+ # stub: ip2location_ruby 7.0.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "ip2location_ruby"
9
- s.version = "0.3.2"
9
+ s.version = "7.0.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Vincent.Z"]
14
- s.date = "2014-07-04"
14
+ s.date = "2014-08-13"
15
15
  s.description = "the ip2location ruby library"
16
16
  s.email = "zhu1230@gmail.com"
17
17
  s.extra_rdoc_files = [
@@ -35,13 +35,15 @@ Gem::Specification.new do |s|
35
35
  "lib/ip2location_ruby/i2l_string_data.rb",
36
36
  "lib/ip2location_ruby/ip2location_config.rb",
37
37
  "lib/ip2location_ruby/ip2location_record.rb",
38
+ "spec/assets/DB24.DEMO4.BIN",
39
+ "spec/assets/DB24.DEMO6.BIN",
38
40
  "spec/assets/IP-COUNTRY-SAMPLE.BIN",
39
41
  "spec/ip2location_ruby_spec.rb",
40
42
  "spec/spec_helper.rb"
41
43
  ]
42
44
  s.homepage = "http://github.com/zhu1230/ip2location"
43
45
  s.licenses = ["MIT"]
44
- s.rubygems_version = "2.2.2"
46
+ s.rubygems_version = "2.4.1"
45
47
  s.summary = "the ip2location ruby library"
46
48
 
47
49
  if s.respond_to? :specification_version then
@@ -9,50 +9,47 @@ require 'ip2location_ruby/i2l_ip_data'
9
9
  require 'ip2location_ruby/ip2location_record'
10
10
 
11
11
  class Ip2location
12
- attr_accessor :record_class, :v4, :file, :db_index, :count, :base_addr, :ipno, :count, :record, :database, :columns, :ip_version
12
+ attr_accessor :record_class, :v4, :file, :db_index, :count, :base_addr, :ipno, :count, :record, :database, :columns, :ip_version, :ipv4databasecount, :ipv4databaseaddr, :ipv6databasecount, :ipv6databaseaddr
13
13
 
14
14
  def open(url)
15
15
  self.file = File.open(File.expand_path url, 'rb')
16
16
  i2l = Ip2locationConfig.read(file)
17
17
  self.db_index = i2l.databasetype
18
- self.count = i2l.databasecount + 0
19
- self.base_addr = i2l.databaseaddr - 1
20
18
  self.columns = i2l.databasecolumn + 0
21
19
  self.database = DbConfig.setup_database(self.db_index)
22
- self.ip_version = (i2l.ipversion == 0 ? 4 : 6)
23
- self.record_class = (Ip2LocationRecord.init database, self.ip_version)
20
+ self.ipv4databasecount = i2l.ipv4databasecount
21
+ self.ipv4databaseaddr = i2l.ipv4databaseaddr
22
+ self.ipv6databasecount = i2l.ipv6databasecount
23
+ self.ipv6databaseaddr = i2l.ipv6databaseaddr
24
24
  self
25
25
  end
26
26
 
27
27
  def get_all(ip)
28
28
  ipno = IPAddr.new(ip, Socket::AF_UNSPEC)
29
- self.v4 = ipno.ipv4? && self.ip_version == 4
29
+ self.ip_version = ipno.ipv4? ? 4 : 6
30
+ self.v4 = ipno.ipv4?
31
+ self.count = ipno.ipv4? ? self.ipv4databasecount + 0 : self.ipv6databasecount + 0
32
+ self.base_addr = (ipno.ipv4? ? self.ipv4databaseaddr - 1 : self.ipv6databaseaddr - 1)
33
+ self.record_class = (Ip2LocationRecord.init database, self.ip_version)
34
+
30
35
  ipnum = ipno.to_i + 0
31
- mid = self.count/2
32
36
  col_length = columns * 4
33
- low = 0
34
- high = count
35
- return self.record = bsearch(low, high, ipnum, self.base_addr, col_length)
37
+
38
+ return self.record = bsearch(0, self.count, ipnum, self.base_addr, col_length)
36
39
  end
37
40
 
38
41
  def get_from_to(mid, base_addr, col_length)
39
- if v4
40
- from_base = ( base_addr + mid * col_length)
41
- else
42
- from_base = ( base_addr + mid * (col_length + 12))
43
- end
44
-
42
+ from_base = ( base_addr + mid * (col_length + (v4 ? 0 : 12)))
45
43
  file.seek(from_base)
46
- ip_from = file.read(4).unpack('L')[0]
47
- file.seek(from_base + col_length)
48
- ip_to = file.read(4).unpack('L')[0]
44
+ ip_from = v4 ? BinData::Uint32le.read(file) : BinData::Uint128le.read(file)
45
+ file.seek(from_base + col_length + (v4 ? 0 : 12))
46
+ ip_to = v4 ? BinData::Uint32le.read(file) : BinData::Uint128le.read(file)
49
47
  [ip_from, ip_to]
50
48
  end
51
49
 
52
50
  def bsearch(low, high, ipnum, base_addr, col_length)
53
51
  mid = (high + low)/2
54
52
  return nil if low == mid
55
-
56
53
  ip_from, ip_to = get_from_to(mid, base_addr, col_length)
57
54
  if ipnum < ip_from
58
55
  low = low
@@ -63,7 +60,7 @@ class Ip2location
63
60
  high = high
64
61
  return bsearch(low, high, ipnum, base_addr, col_length)
65
62
  else
66
- from_base = ( base_addr + mid * col_length)
63
+ from_base = ( base_addr + mid * (col_length + (self.v4 ? 0 : 12)))
67
64
  file.seek(from_base)
68
65
  return self.record_class.read(file)
69
66
  end
@@ -5,7 +5,11 @@ class Ip2locationConfig < BinData::Record
5
5
  uint8 :databaseday
6
6
  uint8 :databasemonth
7
7
  uint8 :databaseyear
8
- uint32 :databasecount
9
- uint32 :databaseaddr
10
- uint32 :ipversion
8
+ # uint32 :databasecount
9
+ # uint32 :databaseaddr
10
+ # uint32 :ipversion
11
+ uint32 :ipv4databasecount
12
+ uint32 :ipv4databaseaddr
13
+ uint32 :ipv6databasecount
14
+ uint32 :ipv6databaseaddr
11
15
  end
@@ -4,9 +4,8 @@ class Ip2LocationRecord
4
4
  cls.class_eval {
5
5
  endian :little
6
6
  i2l_ip_data :ip_from, :ip_version => ip_version
7
-
8
7
  database.each do |col|
9
- if [:latitude, :longitude,:elevation].include? col.first
8
+ if [:latitude, :longitude].include? col.first
10
9
  i2l_float_data col.first
11
10
  elsif col.first == :country
12
11
  i2l_string_data :country_short
Binary file
Binary file
@@ -8,6 +8,41 @@ describe "Ip2location" do
8
8
  record.country_short.should == 'US'
9
9
  end
10
10
 
11
+ it "work correctly with ipv4 with new library" do
12
+ i2l = Ip2location.new.open(File.dirname(__FILE__) + "/assets/DB24.DEMO4.bin")
13
+ record = i2l.get_all('13.5.10.6')
14
+ record.should_not be_nil
15
+ record.country_short.should == 'KR'
16
+ end
17
+
18
+ it "work correctly with ipv6 with new library" do
19
+ i2l = Ip2location.new.open(File.dirname(__FILE__) + "/assets/DB24.DEMO6.bin")
20
+ record = i2l.get_all('2a01:04f8:0d16:26c2::')
21
+ record.should_not be_nil
22
+ record.country_short.should == 'DE'
23
+ end
24
+
25
+ # it "profile ipv6 in IPV6.BIN" do
26
+ # i2l = Ip2location.new.open(File.dirname(__FILE__) + "/assets/IPV6-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN")
27
+ # File.new(File.dirname(__FILE__) + "/assets/ipv6_ip.txt").readlines.each do |line|
28
+ # record = i2l.get_all(line.split(' ').first)
29
+ # end
30
+ # end
31
+
32
+ # it "profile ipv4 in IPV6.BIN" do
33
+ # i2l = Ip2location.new.open(File.dirname(__FILE__) + "/assets/IPV6-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN")
34
+ # File.new(File.dirname(__FILE__) + "/assets/ipv4_ip.txt").readlines.each do |line|
35
+ # record = i2l.get_all(line.split(' ').first)
36
+ # end
37
+ # end
38
+
39
+ # it "profile ipv4 in IPV4.BIN" do
40
+ # i2l = Ip2location.new.open(File.dirname(__FILE__) + "/assets/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN")
41
+ # File.new(File.dirname(__FILE__) + "/assets/ipv4_ip.txt").readlines.each do |line|
42
+ # record = i2l.get_all(line.split(' ').first)
43
+ # end
44
+ # end
45
+
11
46
  # this need spec/assets/IP2LOCATION-LITE-DB5.bin file which too big to include in this test.
12
47
 
13
48
  # it "should get float value of attitude" do
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: 0.3.2
4
+ version: 7.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vincent.Z
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-04 00:00:00.000000000 Z
11
+ date: 2014-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bindata
@@ -132,6 +132,8 @@ files:
132
132
  - lib/ip2location_ruby/i2l_string_data.rb
133
133
  - lib/ip2location_ruby/ip2location_config.rb
134
134
  - lib/ip2location_ruby/ip2location_record.rb
135
+ - spec/assets/DB24.DEMO4.BIN
136
+ - spec/assets/DB24.DEMO6.BIN
135
137
  - spec/assets/IP-COUNTRY-SAMPLE.BIN
136
138
  - spec/ip2location_ruby_spec.rb
137
139
  - spec/spec_helper.rb
@@ -155,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
155
157
  version: '0'
156
158
  requirements: []
157
159
  rubyforge_project:
158
- rubygems_version: 2.2.2
160
+ rubygems_version: 2.4.1
159
161
  signing_key:
160
162
  specification_version: 4
161
163
  summary: the ip2location ruby library