uuidtools 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ == UUIDTools 2.1.0
2
+ * completely reworked the MAC address detection code
3
+ * added additional parsing methods
1
4
  == UUIDTools 2.0.0
2
5
  * moved to its own module to avoid collisions
3
6
  == UUIDTools 1.0.7
data/README CHANGED
@@ -3,11 +3,11 @@
3
3
  possible.
4
4
 
5
5
  == Example
6
- UUID.md5_create(UUID_DNS_NAMESPACE, "www.widgets.com")
6
+ UUIDTools::UUID.md5_create(UUIDTools::UUID_DNS_NAMESPACE, "www.widgets.com")
7
7
  => #<UUID:0x287576 UUID:3d813cbb-47fb-32ba-91df-831e1593ac29>
8
- UUID.sha1_create(UUID_DNS_NAMESPACE, "www.widgets.com")
8
+ UUIDTools::UUID.sha1_create(UUIDTools::UUID_DNS_NAMESPACE, "www.widgets.com")
9
9
  => #<UUID:0x2a0116 UUID:21f7f8de-8051-5b89-8680-0195ef798b6a>
10
- UUID.timestamp_create
10
+ UUIDTools::UUID.timestamp_create
11
11
  => #<UUID:0x2adfdc UUID:64a5189c-25b3-11da-a97b-00c04fd430c8>
12
- UUID.random_create
12
+ UUIDTools::UUID.random_create
13
13
  => #<UUID:0x19013a UUID:984265dc-4200-4f02-ae70-fe4f48964159>
data/lib/uuidtools.rb CHANGED
@@ -161,6 +161,24 @@ module UUIDTools
161
161
  clock_seq_hi_and_reserved, clock_seq_low, nodes)
162
162
  end
163
163
 
164
+ # Parses a UUID from an Integer.
165
+ def self.parse_int(uuid_int)
166
+ unless uuid_int.kind_of?(Integer)
167
+ raise ArgumentError,
168
+ "Expected Integer, got #{uuid_int.class.name} instead."
169
+ end
170
+ return self.parse_raw(self.convert_int_to_byte_string(uuid_int, 16))
171
+ end
172
+
173
+ # Parse a UUID from a hexdigest String.
174
+ def self.parse_hexdigest(uuid_hexdigest)
175
+ unless uuid_hexdigest.kind_of?(String)
176
+ raise ArgumentError,
177
+ "Expected String, got #{uuid_hexdigest.class.name} instead."
178
+ end
179
+ return self.parse_int(uuid_hexdigest.to_i(16))
180
+ end
181
+
164
182
  # Creates a UUID from a random value.
165
183
  def self.random_create()
166
184
  new_uuid = self.parse_raw(SecureRandom.random_bytes(16))
@@ -192,8 +210,7 @@ module UUIDTools
192
210
  octet.to_i(16)
193
211
  end
194
212
  else
195
- nodes = []
196
- SecureRandom.random_bytes(6).each_byte { |chr| nodes << chr }
213
+ nodes = SecureRandom.random_bytes(6).unpack("C*")
197
214
  nodes[0] |= 0b00000001
198
215
  end
199
216
  for i in 0..5
@@ -412,13 +429,36 @@ module UUIDTools
412
429
  if !defined?(@@mac_address)
413
430
  require 'rbconfig'
414
431
  os_platform = Config::CONFIG['target_os']
