ruby-ip 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.rdoc +10 -2
  2. data/Rakefile +6 -1
  3. data/lib/ip/base.rb +24 -4
  4. data/test/ip_test.rb +59 -1
  5. metadata +17 -8
data/README.rdoc CHANGED
@@ -1,6 +1,14 @@
1
- == ruby-ip library
1
+ = ruby-ip library
2
2
 
3
- This is a library for manipulating IP addresses. Feature overview:
3
+ This is a library for manipulating IP addresses.
4
+
5
+ == Installation
6
+
7
+ Gem:: <tt>gem install ruby-ip</tt>
8
+ Source:: <tt>git clone git://github.com/deploy2/ruby-ip.git</tt>
9
+ Docs:: http://deploy2.github.com/ruby-ip/
10
+
11
+ == Feature overview
4
12
 
5
13
  * Create from string and to string
6
14
 
data/Rakefile CHANGED
@@ -1,5 +1,10 @@
1
1
  require 'rake/clean'
2
2
 
3
+ task :gem => :build
4
+ task :build do
5
+ system "gem build ruby-ip.gemspec"
6
+ end
7
+
3
8
  #### TESTING ####
4
9
  require 'rake/testtask'
5
10
  task :default => :test
@@ -24,7 +29,7 @@ rescue LoadError
24
29
  end
25
30
 
26
31
  #### DOCUMENTATION ####
