json-schema 3.0.0 → 4.1.1
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/README.md +15 -1
- data/lib/json-schema/attribute.rb +13 -14
- data/lib/json-schema/attributes/additionalitems.rb +1 -0
- data/lib/json-schema/attributes/additionalproperties.rb +3 -6
- data/lib/json-schema/attributes/allof.rb +6 -4
- data/lib/json-schema/attributes/anyof.rb +2 -2
- data/lib/json-schema/attributes/dependencies.rb +1 -0
- data/lib/json-schema/attributes/disallow.rb +2 -1
- data/lib/json-schema/attributes/enum.rb +2 -2
- data/lib/json-schema/attributes/extends.rb +6 -6
- data/lib/json-schema/attributes/format.rb +2 -1
- data/lib/json-schema/attributes/formats/date.rb +1 -0
- data/lib/json-schema/attributes/formats/date_time.rb +2 -1
- data/lib/json-schema/attributes/formats/date_time_v4.rb +1 -0
- data/lib/json-schema/attributes/formats/uri.rb +1 -0
- data/lib/json-schema/attributes/items.rb +1 -0
- data/lib/json-schema/attributes/limits/numeric.rb +1 -1
- data/lib/json-schema/attributes/maxdecimal.rb +1 -1
- data/lib/json-schema/attributes/not.rb +2 -2
- data/lib/json-schema/attributes/oneof.rb +2 -4
- data/lib/json-schema/attributes/patternproperties.rb +1 -0
- data/lib/json-schema/attributes/properties.rb +7 -7
- data/lib/json-schema/attributes/properties_v4.rb +1 -1
- data/lib/json-schema/attributes/propertynames.rb +23 -0
- data/lib/json-schema/attributes/ref.rb +7 -7
- data/lib/json-schema/attributes/required.rb +3 -2
- data/lib/json-schema/attributes/type.rb +3 -2
- data/lib/json-schema/attributes/type_v4.rb +1 -1
- data/lib/json-schema/errors/validation_error.rb +4 -5
- data/lib/json-schema/schema/reader.rb +2 -0
- data/lib/json-schema/schema/validator.rb +3 -3
- data/lib/json-schema/schema.rb +3 -4
- data/lib/json-schema/util/array_set.rb +1 -1
- data/lib/json-schema/util/uri.rb +7 -7
- data/lib/json-schema/util/uuid.rb +227 -226
- data/lib/json-schema/validator.rb +97 -84
- data/lib/json-schema/validators/draft1.rb +21 -23
- data/lib/json-schema/validators/draft2.rb +22 -24
- data/lib/json-schema/validators/draft3.rb +26 -28
- data/lib/json-schema/validators/draft4.rb +34 -36
- data/lib/json-schema/validators/draft6.rb +36 -37
- data/lib/json-schema/validators/hyper-draft1.rb +2 -3
- data/lib/json-schema/validators/hyper-draft2.rb +2 -3
- data/lib/json-schema/validators/hyper-draft3.rb +2 -3
- data/lib/json-schema/validators/hyper-draft4.rb +2 -3
- data/lib/json-schema/validators/hyper-draft6.rb +2 -3
- data/lib/json-schema.rb +2 -2
- data/resources/draft-06.json +12 -12
- metadata +7 -6
    
        data/lib/json-schema/schema.rb
    CHANGED
    
    | @@ -2,15 +2,14 @@ require 'pathname' | |
| 2 2 |  | 
| 3 3 | 
             
            module JSON
         | 
| 4 4 | 
             
              class Schema
         | 
| 5 | 
            -
             | 
| 6 5 | 
             
                attr_accessor :schema, :uri, :validator
         | 
| 7 6 |  | 
| 8 | 
            -
                def initialize(schema,uri,parent_validator=nil)
         | 
| 7 | 
            +
                def initialize(schema, uri, parent_validator = nil)
         | 
| 9 8 | 
             
                  @schema = schema
         | 
| 10 9 | 
             
                  @uri = uri
         | 
| 11 10 |  | 
| 12 11 | 
             
                  # If there is an ID on this schema, use it to generate the URI
         | 
| 13 | 
            -
                  if @schema['id'] | 
| 12 | 
            +
                  if @schema['id'].is_a?(String)
         | 
| 14 13 | 
             
                    temp_uri = JSON::Util::URI.parse(@schema['id'])
         | 
| 15 14 | 
             
                    if temp_uri.relative?
         | 
| 16 15 | 
             
                      temp_uri = uri.join(temp_uri)
         | 
| @@ -36,7 +35,7 @@ module JSON | |
| 36 35 | 
             
                def self.stringify(schema)
         | 
| 37 36 | 
             
                  case schema
         | 
