cbor-cri 0.0.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7837e86b7141242b2c274f0fc1c8cb510f67cc060d4453321b71bd32cd09ddca
4
+ data.tar.gz: b556e3ad03cd371159eb8656f0583ed0569c5c52e3e94655e874d5c39c7b0e06
5
+ SHA512:
6
+ metadata.gz: 562339239e9243179307e64823a8fc780448cb6866bdd36493f41aaebe6f55c92436c16799fb0739985d9cc46cae55935f240a1a287f2c6d26ed258ed7a114f6
7
+ data.tar.gz: fadba4388a27a3966f9cbb442ff48987d40823d62744a76285bb6f06ad714a2e8d44968aac5e606481301228512a1ffab5c2c8b2068e119617aaee6327d001f5
data/cbor-cri.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "cbor-cri"
3
+ s.version = "0.0.3"
4
+ s.summary = "CBOR (Concise Binary Object Representation) diagnostic notation"
5
+ s.description = %q{cbor-cri implements CBOR constrained resource identifiers, draft-ietf-core-href}
6
+ s.author = "Carsten Bormann"
7
+ s.email = "cabo@tzi.org"
8
+ s.license = "MIT"
9
+ s.homepage = "http://cbor.io/"
10
+ # s.files = `git ls-files`.split("\n") << "lib/cbor-diag-parser.rb"
11
+ # s.test_files = `git ls-files -- {test,spec}/*`.split("\n")
12
+ s.files = Dir['lib/**/*.rb'] + %w(cbor-cri.gemspec) + Dir['bin/**/*.rb']
13
+ s.executables = Dir['bin/**/*.rb'].map {|x| File.basename(x)}
14
+ s.required_ruby_version = '>= 2.3'
15
+
16
+ s.require_paths = ["lib"]
17
+
18
+ s.add_development_dependency 'bundler', '~>1'
19
+ end
data/lib/cbor-cri.rb ADDED
@@ -0,0 +1,230 @@
1
+ require 'uri'
2
+ require 'ipaddr'
3
+ unless defined?(CBOR)
4
+ require 'cbor-pure'
5
+ end
6
+
7
+ class CBOR::CRI
8
+
9
+ # index ~n -1 -2 -3 -4 -5 -6
10
+ SCHEMES = [:coap, :coaps, :http, :https, :urn, :did]
11
+
12
+ attr_accessor :scheme, :authority, :discard, :path, :query, :fragment
13
+
14
+ def discard?(d = discard)
15
+ true == d || (Integer === d && d >= 0)
16
+ end
17
+
18
+ def relpath?
19
+ scheme.nil?
20
+ end
21
+
22
+ def initialize(*parts)
23
+ # p parts
24
+ if discard?(parts[0])
25
+ # p 1
26
+ self.discard, self.path, self.query, self.fragment, slop = parts
27
+ else
28
+ if discard?(parts[2])
29
+ # p 2
30
+ self.scheme, self.authority, self.discard, self.path, self.query, self.fragment, slop = parts
31
+ else
32
+ # p 3
33
+ self.scheme, self.authority, self.path, self.query, self.fragment, slop = parts
34
+ self.discard = true
35
+ end
36
+ end
37
+ fail ArgumentError.new(parts.inspect) if slop
38
+ check
39
+ end
40
+
41
+ def to_item
42
+ ret =
43
+ if scheme || authority
44
+ fail inspect unless discard == true
45
+ [scheme, authority, path, query, fragment]
46
+ else
47
+ [discard, path, query, fragment]
48
+ end
49
+ while ret.size > 0 && ret[-1].nil?
50
+ ret.pop
51
+ end
52
+ ret
53
+ end
54
+
55
+ def ==(other)
56
+ to_item == other.to_item
57
+ end
58
+
59
+ # copy?
60
+
61
+ def check
62
+ # TBD
63
+ end
64
+
65
+ def merge(other)
66
+ ret = dup
67
+ if other.discard == true
68
+ ret.path = other.path.dup
69
+ ret.query = nil
70
+ ret.fragment = nil
71
+ ret.authority = null if ret.authority == true
72
+ elsif Integer === other.discard
73
+ ret.path = ret.path.dup
74
+ ret.path.pop(other.discard)
75
+ ret.path.concat(other.path) if other.path
76
+ if other.discard != 0
77
+ ret.query = nil
78
+ ret.fragment = nil
79
+ end
80
+ end
81
+ if other.scheme
82
+ ret.scheme = other.scheme
83
+ ret.authority = other.authority
84
+ else
85
+ ret.authority = other.authority if other.authority
86
+ end
87
+ if other.query
88
+ ret.query = other.query
89
+ ret.fragment = nil
90
+ end
91
+ ret.fragment = other.fragment if other.fragment
92
+ ret
93
+ end
94
+
95
+ # XXX percent-encoding needed
96
+ def to_uri
97
+ ret = ""
98
+ if scheme
99
+ ret <<
100
+ if Integer === scheme
101
+ SCHEMES[~scheme].to_s
102
+ else
103
+ scheme
104
+ end << ":"
105
+ end
106
+ if authority && authority != true
107
+ ret << "//" << (host || ":::FAIL:::")
108
+ if port
109
+ ret << ":" << port.to_s
110
+ end
111
+ end
112
+ if path
113
+ ret << "/" if authority != true && discard == true
114
+ ret << path.join("/")
115
+ end
116
+ if query
117
+ ret << "?"
118
+ ret << query.join("&")
119
+ end
120
+ if fragment
121
+ ret << "#"
122
+ ret << fragment
123
+ end
124
+ ret
125
+ end
126
+
127
+ # --------------------------------- host/port
128
+
129
+ def self.authority_from_host_port(uri)
130
+ if uri.host
131
+ begin
132
+ a = [a_to_n(uri.host)] # XXX platform can't do zone
133
+ rescue IPAddr::InvalidAddressError
134
+ a = uri.host.split(".")
135
+ end
136
+ if Integer === uri.port
137
+ a << uri.port
138
+ end
139
+ a
140
+ end
141
+ end
142
+
143
+ def n_to_a(n)
144
+ IPAddr.new_ntoh(n).to_s
145
+ end
146
+
147
+ def self.a_to_n(a)
148
+ IPAddr.new(a).hton
149
+ end
150
+
151
+ def host
152
+ if authority
153
+ host_a = authority
154
+ if Array === authority
155
+ p = authority.last
156
+ host_a = authority[0...-1] if Integer === p
157
+ return host_a.join(".") if host_a.first.encoding != Encoding::BINARY
158
+ n_to_a(host_a.first)
159
+ end
160
+ end
161
+ end
162
+
163
+ def port
164
+ if authority
165
+ if Array === authority
166
+ p = authority.last
167
+ p if Integer === p
168
+ end
169
+ end
170
+ end
171
+
172
+ # --------------------------------- URI conversion
173
+
174
+ def self.parse_uri_path(uri)
175
+ # !!! check for opaque
176
+ if uri.path
177
+ path = uri.path
178
+ elsif uri.opaque
179
+ path = uri.opaque
180
+ end
181
+ # p path
182
+ if path
183
+ segs = path.split('/', -1).reject {|x| x == '.'}
184
+ if path[0] == '/'
185
+ discard = true
186
+ segs[0..0] = [] # special case
187
+ elsif path == ""
188
+ discard = 0
189
+ segs = nil
190
+ else
191
+ discard = 1 # if no scheme...
192
+ segs = nil if segs == []
193
+ end
194
+ # p segs
195
+ if segs
196
+ opath = []
197
+ # discard = 0 # XXX
198
+ segs.each do |x|
199
+ if x == '..'
200
+ discard += 1 unless opath.pop
201
+ else opath << x
202
+ end
203
+ end
204
+ end
205
+ end
206
+ [uri.opaque ? true : nil, discard, opath]
207
+ end
208
+
209
+ def self.from_uri(us)
210
+ if String === us
211
+ us = ::URI.parse(us.force_encoding(Encoding::UTF_8))
212
+ end
213
+ # p us
214
+ if us.scheme || us.host
215
+ authority = authority_from_host_port(us)
216
+ discard = true
217
+ end
218
+ # XXX need to work on host, whether IP address
219
+ opq, dsc, opath = self.parse_uri_path(us)
220
+ # p [opq, dsc, opath, authority]
221
+ query = us.query.split('&') if us.query
222
+ # p opath
223
+ scheme = us.scheme
224
+ if scheme && (a = SCHEMES.index(scheme.intern))
225
+ scheme = ~a
226
+ end
227
+ new(*[scheme, authority || opq, discard || dsc, opath, query, us.fragment])
228
+ end
229
+
230
+ end
@@ -0,0 +1,7 @@
1
+ require 'cbor-cri'
2
+
3
+ class CBOR_DIAG::App_cri
4
+ def self.decode(_, s)
5
+ CBOR::CRI.from_uri(s).to_item
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cbor-cri
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Carsten Bormann
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-05-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ description: cbor-cri implements CBOR constrained resource identifiers, draft-ietf-core-href
28
+ email: cabo@tzi.org
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - cbor-cri.gemspec
34
+ - lib/cbor-cri.rb
35
+ - lib/cbor-diagnostic-app/cri.rb
36
+ homepage: http://cbor.io/
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '2.3'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.5.9
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: CBOR (Concise Binary Object Representation) diagnostic notation
59
+ test_files: []