27
- require 'rake/rdoctask'
32
+ require 'rdoc/task'
28
33
  Rake::RDocTask.new { |rdoc|
29
34
  rdoc.rdoc_dir = 'doc/rdoc'
30
35
  rdoc.template = ENV['template'] if ENV['template']
data/lib/ip/base.rb CHANGED
@@ -175,12 +175,19 @@ class IP
175
175
  self
176
176
  end
177
177
 
178
- def to_range
178
+ def to_irange
179
179
  a1 = @addr & ~mask
180
180
  a2 = a1 | mask
181
181
  (a1..a2)
182
182
  end
183
183
 
184
+ # QUERY: IPAddr (1.9) turns 1.2.3.0/24 into 1.2.3.0/24..1.2.3.255/24
185
+ # Here I turn it into 1.2.3.0..1.2.3.255. Which is better?
186
+ def to_range
187
+ self.class.new(@addr & ~mask, self.class::ADDR_BITS, @ctx) ..
188
+ self.class.new(@addr | mask, self.class::ADDR_BITS, @ctx)
189
+ end
190
+
184
191
  # The number of IP addresses in subnet
185
192
  # IP.new("1.2.3.4/24").size => 256
186
193
  def size
@@ -211,8 +218,12 @@ class IP
211
218
  self.class.new(~@addr & self.class::MASK, @pfxlen, @ctx)
212
219
  end
213
220
 
221
+ def succ
222
+ self.class.new(@addr + size, @pfxlen, @ctx)
223
+ end
224
+
214
225
  def succ!
215
- @addr += 1
226
+ @addr += size
216
227
  self
217
228
  end
218
229
 
@@ -236,6 +247,11 @@ class IP
236
247
  to_a.hash
237
248
  end
238
249
 
250
+ def freeze
251
+ mask
252
+ super
253
+ end
254
+
239
255
  def eql?(other)
240
256
  to_a.eql?(other.to_a)
241
257
  end
@@ -328,11 +344,15 @@ class IP
328
344
  "::#{native.to_addr}"
329
345
  elsif ipv4_mapped?
330
346
  "::ffff:#{native.to_addr}"
347
+ elsif @addr.zero?
348
+ "::"
331
349
  else
332
350
  res = to_hex.scan(/..../).join(':')
333
351
  res.gsub!(/\b0{1,3}/,'')
334
- res.gsub!(/(\A|:)(0:)+/,'::')
335
- res.gsub!(/::0\z/, '::')
352
+ res.sub!(/\b0:0:0:0(:0)*\b/,':') ||
353
+ res.sub!(/\b0:0:0\b/,':') ||
354
+ res.sub!(/\b0:0\b/,':')
355
+ res.sub!(/:::+/,'::')
336
356
  res
337
357
  end
338
358
  end
data/test/ip_test.rb CHANGED
@@ -121,8 +121,12 @@ class IPTest < Test::Unit::TestCase
121
121
  assert_equal "v4", @addr.proto
122
122
  end
123
123
 
124
+ should "have to_irange" do
125
+ assert_equal((0x01020300 .. 0x010203ff), @addr.to_irange)
126
+ end
127
+
124
128
  should "have to_range" do
125
- assert_equal((0x01020300 .. 0x010203ff), @addr.to_range)
129
+ assert_equal IP.new("1.2.3.0@foo")..IP.new("1.2.3.255@foo"), @addr.to_range
126
130
  end
127
131
 
128
132
  should "have size" do
@@ -419,6 +423,11 @@ class IPTest < Test::Unit::TestCase
419
423
  end
420
424
  end
421
425
 
426
+ should "convert v6 addresses unambiguously" do
427
+ assert_equal "1:0:2::", IP.new('1:0:2:0:0:0:0:0').to_s
428
+ assert_equal "1::2:0", IP.new('1:0:0:0:0:0:2:0').to_s
429
+ end
430
+
422
431
  context "comparing" do
423
432
  setup do
424
433
  @a1 = IP.new("1.2.3.4")
@@ -467,6 +476,55 @@ class IPTest < Test::Unit::TestCase
467
476
  end
468
477
  end
469
478
 
479
+ context "freezing" do
480
+ setup do
481
+ @addr = IP.new('1.2.3.4/24@foo').freeze
482
+ end
483
+
484
+ should "be able to get values without a TypeError" do
485
+ assert_nothing_raised(TypeError) do
486
+ @addr.to_s
487
+ @addr.to_addrlen
488
+ @addr.to_addr
489
+ @addr.to_i
490
+ @addr.to_a
491
+ @addr.to_ah
492
+ @addr.to_hex
493
+ @addr.pfxlen
494
+ @addr.proto
495
+ @addr.to_irange
496
+ @addr.to_range
497
+ @addr.size
498
+ @addr.ctx
499
+ @addr.network
500
+ @addr.broadcast
501
+ @addr.mask
502
+ @addr.netmask
503
+ @addr.wildmask
504
+ @addr.offset
505
+ end
506
+ end
507
+ end
508
+
509
+ context "range between two IPs" do
510
+ should "be able to iterate" do
511
+ r = IP.new("10.0.0.6")..IP.new("10.0.0.8")
512
+ a = []
513
+ r.each { |x| a << x.to_s }
514
+ assert_equal ["10.0.0.6","10.0.0.7","10.0.0.8"], a
515
+ end
516
+
517
+ should "iterate when prefix present" do
518
+ # Spec question: should this increment in blocks of /30 or single IPs?
519
+ # Iteration of single IPs is not really useful for v6; but then again,
520
+ # having an off-base start IP isn't really useful either.
521
+ r = IP.new("10.0.0.6/30")..IP.new("10.0.0.11/29")
522
+ a = []
523
+ r.each { |x| a << x.to_s }
524
+ assert_equal ["10.0.0.6/30","10.0.0.10/30"], a
525
+ end
526
+ end
527
+
470
528
  PARSE_TESTS = [
471
529
  # ipv4
472
530
  [["v4", 0x01020304, 32], "1.2.3.4"],
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-ip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 9
8
+ - 1
9
+ version: 0.9.1
5
10
  platform: ruby
6
11
  authors:
7
12
  - Brian Candler
@@ -9,7 +14,7 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-01-26 00:00:00 +00:00
17
+ date: 2011-12-13 00:00:00 +00:00
13
18
  default_executable:
14
19
  dependencies: []
15
20
 
@@ -22,12 +27,12 @@ extensions: []
22
27
  extra_rdoc_files:
23
28
  - README.rdoc
24
29
  files:
25
- - lib/ip/cpal.rb
26
30
  - lib/ip/base.rb
31
+ - lib/ip/cpal.rb
27
32
  - lib/ip/socket.rb
28
33
  - lib/ip.rb
29
- - test/ip_test.rb
30
34
  - test/ip_cpal_test.rb
35
+ - test/ip_test.rb
31
36
  - test/test_helper.rb
32
37
  - README.rdoc
33
38
  - Rakefile
@@ -47,18 +52,22 @@ required_ruby_version: !ruby/object:Gem::Requirement
47
52
  requirements:
48
53
  - - ">="
49
54
  - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
50
57
  version: "0"
51
- version:
52
58
  required_rubygems_version: !ruby/object:Gem::Requirement
53
59
  requirements:
54
60
  - - ">="
55
61
  - !ruby/object:Gem::Version
56
- version: "0"
57
- version:
62
+ segments:
63
+ - 1
64
+ - 3
65
+ - 6
66
+ version: 1.3.6
58
67
  requirements: []
59
68
 
60
69
  rubyforge_project: ruby-ip
61
- rubygems_version: 1.3.5
70
+ rubygems_version: 1.3.6
62
71
  signing_key:
63
72
  specification_version: 2
64
73
  summary: IP address manipulation library