ipcat 2.0.6 → 2.0.7
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 +4 -4
- data/Rakefile +15 -16
- data/data/datacenters +0 -0
- data/lib/ipcat.rb +10 -9
- data/lib/ipcat/iprange.rb +7 -4
- data/lib/ipcat/version.rb +3 -1
- data/spec/benchmark_spec.rb +9 -10
- data/spec/ipcat_iprange_spec.rb +8 -7
- data/spec/ipcat_spec.rb +8 -8
- data/spec/spec_helper.rb +7 -5
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 504383114dd0844a4a5f91ebb4afa296fe098003
|
4
|
+
data.tar.gz: f818cb7072a8c8882198751871283a91ddc1cdca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1aed19167372bb4b8d18e035783ee8d0130c2ed50791e708cec1add11310825a18a215f009ef1ecbef20201001799684a4a07f153ba0111e9fd6b20d2f073d2c
|
7
|
+
data.tar.gz: 981cb897772de0aa94732fc3caf968b289879e2b82016e7a9ef350b0b9eed103b33fd5f8fbf2e9dce13afca439a940e5a63cdc43ce02494e2afc051927ffb7e2
|
data/Rakefile
CHANGED
@@ -1,35 +1,34 @@
|
|
1
|
-
|
2
|
-
require "bundler/setup"
|
3
|
-
require "bundler/gem_tasks"
|
4
|
-
require "rake/testtask"
|
1
|
+
# frozen_string_literal: true
|
5
2
|
|
6
|
-
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'bundler/gem_tasks'
|
6
|
+
require 'rake/testtask'
|
7
|
+
|
8
|
+
$LOAD_PATH.unshift './lib'
|
7
9
|
require 'ipcat'
|
8
10
|
Rake::TestTask.new do |t|
|
9
|
-
t.pattern =
|
11
|
+
t.pattern = 'spec/*_spec.rb'
|
10
12
|
end
|
11
13
|
|
12
|
-
task :
|
14
|
+
task default: :test
|
13
15
|
|
14
16
|
desc 'Regenerate data/datacenters'
|
15
17
|
task :generate_dataset do
|
16
|
-
|
17
|
-
$:.unshift './lib'
|
18
|
+
$LOAD_PATH.unshift './lib'
|
18
19
|
require 'ipcat'
|
19
20
|
IPCat.load_csv!
|
20
|
-
File.open(
|
21
|
+
File.open('data/datacenters', 'w') { |f| f << Marshal.dump(IPCat.ranges) }
|
21
22
|
end
|
22
23
|
|
23
|
-
desc
|
24
|
+
desc 'Run benchmark'
|
24
25
|
task :bench do
|
25
|
-
|
26
26
|
require 'benchmark'
|
27
27
|
# random ips
|
28
|
-
ips = 100_000
|
28
|
+
ips = Array.new(100_000) { rand(2**32) }
|
29
29
|
Benchmark.bm do |x|
|
30
|
-
x.report(
|
30
|
+
x.report('IPCat.bsearch (100k)') do
|
31
31
|
ips.each { |ip| IPCat.bsearch(ip) }
|
32
|
-
|
33
|
-
|
32
|
+
end
|
34
33
|
end
|
35
34
|
end
|
data/data/datacenters
CHANGED
Binary file
|
data/lib/ipcat.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
##
|
2
4
|
# IPCat
|
3
5
|
# Ruby lib for https://github.com/client9/ipcat/
|
@@ -6,13 +8,12 @@ require 'ipaddr'
|
|
6
8
|
require 'ipcat/iprange'
|
7
9
|
require 'ipcat/version'
|
8
10
|
|
9
|
-
|
10
11
|
class IPCat
|
11
12
|
class << self
|
12
13
|
def datacenter?(ip)
|
13
14
|
bsearch(ip_to_integer(ip))
|
14
15
|
end
|
15
|
-
|
16
|
+
alias classify datacenter?
|
16
17
|
|
17
18
|
def ip_to_integer(ip)
|
18
19
|
Integer === ip ? ip : IPAddr.new(ip).to_i
|
@@ -26,15 +27,15 @@ class IPCat
|
|
26
27
|
@ranges = new_ranges
|
27
28
|
end
|
28
29
|
|
29
|
-
def load_csv!(path='https://raw.github.com/client9/ipcat/master/datacenters.csv')
|
30
|
+
def load_csv!(path = 'https://raw.github.com/client9/ipcat/master/datacenters.csv')
|
30
31
|
reset_ranges!
|
31
32
|
|
32
33
|
require 'open-uri'
|
33
34
|
open(path).readlines.each do |line|
|
34
35
|
first, last, name, url = line.split(',')
|
35
|
-
|
36
|
+
ranges << IPRange.new(first, last, name, url).freeze
|
36
37
|
end
|
37
|
-
|
38
|
+
ranges.freeze
|
38
39
|
end
|
39
40
|
|
40
41
|
def load!
|
@@ -50,15 +51,15 @@ class IPCat
|
|
50
51
|
end
|
51
52
|
|
52
53
|
# Assume ranges is an array of comparable objects
|
53
|
-
def bsearch(needle, haystack=ranges, first=0, last=ranges.size-1)
|
54
|
+
def bsearch(needle, haystack = ranges, first = 0, last = ranges.size - 1)
|
54
55
|
return nil if last < first # not found, or empty range
|
55
56
|
|
56
|
-
cur = first + (last - first)/2
|
57
|
+
cur = first + (last - first) / 2
|
57
58
|
case haystack[cur] <=> needle
|
58
59
|
when -1 # needle is larger than cur value
|
59
|
-
bsearch(needle, haystack, cur+1, last)
|
60
|
+
bsearch(needle, haystack, cur + 1, last)
|
60
61
|
when 1 # needle is smaller than cur value
|
61
|
-
bsearch(needle, haystack, first, cur-1)
|
62
|
+
bsearch(needle, haystack, first, cur - 1)
|
62
63
|
when 0
|
63
64
|
haystack[cur]
|
64
65
|
end
|
data/lib/ipcat/iprange.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class IPCat
|
2
4
|
class IPRange
|
3
|
-
|
4
5
|
attr_accessor :first, :last, :name, :url
|
5
6
|
|
6
|
-
def initialize(first, last, name=nil, url=nil)
|
7
|
+
def initialize(first, last, name = nil, url = nil)
|
7
8
|
@first = IPCat.ip_to_integer(first)
|
8
9
|
@last = IPCat.ip_to_integer(last)
|
9
|
-
@name
|
10
|
-
|
10
|
+
@name = name
|
11
|
+
@url = url
|
12
|
+
raise ArgumentError, 'first must be <= last' if @first > @last
|
11
13
|
end
|
12
14
|
|
13
15
|
def <=>(obj)
|
@@ -21,6 +23,7 @@ class IPCat
|
|
21
23
|
end
|
22
24
|
|
23
25
|
protected
|
26
|
+
|
24
27
|
def compare_with_integer(i)
|
25
28
|
if first > i
|
26
29
|
1
|
data/lib/ipcat/version.rb
CHANGED
data/spec/benchmark_spec.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'spec_helper'
|
2
4
|
require 'minitest/benchmark'
|
3
5
|
|
4
6
|
describe 'IPCat.bsearch Benchmark' do
|
5
|
-
|
6
7
|
# Makes +n+ IPRanges
|
7
8
|
def make_ranges(n)
|
8
|
-
ips = (n*2)
|
9
|
+
ips = Array.new((n * 2)) { rand(2**32) }.sort
|
9
10
|
ranges = []
|
10
11
|
ips.each_slice(2) do |first, last|
|
11
12
|
ranges << IPCat::IPRange.new(first, last)
|
@@ -15,20 +16,18 @@ describe 'IPCat.bsearch Benchmark' do
|
|
15
16
|
end
|
16
17
|
|
17
18
|
before do
|
18
|
-
@ips = 1000
|
19
|
-
@ranges = bench_range.
|
19
|
+
@ips = Array.new(1000) { rand(2**32) }
|
20
|
+
@ranges = bench_range.each_with_object({}) do |n, h|
|
20
21
|
h[n] = make_ranges(n)
|
21
|
-
|
22
|
-
}
|
22
|
+
end
|
23
23
|
end
|
24
24
|
|
25
|
-
it(
|
25
|
+
it('should be logarithmic') do
|
26
26
|
assert_performance_logarithmic 0.95 do |n|
|
27
27
|
IPCat.reset_ranges!(@ranges[n])
|
28
|
-
@ips.each
|
28
|
+
@ips.each do |ip|
|
29
29
|
10.times { IPCat.bsearch(ip) }
|
30
|
-
|
30
|
+
end
|
31
31
|
end
|
32
32
|
end
|
33
|
-
|
34
33
|
end
|
data/spec/ipcat_iprange_spec.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'spec_helper'
|
2
4
|
|
3
5
|
describe 'IPCat::IPRange' do
|
@@ -11,16 +13,15 @@ describe 'IPCat::IPRange' do
|
|
11
13
|
end
|
12
14
|
|
13
15
|
describe '#initialize' do
|
14
|
-
it
|
15
|
-
->{ IPCat::IPRange.new(2, 1) }.must_raise ArgumentError
|
16
|
+
it 'should fail if last < first' do
|
17
|
+
-> { IPCat::IPRange.new(2, 1) }.must_raise ArgumentError
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
21
|
describe '#<=> for integers' do
|
20
|
-
it(
|
21
|
-
it(
|
22
|
-
it(
|
23
|
-
it(
|
22
|
+
it('should match first') { (range <=> range.first).must_equal 0 }
|
23
|
+
it('should match last') { (range <=> range.last).must_equal 0 }
|
24
|
+
it('should match first-1') { (range <=> range.first - 1).must_equal 1 }
|
25
|
+
it('should match last+1') { (range <=> range.last + 1).must_equal(-1) }
|
24
26
|
end
|
25
27
|
end
|
26
|
-
|
data/spec/ipcat_spec.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'spec_helper'
|
2
4
|
|
3
5
|
describe 'IPCat' do
|
4
|
-
|
5
6
|
before do
|
6
7
|
IPCat.reset_ranges!
|
7
8
|
start = IPAddr.new('1.2.3.0').to_i
|
@@ -10,15 +11,14 @@ describe 'IPCat' do
|
|
10
11
|
end
|
11
12
|
|
12
13
|
describe '#ranges' do
|
13
|
-
|
14
|
+
it('has a range') { IPCat.ranges.size.must_equal 1 }
|
14
15
|
end
|
15
16
|
|
16
17
|
describe '#datacenter?' do
|
17
|
-
it(
|
18
|
-
it(
|
19
|
-
it(
|
20
|
-
it(
|
21
|
-
it(
|
18
|
+
it('should match 1.2.3.0') { IPCat.datacenter?('1.2.3.0').must_be_instance_of IPCat::IPRange }
|
19
|
+
it('should match 1.2.3.1') { IPCat.datacenter?('1.2.3.1').must_be_instance_of IPCat::IPRange }
|
20
|
+
it('should match 1.2.3.1') { IPCat.datacenter?('1.2.3.1').must_be_instance_of IPCat::IPRange }
|
21
|
+
it('should not match 1.1.1.1') { IPCat.datacenter?('1.1.1.1').must_be_nil }
|
22
|
+
it('should not match 2.2.2.2') { IPCat.datacenter?('2.2.2.2').must_be_nil }
|
22
23
|
end
|
23
|
-
|
24
24
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
-
|
2
|
-
require "bundler/setup"
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler/setup'
|
6
5
|
|
7
|
-
require
|
6
|
+
require 'minitest/autorun'
|
7
|
+
require 'minitest/pride'
|
8
|
+
|
9
|
+
require 'ipcat'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ipcat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Suggs
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -74,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
74
|
version: '0'
|
75
75
|
requirements: []
|
76
76
|
rubyforge_project:
|
77
|
-
rubygems_version: 2.
|
77
|
+
rubygems_version: 2.6.11
|
78
78
|
signing_key:
|
79
79
|
specification_version: 4
|
80
80
|
summary: dataset for categorizing IP addresses in ruby
|