| 38 37 | 
             
                  when Hash then
         | 
| 39 | 
            -
                     | 
| 38 | 
            +
                    schema.map { |key, value| [key.to_s, stringify(schema[key])] }.to_h
         | 
| 40 39 | 
             
                  when Array then
         | 
| 41 40 | 
             
                    schema.map do |schema_item|
         | 
| 42 41 | 
             
                      stringify(schema_item)
         | 
| @@ -7,7 +7,7 @@ class ArraySet < Array | |
| 7 7 | 
             
              def include?(obj)
         | 
| 8 8 | 
             
                if !defined? @values
         | 
| 9 9 | 
             
                  @values = Set.new
         | 
| 10 | 
            -
                   | 
| 10 | 
            +
                  each { |x| @values << convert_to_float_if_numeric(x) }
         | 
| 11 11 | 
             
                end
         | 
| 12 12 | 
             
                @values.include?(convert_to_float_if_numeric(obj))
         | 
| 13 13 | 
             
              end
         | 
    
        data/lib/json-schema/util/uri.rb
    CHANGED
    
    | @@ -14,7 +14,7 @@ module JSON | |
| 14 14 | 
             
                      # Check for absolute path
         | 
| 15 15 | 
             
                      if normalized_uri.relative?
         | 
| 16 16 | 
             
                        data = normalized_uri
         | 
| 17 | 
            -
                        data = File.join(base_path, data) if data.path[0,1] !=  | 
| 17 | 
            +
                        data = File.join(base_path, data) if data.path[0, 1] != '/'
         | 
| 18 18 | 
             
                        normalized_uri = file_uri(data)
         | 
| 19 19 | 
             
                      end
         | 
| 20 20 | 
             
                      @normalize_cache[uri] = normalized_uri.freeze
         | 
| @@ -42,10 +42,10 @@ module JSON | |
| 42 42 | 
             
                        ref_uri.merge!(base_uri)
         | 
| 43 43 |  | 
| 44 44 | 
             
                        # Check for absolute path
         | 
