json-schema 0.9.6 → 0.9.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.
- data/README.textile +1 -1
- data/lib/json-schema/uri/uuid.rb +229 -227
- data/lib/json-schema/validator.rb +1 -1
- metadata +4 -4
data/README.textile
CHANGED
data/lib/json-schema/uri/uuid.rb
CHANGED
@@ -28,256 +28,258 @@ require 'digest/md5'
|
|
28
28
|
require 'digest/sha1'
|
29
29
|
require 'tmpdir'
|
30
30
|
|
31
|
-
module
|
31
|
+
module JSON
|
32
|
+
module Util
|
32
33
|
|
33
|
-
|
34
|
-
|
34
|
+
# Pure ruby UUID generator, which is compatible with RFC4122
|
35
|
+
UUID = Struct.new :raw_bytes
|
35
36
|
|
36
|
-
|
37
|
-
|
37
|
+
class UUID
|
38
|
+
private_class_method :new
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
40
|
+
class << self
|
41
|
+
def mask19 v, str # :nodoc
|
42
|
+
nstr = str.bytes.to_a
|
43
|
+
version = [0, 16, 32, 48, 64, 80][v]
|
44
|
+
nstr[6] &= 0b00001111
|
45
|
+
nstr[6] |= version
|
46
|
+
# nstr[7] &= 0b00001111
|
47
|
+
# nstr[7] |= 0b01010000
|
48
|
+
nstr[8] &= 0b00111111
|
49
|
+
nstr[8] |= 0b10000000
|
50
|
+
str = ''
|
51
|
+
nstr.each { |s| str << s.chr }
|
52
|
+
str
|
53
|
+
end
|
53
54
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
55
|
+
def mask18 v, str # :nodoc
|
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
|
64
65
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
66
|
+
def mask v, str
|
67
|
+
if RUBY_VERSION >= "1.9.0"
|
68
|
+
return mask19 v, str
|
69
|
+
else
|
70
|
+
return mask18 v, str
|
71
|
+
end
|
72
|
+
end
|
73
|
+
private :mask, :mask18, :mask19
|
73
74
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
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
|
87
88
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
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
|
100
101
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
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
|
117
118
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
119
|
+
def read_state fp # :nodoc:
|
120
|
+
fp.rewind
|
121
|
+
Marshal.load fp.read
|
122
|
+
end
|
122
123
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
124
|
+
def write_state fp, c, m # :nodoc:
|
125
|
+
fp.rewind
|
126
|
+
str = Marshal.dump [c, m]
|
127
|
+
fp.write str
|
128
|
+
end
|
128
129
|
|
129
|
-
|
130
|
-
|
130
|
+
private :read_state, :write_state
|
131
|
+
STATE_FILE = 'ruby-uuid'
|
131
132
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
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
|
185
186
|
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
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
|
199
200
|
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
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 %r/\Aurn:uuid:/, ''
|
205
|
+
str.gsub! %r/[^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
|
210
211
|
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
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
|
220
221
|
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
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
|
226
227
|
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
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] = sprintf '%02x%02x%02x%02x%02x%02x', *tmp
|
233
|
+
"%08x-%04x-%04x-%02x%02x-%s" % a
|
234
|
+
end
|
235
|
+
alias guid to_s
|
235
236
|
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
237
|
+
# Convert into a RFC4122-comforming URN representation
|
238
|
+
def to_uri
|
239
|
+
"urn:uuid:" + self.to_s
|
240
|
+
end
|
241
|
+
alias urn to_uri
|
241
242
|
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
243
|
+
# Convert into 128-bit unsigned integer
|
244
|
+
# Typically a Bignum instance, but can be a Fixnum.
|
245
|
+
def to_int
|
246
|
+
tmp = self.raw_bytes.unpack "C*"
|
247
|
+
tmp.inject do |r, i|
|
248
|
+
r * 256 | i
|
249
|
+
end
|
250
|
+
end
|
251
|
+
alias to_i to_int
|
251
252
|
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
253
|
+
# Gets the version of this UUID
|
254
|
+
# returns nil if bad version
|
255
|
+
def version
|
256
|
+
a = unpack
|
257
|
+
v = (a[2] & 0xF000).to_s(16)[0].chr.to_i
|
258
|
+
return v if (1..5).include? v
|
259
|
+
return 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"
|
283
|
+
end
|
282
284
|
end
|
283
285
|
end
|
@@ -324,7 +324,7 @@ module JSON
|
|
324
324
|
@@fake_uri_generator = lambda{|s| UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, s).to_s }
|
325
325
|
else
|
326
326
|
require 'uri/uuid'
|
327
|
-
@@fake_uri_generator = lambda{|s|
|
327
|
+
@@fake_uri_generator = lambda{|s| JSON::Util::UUID.create_v5(s,JSON::Util::UUID::Nil).to_s }
|
328
328
|
end
|
329
329
|
|
330
330
|
def fake_uri schema
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 53
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 9
|
9
|
-
-
|
10
|
-
version: 0.9.
|
9
|
+
- 7
|
10
|
+
version: 0.9.7
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kenny Hoxworth
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-06-
|
18
|
+
date: 2011-06-29 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|