415
- if (os_platform =~ /win/ && !(os_platform =~ /darwin/)) ||
416
- os_platform =~ /w32/
432
+ os_class = nil
433
+ if (os_platform =~ /win/i && !(os_platform =~ /darwin/i)) ||
434
+ os_platform =~ /w32/i
435
+ os_class = :windows
436
+ elsif os_platform =~ /solaris/i
437
+ os_class = :solaris
438
+ elsif os_platform =~ /netbsd/i
439
+ os_class = :netbsd
440
+ elsif os_platform =~ /openbsd/i
441
+ os_class = :openbsd
442
+ end
443
+ mac_regexps = [
444
+ Regexp.new("address:? (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
445
+ Regexp.new("addr:? (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
446
+ Regexp.new("ether:? (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
447
+ Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
448
+ Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join("-")})")
449
+ ]
450
+ parse_mac = lambda do |output|
451
+ (mac_regexps.map do |regexp|
452
+ result = output[regexp, 1]
453
+ result.downcase.gsub(/-/, ":") if result != nil
454
+ end).compact.first
455
+ end
456
+ if os_class == :windows
417
457
  script_in_path = true
418
458
  else
419
459
  script_in_path = Kernel.system("which ifconfig 2>&1 > /dev/null")
420
460
  end
421
- if os_platform =~ /solaris/
461
+ if os_class == :solaris
422
462
  begin
423
463
  ifconfig_output =
424
464
  (script_in_path ? `ifconfig -a` : `/sbin/ifconfig -a`)
@@ -439,74 +479,56 @@ module UUIDTools
439
479
  rescue Exception
440
480
  end
441
481
  end
442
- elsif os_platform =~ /win/ && !(os_platform =~ /darwin/)
482
+ elsif os_class == :windows
443
483
  begin
444
- ifconfig_output = `ipconfig /all`
445
- mac_addresses = ifconfig_output.scan(
446
- Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join("-")})"))
447
- if mac_addresses.size > 0
448
- @@mac_address = mac_addresses.first.first.downcase.gsub(/-/, ":")
449
- end
484
+ @@mac_address = parse_mac.call(`ipconfig /all`)
450
485
  rescue
451
486
  end
452
487
  else
453
488
  begin