| 45 | 
            -
                        path, fragment = ref.to_s.split( | 
| 45 | 
            +
                        path, fragment = ref.to_s.split('#')
         | 
| 46 46 | 
             
                        if path.nil? || path == ''
         | 
| 47 47 | 
             
                          ref_uri.path = base_uri.path
         | 
| 48 | 
            -
                        elsif path[0,1] ==  | 
| 48 | 
            +
                        elsif path[0, 1] == '/'
         | 
| 49 49 | 
             
                          ref_uri.path = Pathname.new(path).cleanpath.to_s
         | 
| 50 50 | 
             
                        else
         | 
| 51 51 | 
             
                          ref_uri.join!(path)
         | 
| @@ -54,7 +54,7 @@ module JSON | |
| 54 54 | 
             
                        ref_uri.fragment = fragment
         | 
| 55 55 | 
             
                      end
         | 
| 56 56 |  | 
| 57 | 
            -
                      ref_uri.fragment =  | 
| 57 | 
            +
                      ref_uri.fragment = '' if ref_uri.fragment.nil? || ref_uri.fragment.empty?
         | 
| 58 58 | 
             
                    end
         | 
| 59 59 |  | 
| 60 60 | 
             
                    ref_uri
         | 
| @@ -62,7 +62,7 @@ module JSON | |
| 62 62 |  | 
| 63 63 | 
             
                  def self.parse(uri)
         | 
| 64 64 | 
             
                    if uri.is_a?(Addressable::URI)
         | 
| 65 | 
            -
                       | 
| 65 | 
            +
                      uri.dup
         | 
| 66 66 | 
             
                    else
         | 
| 67 67 | 
             
                      @parse_cache ||= {}
         | 
| 68 68 | 
             
                      parsed_uri = @parse_cache[uri]
         | 
| @@ -73,7 +73,7 @@ module JSON | |
| 73 73 | 
             
                      end
         | 
| 74 74 | 
             
                    end
         | 
| 75 75 | 
             
                  rescue Addressable::URI::InvalidURIError => e
         | 
| 76 | 
            -
                    raise JSON::Schema::UriError | 
| 76 | 
            +
                    raise JSON::Schema::UriError, e.message
         | 
| 77 77 | 
             
                  end
         | 
| 78 78 |  | 
| 79 79 | 
             
                  def self.strip_fragment(uri)
         | 
| @@ -81,7 +81,7 @@ module JSON | |
| 81 81 | 
             
                    if parsed_uri.fragment.nil? || parsed_uri.fragment.empty?
         | 
| 82 82 | 
             
                      parsed_uri
         | 
| 83 83 | 
             
                    else
         | 
| 84 | 
            -
                      parsed_uri.merge(: | 
| 84 | 
            +
                      parsed_uri.merge(fragment: '')
         | 
| 85 85 | 
             
                    end
         | 
| 86 86 | 
             
                  end
         | 
| 87 87 |  | 
| @@ -23,262 +23,263 @@ | |
| 23 23 | 
             
            # 2009-02-20:  Modified by Pablo Lorenzoni <pablo@propus.com.br>  to  correctly
         | 
| 24 24 | 
             
            # include the version in the raw_bytes.
         | 
| 25 25 |  | 
| 26 | 
            -
             | 
| 27 26 | 
             
            require 'digest/md5'
         | 
| 28 27 | 
             
            require 'digest/sha1'
         | 
| 29 28 | 
             
            require 'tmpdir'
         | 
| 30 29 |  | 
| 31 30 | 
             
            module JSON
         | 
| 32 31 | 
             
              module Util
         | 
| 33 | 
            -
              
         | 
| 34 32 | 
             
                # Pure ruby UUID generator, which is compatible with RFC4122
         | 
| 35 33 | 
             
                UUID = Struct.new :raw_bytes
         | 
| 36 | 
            -
             | 
| 34 | 
            +
             | 
| 37 35 | 
             
                class UUID
         | 
| 38 | 
            -
             | 
| 36 | 
            +
                  private_class_method :new
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                  class << self
         | 
| 39 | 
            +
                    # :nodoc
         | 
| 40 | 
            +
                    def mask19 v, str
         | 
| 41 | 
            +
                      nstr = str.bytes.to_a
         | 
| 42 | 
            +
                      version = [0, 16, 32, 48, 64, 80][v]
         | 
| 43 | 
            +
                      nstr[6] &= 0b00001111
         | 
| 44 | 
            +
                      nstr[6] |= version
         | 
| 45 | 
            +
                      # nstr[7] &= 0b00001111
         | 
| 46 | 
            +
                      # nstr[7] |= 0b01010000
         | 
| 47 | 
            +
                      nstr[8] &= 0b00111111
         | 
| 48 | 
            +
                      nstr[8] |= 0b10000000
         | 
| 49 | 
            +
                      str = ''
         | 
| 50 | 
            +
                      nstr.each { |s| str << s.chr }
         | 
| 51 | 
            +
                      str
         | 
| 52 | 
            +
                    end
         | 
| 39 53 |  | 
| 40 | 
            -
             | 
| 41 | 
            -
             | 
| 42 | 
            -
             | 
| 43 | 
            -
             | 
| 44 | 
            -
             | 
| 45 | 
            -
             | 
| 46 | 
            -
             | 
| 47 | 
            -
             | 
| 48 | 
            -
             | 
| 49 | 
            -
             | 
| 50 | 
            -
             | 
| 51 | 
            -
                			nstr.each { |s| str << s.chr }
         | 
| 52 | 
            -
                			str
         | 
| 53 | 
            -
                		end
         | 
| 54 | 
            +
                    # :nodoc
         | 
| 55 | 
            +
                    def mask18 v, str
         | 
| 56 | 
            +
                      version = [0, 16, 32, 48, 64, 80][v]
         | 
| 57 | 
            +
                      str[6] &= 0b00001111
         | 
| 58 | 
            +
                      str[6] |= version
         | 
| 59 | 
            +
                      # str[7] &= 0b00001111
         | 
| 60 | 
            +
                      # str[7] |= 0b01010000
         | 
| 61 | 
            +
                      str[8] &= 0b00111111
         | 
| 62 | 
            +
                      str[8] |= 0b10000000
         | 
| 63 | 
            +
                      str
         | 
| 64 | 
            +
                    end
         | 
| 54 65 |  | 
| 55 | 
            -
             | 
| 56 | 
            -
             | 
| 57 | 
            -
             | 
| 58 | 
            -
             | 
| 59 | 
            -
             | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 63 | 
            -
                			str
         | 
| 64 | 
            -
                		end
         | 
| 66 | 
            +
                    def mask v, str
         | 
| 67 | 
            +
                      if RUBY_VERSION >= '1.9.0'
         | 
| 68 | 
            +
                        mask19 v, str
         | 
| 69 | 
            +
                      else
         | 
| 70 | 
            +
                        mask18 v, str
         | 
| 71 | 
            +
                      end
         | 
| 72 | 
            +
                    end
         | 
| 73 | 
            +
                    private :mask, :mask18, :mask19
         | 
| 65 74 |  | 
| 66 | 
            -
             | 
| 67 | 
            -
             | 
| 68 | 
            -
             | 
| 69 | 
            -
             | 
| 70 | 
            -
             | 
| 71 | 
            -
             | 
| 72 | 
            -
             | 
| 73 | 
            -
             | 
| 75 | 
            +
                    # UUID generation using SHA1. Recommended over create_md5.
         | 
| 76 | 
            +
                    # Namespace object is another UUID, some of them are pre-defined below.
         | 
| 77 | 
            +
                    def create_sha1 str, namespace
         | 
| 78 | 
            +
                      sha1 = Digest::SHA1.new
         | 
| 79 | 
            +
                      sha1.update namespace.raw_bytes
         | 
| 80 | 
            +
                      sha1.update str
         | 
| 81 | 
            +
                      sum = sha1.digest
         | 
| 82 | 
            +
                      raw = mask 5, sum[0..15]
         | 
| 83 | 
            +
                      ret = new raw
         | 
| 84 | 
            +
                      ret.freeze
         | 
| 85 | 
            +
                      ret
         | 
| 86 | 
            +
                    end
         | 
| 87 | 
            +
                    alias :create_v5 :create_sha1
         | 
| 74 88 |  | 
| 75 | 
            -
             | 
| 76 | 
            -
             | 
| 77 | 
            -
             | 
| 78 | 
            -
             | 
| 79 | 
            -
             | 
| 80 | 
            -
             | 
| 81 | 
            -
             | 
| 82 | 
            -
             | 
| 83 | 
            -
             | 
| 84 | 
            -
             | 
| 85 | 
            -
             | 
| 86 | 
            -
             | 
| 87 | 
            -
                		alias :create_v5 :create_sha1
         | 
| 89 | 
            +
                    # UUID generation using MD5 (for backward compat.)
         | 
| 90 | 
            +
                    def create_md5 str, namespace
         | 
| 91 | 
            +
                      md5 = Digest::MD5.new
         | 
| 92 | 
            +
                      md5.update namespace.raw_bytes
         | 
| 93 | 
            +
                      md5.update str
         | 
| 94 | 
            +
                      sum = md5.digest
         | 
| 95 | 
            +
                      raw = mask 3, sum[0..16]
         | 
| 96 | 
            +
                      ret = new raw
         | 
| 97 | 
            +
                      ret.freeze
         | 
| 98 | 
            +
                      ret
         | 
| 99 | 
            +
                    end
         | 
| 100 | 
            +
                    alias :create_v3 :create_md5
         | 
| 88 101 |  | 
| 89 | 
            -
             | 
| 90 | 
            -
             | 
| 91 | 
            -
             | 
| 92 | 
            -
             | 
| 93 | 
            -
             | 
| 94 | 
            -
             | 
| 95 | 
            -
             | 
| 96 | 
            -
             | 
| 97 | 
            -
             | 
| 98 | 
            -
             | 
| 99 | 
            -
             | 
| 100 | 
            -
             | 
| 102 | 
            +
                    # UUID  generation  using  random-number  generator.   From  it's  random
         | 
| 103 | 
            +
                    # nature, there's  no warranty that  the created ID is  really universaly
         | 
| 104 | 
            +
                    # unique.
         | 
| 105 | 
            +
                    def create_random
         | 
| 106 | 
            +
                      rnd = [
         | 
| 107 | 
            +
                        rand(0x100000000),
         | 
| 108 | 
            +
                        rand(0x100000000),
         | 
| 109 | 
            +
                        rand(0x100000000),
         | 
| 110 | 
            +
                        rand(0x100000000),
         | 
| 111 | 
            +
                      ].pack 'N4'
         | 
| 112 | 
            +
                      raw = mask 4, rnd
         | 
| 113 | 
            +
                      ret = new raw
         | 
| 114 | 
            +
                      ret.freeze
         | 
| 115 | 
            +
                      ret
         | 
| 116 | 
            +
                    end
         | 
| 117 | 
            +
                    alias :create_v4 :create_random
         | 
| 101 118 |  | 
| 102 | 
            -
             | 
| 103 | 
            -
             | 
| 104 | 
            -
             | 
| 105 | 
            -
             | 
| 106 | 
            -
                			rnd = [
         | 
| 107 | 
            -
                				rand(0x100000000),
         | 
| 108 | 
            -
                				rand(0x100000000),
         | 
| 109 | 
            -
                				rand(0x100000000),
         | 
| 110 | 
            -
                				rand(0x100000000),
         | 
| 111 | 
            -
                			].pack "N4"
         | 
| 112 | 
            -
                			raw = mask 4, rnd
         | 
| 113 | 
            -
                			ret = new raw
         | 
| 114 | 
            -
                			ret.freeze
         | 
| 115 | 
            -
                			ret
         | 
| 116 | 
            -
                		end
         | 
| 117 | 
            -
                		alias :create_v4 :create_random
         | 
| 119 | 
            +
                    def read_state fp # :nodoc:
         | 
| 120 | 
            +
                      fp.rewind
         | 
| 121 | 
            +
                      Marshal.load fp.read
         | 
| 122 | 
            +
                    end
         | 
| 118 123 |  | 
| 119 | 
            -
             | 
| 120 | 
            -
             | 
| 121 | 
            -
             | 
| 122 | 
            -
             | 
| 124 | 
            +
                    def write_state fp, c, m # :nodoc:
         | 
| 125 | 
            +
                      fp.rewind
         | 
| 126 | 
            +
                      str = Marshal.dump [c, m]
         | 
| 127 | 
            +
                      fp.write str
         | 
| 128 | 
            +
                    end
         | 
| 123 129 |  | 
| 124 | 
            -
             | 
| 125 | 
            -
             | 
| 126 | 
            -
                			str = Marshal.dump [c, m]
         | 
| 127 | 
            -
                			fp.write str
         | 
| 128 | 
            -
                		end
         | 
| 130 | 
            +
                    private :read_state, :write_state
         | 
| 131 | 
            +
                    STATE_FILE = 'ruby-uuid'
         | 
| 129 132 |  | 
| 130 | 
            -
             | 
| 131 | 
            -
             | 
| 133 | 
            +
                    # create  the "version  1" UUID  with current  system clock,  current UTC
         | 
| 134 | 
            +
                    # timestamp, and the IEEE 802 address (so-called MAC address).
         | 
| 135 | 
            +
                    #
         | 
| 136 | 
            +
                    # Speed notice: it's slow.  It writes  some data into hard drive on every
         | 
| 137 | 
            +
                    # invokation. If you want to speed  this up, try remounting tmpdir with a
         | 
| 138 | 
            +
                    # memory based filesystem  (such as tmpfs).  STILL slow?  then no way but
         | 
| 139 | 
            +
                    # rewrite it with c :)
         | 
| 140 | 
            +
                    def create clock = nil, time = nil, mac_addr = nil
         | 
| 141 | 
            +
                      c = t = m = nil
         | 
| 142 | 
            +
                      Dir.chdir Dir.tmpdir do
         | 
| 143 | 
            +
                        unless FileTest.exist? STATE_FILE then
         | 
| 144 | 
            +
                          # Generate a pseudo MAC address because we have no pure-ruby way
         | 
| 145 | 
            +
                          # to know  the MAC  address of the  NIC this system  uses.  Note
         | 
| 146 | 
            +
                          # that cheating  with pseudo arresses here  is completely legal:
         | 
| 147 | 
            +
                          # see Section 4.5 of RFC4122 for details.
         | 
| 148 | 
            +
                          sha1 = Digest::SHA1.new
         | 
| 149 | 
            +
                          256.times do
         | 
| 150 | 
            +
                            r = [rand(0x100000000)].pack 'N'
         | 
| 151 | 
            +
                            sha1.update r
         | 
| 152 | 
            +
                          end
         | 
| 153 | 
            +
                          str = sha1.digest
         | 
| 154 | 
            +
                          r = rand 14 # 20-6
         | 
| 155 | 
            +
                          node = str[r, 6] || str
         | 
| 156 | 
            +
                          if RUBY_VERSION >= '1.9.0'
         | 
| 157 | 
            +
                            nnode = node.bytes.to_a
         | 
| 158 | 
            +
                            nnode[0] |= 0x01
         | 
| 159 | 
            +
                            node = ''
         | 
| 160 | 
            +
                            nnode.each { |s| node << s.chr }
         | 
| 161 | 
            +
                          else
         | 
| 162 | 
            +
                            node[0] |= 0x01 # multicast bit
         | 
| 163 | 
            +
                          end
         | 
| 164 | 
            +
                          k = rand 0x40000
         | 
| 165 | 
            +
                          open STATE_FILE, 'w' do |fp|
         | 
| 166 | 
            +
                            fp.flock IO::LOCK_EX
         | 
| 167 | 
            +
                            write_state fp, k, node
         | 
| 168 | 
            +
                            fp.chmod 0o777 # must be world writable
         | 
| 169 | 
            +
                          end
         | 
| 170 | 
            +
                        end
         | 
| 171 | 
            +
                        open STATE_FILE, 'r+' do |fp|
         | 
| 172 | 
            +
                          fp.flock IO::LOCK_EX
         | 
| 173 | 
            +
                          c, m = read_state fp
         | 
| 174 | 
            +
                          c = clock % 0x4000 if clock
         | 
| 175 | 
            +
                          m = mac_addr if mac_addr
         | 
| 176 | 
            +
                          t = time
         | 
| 177 | 
            +
                          if t.nil? then
         | 
| 178 | 
            +
                            # UUID epoch is 1582/Oct/15
         | 
| 179 | 
            +
                            tt = Time.now
         | 
| 180 | 
            +
                            t = tt.to_i * 10000000 + tt.tv_usec * 10 + 0x01B21DD213814000
         | 
| 181 | 
            +
                          end
         | 
| 182 | 
            +
                          c = c.succ # important; increment here
         | 
| 183 | 
            +
                          write_state fp, c, m
         | 
| 184 | 
            +
                        end
         | 
| 185 | 
            +
                      end
         | 
| 132 186 |  | 
| 133 | 
            -
             | 
| 134 | 
            -
             | 
| 135 | 
            -
             | 
| 136 | 
            -
             | 
| 137 | 
            -
             | 
| 138 | 
            -
             | 
| 139 | 
            -
             | 
| 140 | 
            -
             | 
| 141 | 
            -
             | 
| 142 | 
            -
             | 
| 143 | 
            -
             | 
| 144 | 
            -
             | 
| 145 | 
            -
             | 
| 146 | 
            -
                					# that cheating  with pseudo arresses here  is completely legal:
         | 
| 147 | 
            -
                					# see Section 4.5 of RFC4122 for details.
         | 
| 148 | 
            -
                					sha1 = Digest::SHA1.new
         | 
| 149 | 
            -
                					256.times do
         | 
| 150 | 
            -
                						r = [rand(0x100000000)].pack "N"
         | 
| 151 | 
            -
                						sha1.update r
         | 
| 152 | 
            -
                					end
         | 
| 153 | 
            -
                					str = sha1.digest
         | 
| 154 | 
            -
                					r = rand 14 # 20-6
         | 
| 155 | 
            -
                					node = str[r, 6] || str
         | 
| 156 | 
            -
                					if RUBY_VERSION >= "1.9.0"
         | 
| 157 | 
            -
                						nnode = node.bytes.to_a
         | 
| 158 | 
            -
                						nnode[0] |= 0x01
         | 
| 159 | 
            -
                						node = ''
         | 
| 160 | 
            -
                						nnode.each { |s| node << s.chr }
         | 
| 161 | 
            -
                					else
         | 
| 162 | 
            -
                						node[0] |= 0x01 # multicast bit
         | 
| 163 | 
            -
                					end
         | 
| 164 | 
            -
                					k = rand 0x40000
         | 
| 165 | 
            -
                					open STATE_FILE, 'w' do |fp|
         | 
| 166 | 
            -
                						fp.flock IO::LOCK_EX
         | 
| 167 | 
            -
                						write_state fp, k, node
         | 
| 168 | 
            -
                						fp.chmod 0o777 # must be world writable
         | 
| 169 | 
            -
                					end
         | 
| 170 | 
            -
                				end
         | 
| 171 | 
            -
                				open STATE_FILE, 'r+' do |fp|
         | 
| 172 | 
            -
                					fp.flock IO::LOCK_EX
         | 
| 173 | 
            -
                					c, m = read_state fp
         | 
| 174 | 
            -
                					c = clock % 0x4000 if clock
         | 
| 175 | 
            -
                					m = mac_addr if mac_addr
         | 
| 176 | 
            -
                					t = time
         | 
| 177 | 
            -
                					if t.nil? then
         | 
| 178 | 
            -
                						# UUID epoch is 1582/Oct/15
         | 
| 179 | 
            -
                						tt = Time.now
         | 
| 180 | 
            -
                						t = tt.to_i*10000000 + tt.tv_usec*10 + 0x01B21DD213814000
         | 
| 181 | 
            -
                					end
         | 
| 182 | 
            -
                					c = c.succ # important; increment here
         | 
| 183 | 
            -
                					write_state fp, c, m
         | 
| 184 | 
            -
                				end
         | 
| 185 | 
            -
                			end
         | 
| 187 | 
            +
                      tl = t & 0xFFFF_FFFF
         | 
| 188 | 
            +
                      tm = t >> 32
         | 
| 189 | 
            +
                      tm = tm & 0xFFFF
         | 
| 190 | 
            +
                      th = t >> 48
         | 
| 191 | 
            +
                      th = th & 0x0FFF
         | 
| 192 | 
            +
                      th = th | 0x1000
         | 
| 193 | 
            +
                      cl = c & 0xFF
         | 
| 194 | 
            +
                      ch = c & 0x3F00
         | 
| 195 | 
            +
                      ch = ch >> 8
         | 
| 196 | 
            +
                      ch = ch | 0x80
         | 
| 197 | 
            +
                      pack tl, tm, th, cl, ch, m
         | 
| 198 | 
            +
                    end
         | 
| 199 | 
            +
                    alias :create_v1 :create
         | 
| 186 200 |  | 
| 187 | 
            -
             | 
| 188 | 
            -
             | 
| 189 | 
            -
             | 
| 190 | 
            -
             | 
| 191 | 
            -
             | 
| 192 | 
            -
             | 
| 193 | 
            -
             | 
| 194 | 
            -
             | 
| 195 | 
            -
             | 
| 196 | 
            -
             | 
| 197 | 
            -
                			pack tl, tm, th, cl, ch, m
         | 
| 198 | 
            -
                		end
         | 
| 199 | 
            -
                		alias :create_v1 :create
         | 
| 201 | 
            +
                    # A  simple GUID  parser:  just ignores  unknown  characters and  convert
         | 
| 202 | 
            +
                    # hexadecimal dump into 16-octet object.
         | 
| 203 | 
            +
                    def parse obj
         | 
| 204 | 
            +
                      str = obj.to_s.sub(/\Aurn:uuid:/, '')
         | 
| 205 | 
            +
                      str.gsub!(/[^0-9A-Fa-f]/, '')
         | 
| 206 | 
            +
                      raw = str[0..31].lines.to_a.pack 'H*'
         | 
| 207 | 
            +
                      ret = new raw
         | 
| 208 | 
            +
                      ret.freeze
         | 
| 209 | 
            +
                      ret
         | 
| 210 | 
            +
                    end
         | 
| 200 211 |  | 
| 201 | 
            -
             | 
| 202 | 
            -
             | 
| 203 | 
            -
             | 
| 204 | 
            -
             | 
| 205 | 
            -
             | 
| 206 | 
            -
             | 
| 207 | 
            -
             | 
| 208 | 
            -
             | 
| 209 | 
            -
             | 
| 210 | 
            -
                		end
         | 
| 212 | 
            +
                    # The 'primitive constructor' of this class
         | 
| 213 | 
            +
                    # Note UUID.pack(uuid.unpack) == uuid
         | 
| 214 | 
            +
                    def pack tl, tm, th, ch, cl, n
         | 
| 215 | 
            +
                      raw = [tl, tm, th, ch, cl, n].pack 'NnnCCa6'
         | 
| 216 | 
            +
                      ret = new raw
         | 
| 217 | 
            +
                      ret.freeze
         | 
| 218 | 
            +
                      ret
         | 
| 219 | 
            +
                    end
         | 
| 220 | 
            +
                  end
         | 
| 211 221 |  | 
| 212 | 
            -
             | 
| 213 | 
            -
             | 
| 214 | 
            -
             | 
| 215 | 
            -
             | 
| 216 | 
            -
             | 
| 217 | 
            -
                			ret.freeze
         | 
| 218 | 
            -
                			ret
         | 
| 219 | 
            -
                		end
         | 
| 220 | 
            -
                	end
         | 
| 222 | 
            +
                  # The 'primitive deconstructor', or the dual to pack.
         | 
| 223 | 
            +
                  # Note UUID.pack(uuid.unpack) == uuid
         | 
| 224 | 
            +
                  def unpack
         | 
| 225 | 
            +
                    raw_bytes.unpack 'NnnCCa6'
         | 
| 226 | 
            +
                  end
         | 
| 221 227 |  | 
| 222 | 
            -
             | 
| 223 | 
            -
             | 
| 224 | 
            -
             | 
| 225 | 
            -
             | 
| 226 | 
            -
             | 
| 228 | 
            +
                  # Generate the string representation (a.k.a GUID) of this UUID
         | 
| 229 | 
            +
                  def to_s
         | 
| 230 | 
            +
                    a = unpack
         | 
| 231 | 
            +
                    tmp = a[-1].unpack 'C*'
         | 
| 232 | 
            +
                    a[-1] = format '%02x%02x%02x%02x%02x%02x', *tmp
         | 
| 233 | 
            +
                    '%08x-%04x-%04x-%02x%02x-%s' % a
         | 
| 234 | 
            +
                  end
         | 
| 235 | 
            +
                  alias guid to_s
         | 
| 227 236 |  | 
| 228 | 
            -
             | 
| 229 | 
            -
             | 
| 230 | 
            -
             | 
| 231 | 
            -
             | 
| 232 | 
            -
             | 
| 233 | 
            -
                		"%08x-%04x-%04x-%02x%02x-%s" % a
         | 
| 234 | 
            -
                	end
         | 
| 235 | 
            -
                	alias guid to_s
         | 
| 237 | 
            +
                  # Convert into a RFC4122-comforming URN representation
         | 
| 238 | 
            +
                  def to_uri
         | 
| 239 | 
            +
                    'urn:uuid:' + to_s
         | 
| 240 | 
            +
                  end
         | 
| 241 | 
            +
                  alias urn to_uri
         | 
| 236 242 |  | 
| 237 | 
            -
             | 
| 238 | 
            -
             | 
| 239 | 
            -
             | 
| 240 | 
            -
             | 
| 241 | 
            -
             | 
| 243 | 
            +
                  # Convert into 128-bit unsigned integer
         | 
| 244 | 
            +
                  def to_int
         | 
| 245 | 
            +
                    tmp = raw_bytes.unpack 'C*'
         | 
| 246 | 
            +
                    tmp.inject do |r, i|
         | 
| 247 | 
            +
                      r * 256 | i
         | 
| 248 | 
            +
                    end
         | 
| 249 | 
            +
                  end
         | 
| 250 | 
            +
                  alias to_i to_int
         | 
| 242 251 |  | 
| 243 | 
            -
             | 
| 244 | 
            -
             | 
| 245 | 
            -
             | 
| 246 | 
            -
             | 
| 247 | 
            -
             | 
| 248 | 
            -
             | 
| 249 | 
            -
                	end
         | 
| 250 | 
            -
                	alias to_i to_int
         | 
| 252 | 
            +
                  # Gets the version of this UUID
         | 
| 253 | 
            +
                  # returns nil if bad version
         | 
| 254 | 
            +
                  def version
         | 
| 255 | 
            +
                    a = unpack
         | 
| 256 | 
            +
                    v = (a[2] & 0xF000).to_s(16)[0].chr.to_i
         | 
| 257 | 
            +
                    return v if (1..5).include? v
         | 
| 251 258 |  | 
| 252 | 
            -
             | 
| 253 | 
            -
             | 
| 254 | 
            -
                	def version
         | 
| 255 | 
            -
                		a = unpack
         | 
| 256 | 
            -
                		v = (a[2] & 0xF000).to_s(16)[0].chr.to_i
         | 
| 257 | 
            -
                		return v if (1..5).include? v
         | 
| 258 | 
            -
                		return nil
         | 
| 259 | 
            -
                	end
         | 
| 259 | 
            +
                    nil
         | 
| 260 | 
            +
                  end
         | 
| 260 261 |  | 
| 261 | 
            -
             | 
| 262 | 
            -
             | 
| 263 | 
            -
             | 
| 264 | 
            -
             | 
| 265 | 
            -
             | 
| 266 | 
            -
             | 
| 262 | 
            +
                  # Two  UUIDs  are  said  to  be  equal if  and  only  if  their  (byte-order
         | 
| 263 | 
            +
                  # canonicalized) integer representations are equivallent.  Refer RFC4122 for
         | 
| 264 | 
            +
                  # details.
         | 
| 265 | 
            +
                  def == other
         | 
| 266 | 
            +
                    to_i == other.to_i
         | 
| 267 | 
            +
                  end
         | 
| 267 268 |  | 
| 268 | 
            -
             | 
| 269 | 
            -
             | 
| 270 | 
            -
             | 
| 271 | 
            -
             | 
| 272 | 
            -
             | 
| 269 | 
            +
                  include Comparable
         | 
| 270 | 
            +
                  # UUIDs are comparable (don't know what benefits are there, though).
         | 
| 271 | 
            +
                  def <=> other
         | 
| 272 | 
            +
                    to_s <=> other.to_s
         | 
| 273 | 
            +
                  end
         | 
| 273 274 |  | 
| 274 | 
            -
             | 
| 275 | 
            -
             | 
| 276 | 
            -
             | 
| 277 | 
            -
             | 
| 278 | 
            -
             | 
| 275 | 
            +
                  # Pre-defined UUID Namespaces described in RFC4122 Appendix C.
         | 
| 276 | 
            +
                  NameSpace_DNS = parse '6ba7b810-9dad-11d1-80b4-00c04fd430c8'
         | 
| 277 | 
            +
                  NameSpace_URL = parse '6ba7b811-9dad-11d1-80b4-00c04fd430c8'
         | 
| 278 | 
            +
                  NameSpace_OID = parse '6ba7b812-9dad-11d1-80b4-00c04fd430c8'
         | 
| 279 | 
            +
                  NameSpace_X500 = parse '6ba7b814-9dad-11d1-80b4-00c04fd430c8'
         | 
| 279 280 |  | 
| 280 | 
            -
             | 
| 281 | 
            -
             | 
| 281 | 
            +
                  # The Nil UUID in RFC4122 Section 4.1.7
         | 
| 282 | 
            +
                  Nil = parse '00000000-0000-0000-0000-000000000000'
         | 
| 282 283 | 
             
                end
         | 
| 283 284 | 
             
              end
         | 
| 284 | 
            -
            end
         | 
| 285 | 
            +
            end
         |