454
- mac_addresses = []
455
- if os_platform =~ /netbsd/
456
- ifconfig_output =
457
- (script_in_path ? `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`)
458
- mac_addresses = ifconfig_output.scan(
459
- Regexp.new("address\: (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
460
- elsif os_platform =~ /openbsd/
461
- ifconfig_output = `/sbin/ifconfig -a 2>&1`
462
- ifconfig_output =
463
- (script_in_path ? `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`)
464
- mac_addresses = ifconfig_output.scan(
465
- Regexp.new("addr (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
489
+ if os_class == :netbsd
490
+ @@mac_address = parse_mac.call(
491
+ script_in_path ? `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`
492
+ )
493
+ elsif os_class == :openbsd
494
+ @@mac_address = parse_mac.call(
495
+ script_in_path ? `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`
496
+ )
466
497
  elsif File.exists?('/sbin/ifconfig')
467
- ifconfig_output =
468
- (script_in_path ? `ifconfig 2>&1` : `/sbin/ifconfig 2>&1`)
469
- mac_addresses = ifconfig_output.scan(
470
- Regexp.new("ether (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
471
- if mac_addresses.size == 0
472
- ifconfig_output =
473
- (script_in_path ?
474
- `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`)
475
- mac_addresses = ifconfig_output.scan(
476
- Regexp.new("ether (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
498
+ @@mac_address = parse_mac.call(
499
+ script_in_path ? `ifconfig 2>&1` : `/sbin/ifconfig 2>&1`
500
+ )
501
+ if @@mac_address == nil
502
+ @@mac_address = parse_mac.call(
503
+ script_in_path ?
504
+ `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`
505
+ )
477
506
  end
478
- if mac_addresses.size == 0
479
- ifconfig_output =
480
- (script_in_path ?
507
+ if @@mac_address == nil
508
+ @@mac_address = parse_mac.call(
509
+ script_in_path ?
481
510
  `ifconfig | grep HWaddr | cut -c39- 2>&1` :
482
- `/sbin/ifconfig | grep HWaddr | cut -c39- 2>&1`)
483
- mac_addresses = ifconfig_output.scan(
484
- Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
511
+ `/sbin/ifconfig | grep HWaddr | cut -c39- 2>&1`
512
+ )
485
513
  end
486
514
  else
487
- ifconfig_output =
488
- (script_in_path ? `ifconfig 2>&1` : `/sbin/ifconfig 2>&1`)
489
- mac_addresses = ifconfig_output.scan(
490
- Regexp.new("ether (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
491
- if mac_addresses.size == 0
492
- ifconfig_output =
493
- (script_in_path ?
494
- `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`)
495
- mac_addresses = ifconfig_output.scan(
496
- Regexp.new("ether (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
515
+ @@mac_address = parse_mac.call(
516
+ script_in_path ? `ifconfig 2>&1` : `/sbin/ifconfig 2>&1`
517
+ )
518
+ if @@mac_address == nil
519
+ @@mac_address = parse_mac.call(
520
+ script_in_path ?
521
+ `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`
522
+ )
497
523
  end
498
- if mac_addresses.size == 0
499
- ifconfig_output =
500
- (script_in_path ?
524
+ if @@mac_address == nil
525
+ @@mac_address = parse_mac.call(
526
+ script_in_path ?
501
527
  `ifconfig | grep HWaddr | cut -c39- 2>&1` :
502
- `/sbin/ifconfig | grep HWaddr | cut -c39- 2>&1`)
503
- mac_addresses = ifconfig_output.scan(
504
- Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
528
+ `/sbin/ifconfig | grep HWaddr | cut -c39- 2>&1`
529
+ )
505
530
  end
506
531
  end
507
- if mac_addresses.size > 0
508
- @@mac_address = mac_addresses.first.first
509
- end
510
532
  rescue
511
533
  end
512
534
  end
@@ -26,7 +26,7 @@ unless defined? UUID::VERSION
26
26
  class UUID
27
27
  module VERSION #:nodoc:
28
28
  MAJOR = 2
29
- MINOR = 0
29
+ MINOR = 1
30
30
  TINY = 0
31
31
 
32
32
  STRING = [MAJOR, MINOR, TINY].join('.')
@@ -106,4 +106,22 @@ describe UUIDTools::UUID, "when parsing" do
106
106
  UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])
107
107
  )
108
108
  end
109
+
110
+ it "should correctly parse integers" do
111
+ UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0]).should ==
112
+ UUIDTools::UUID.parse_int(0)
113
+ UUIDTools::UUID.parse_int(0).should be_nil_uuid
114
+ uuid = UUIDTools::UUID.timestamp_create
115
+ UUIDTools::UUID.parse_int(uuid.to_i).should == uuid
116
+ end
117
+
118
+ it "should correctly parse hexdigests" do
119
+ UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0]).should ==
120
+ UUIDTools::UUID.parse_hexdigest("00000000000000000000000000000000")
121
+ UUIDTools::UUID.parse_hexdigest(
122
+ "00000000000000000000000000000000"
123
+ ).should be_nil_uuid
124
+ uuid = UUIDTools::UUID.timestamp_create
125
+ UUIDTools::UUID.parse_hexdigest(uuid.hexdigest).should == uuid
126
+ end
109
127
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uuidtools
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bob Aman
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-02 00:00:00 -04:00
12
+ date: 2009-10-12 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -42,7 +42,9 @@ dependencies:
42
42
  - !ruby/object:Gem::Version
43
43
  version: 0.3.2
44
44
  version:
45
- description: A simple universally unique ID generation library.
45
+ description: |
46
+ A simple universally unique ID generation library.
47
+
46
48
  email: bob@sporkmonger.com
47
49
  executables: []
48
50
 
@@ -51,14 +53,11 @@ extensions: []
51
53
  extra_rdoc_files:
52
54
  - README
53
55
  files:
54
- - lib/compat
55
56
  - lib/compat/securerandom.rb
56
- - lib/uuidtools
57
57
  - lib/uuidtools/version.rb
58
58
  - lib/uuidtools.rb
59
59
  - spec/spec.opts
60
60
  - spec/spec_helper.rb
61
- - spec/uuidtools
62
61
  - spec/uuidtools/mac_address_spec.rb
63
62
  - spec/uuidtools/utility_spec.rb
64
63
  - spec/uuidtools/uuid_creation_spec.rb
@@ -78,6 +77,8 @@ files:
78
77
  - README
79
78
  has_rdoc: true
80
79
  homepage: http://uuidtools.rubyforge.org/
80
+ licenses: []
81
+
81
82
  post_install_message:
82
83
  rdoc_options:
83
84
  - --main
@@ -99,9 +100,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
100
  requirements: []
100
101
 
101
102
  rubyforge_project: uuidtools
102
- rubygems_version: 1.3.1
103
+ rubygems_version: 1.3.4
103
104
  signing_key:
104
- specification_version: 2
105
+ specification_version: 3
105
106
  summary: UUID generator
106
107
  test_files: []
107
108