Class: CBOR::CRI

Inherits:
Object
  • Object
show all
Defined in:
lib/cbor-cri.rb

Constant Summary collapse

SCHEMES =

index ~n -1 -2 -3 -4 -5 -6

[:coap, :coaps, :http, :https, :urn, :did]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*parts) ⇒ CRI

Returns a new instance of CRI.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cbor-cri.rb', line 22

def initialize(*parts)
  # p parts
  if discard?(parts[0])
    # p 1
    self.discard, self.path, self.query, self.fragment, slop = parts
  else
    if discard?(parts[2])
      # p 2
      self.scheme, self.authority, self.discard, self.path, self.query, self.fragment, slop = parts
    else
      # p 3
      self.scheme, self.authority, self.path, self.query, self.fragment, slop = parts
      self.discard = true
    end
  end
  fail ArgumentError.new(parts.inspect) if slop
  check
end

Instance Attribute Details

#authorityObject

Returns the value of attribute authority.



12
13
14
# File 'lib/cbor-cri.rb', line 12

def authority
  @authority
end

#discardObject

Returns the value of attribute discard.



12
13
14
# File 'lib/cbor-cri.rb', line 12

def discard
  @discard
end

#fragmentObject

Returns the value of attribute fragment.



12
13
14
# File 'lib/cbor-cri.rb', line 12

def fragment
  @fragment
end

#pathObject

Returns the value of attribute path.



12
13
14
# File 'lib/cbor-cri.rb', line 12

def path
  @path
end

#queryObject

Returns the value of attribute query.



12
13
14
# File 'lib/cbor-cri.rb', line 12

def query
  @query
end

#schemeObject

Returns the value of attribute scheme.



12
13
14
# File 'lib/cbor-cri.rb', line 12

def scheme
  @scheme
end

Class Method Details

.a_to_n(a) ⇒ Object



147
148
149
# File 'lib/cbor-cri.rb', line 147

def self.a_to_n(a)
  IPAddr.new(a).hton
end

.authority_from_host_port(uri) ⇒ Object

——————————— host/port



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cbor-cri.rb', line 129

def self.authority_from_host_port(uri)
  if uri.host
    begin
      a = [a_to_n(uri.host)] # XXX platform can't do zone
    rescue IPAddr::InvalidAddressError
      a = uri.host.split(".")
    end
    if Integer === uri.port
      a << uri.port
    end
    a
  end
end

.from_uri(us) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/cbor-cri.rb', line 209

def self.from_uri(us)
  if String === us
    us = ::URI.parse(us.force_encoding(Encoding::UTF_8))
  end
  # p us
  if us.scheme || us.host
    authority = authority_from_host_port(us)
    discard = true
  end
  # XXX need to work on host, whether IP address
  opq, dsc, opath = self.parse_uri_path(us)
  # p [opq, dsc, opath, authority]
  query = us.query.split('&') if us.query
  # p opath
  scheme = us.scheme
  if scheme && (a = SCHEMES.index(scheme.intern))
    scheme = ~a
  end
  new(*[scheme, authority || opq, discard || dsc, opath, query, us.fragment])
end

.parse_uri_path(uri) ⇒ Object

——————————— URI conversion



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/cbor-cri.rb', line 174

def self.parse_uri_path(uri)
  # !!! check for opaque
  if uri.path
    path = uri.path
  elsif uri.opaque
    path = uri.opaque
  end
  # p path
  if path
    segs = path.split('/', -1).reject {|x| x == '.'}
    if path[0] == '/'
      discard = true
      segs[0..0] = []         # special case
    elsif path == ""
      discard = 0
      segs = nil
    else
      discard = 1             # if no scheme...
      segs = nil if segs == []
    end
    # p segs
    if segs
      opath = []
      # discard = 0             # XXX
      segs.each do |x|
        if x == '..'
          discard += 1 unless opath.pop
        else opath << x
        end
      end
    end
  end
  [uri.opaque ? true : nil, discard, opath]
end

Instance Method Details

#==(other) ⇒ Object



55
56
57
# File 'lib/cbor-cri.rb', line 55

def ==(other)
  to_item == other.to_item
end

#checkObject

copy?



61
62
63
# File 'lib/cbor-cri.rb', line 61

def check
  # TBD
end

#discard?(d = discard) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/cbor-cri.rb', line 14

def discard?(d = discard)
  true == d || (Integer === d && d >= 0)
end

#hostObject



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/cbor-cri.rb', line 151

def host
  if authority
    host_a = authority
    if Array === authority
      p = authority.last
      host_a = authority[0...-1] if Integer === p
      return host_a.join(".") unless String === host_a.first && host_a.first.encoding == Encoding::BINARY
      n_to_a(host_a.first)
    end
  end
end

#merge(other) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cbor-cri.rb', line 65

def merge(other)
  ret = dup
  if other.discard == true
    ret.path = other.path.dup
    ret.query = nil
    ret.fragment = nil
    ret.authority = null if ret.authority == true
  elsif Integer === other.discard
    ret.path = ret.path.dup
    ret.path.pop(other.discard)
    ret.path.concat(other.path) if other.path
    if other.discard != 0
      ret.query = nil
      ret.fragment = nil
    end
  end
  if other.scheme
    ret.scheme = other.scheme
    ret.authority = other.authority
  else
    ret.authority = other.authority if other.authority
  end
  if other.query
    ret.query = other.query
    ret.fragment = nil
  end
  ret.fragment = other.fragment if other.fragment
  ret
end

#n_to_a(n) ⇒ Object



143
144
145
# File 'lib/cbor-cri.rb', line 143

def n_to_a(n)
  IPAddr.new_ntoh(n).to_s
end

#portObject



163
164
165
166
167
168
169
170
# File 'lib/cbor-cri.rb', line 163

def port
  if authority
    if Array === authority
      p = authority.last
      p if Integer === p
    end
  end
end

#relpath?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/cbor-cri.rb', line 18

def relpath?
  scheme.nil?
end

#to_itemObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cbor-cri.rb', line 41

def to_item
  ret =
    if scheme || authority
      fail inspect unless discard == true
      [scheme, authority, path, query, fragment]
    else
      [discard, path, query, fragment]
    end
  while ret.size > 0 && ret[-1].nil?
    ret.pop
  end
  ret
end

#to_uriObject

XXX percent-encoding needed



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cbor-cri.rb', line 96

def to_uri
  ret = ""
  if scheme
    ret <<
      if Integer === scheme
        SCHEMES[~scheme].to_s
      else
        scheme
      end << ":"
  end
  if authority && authority != true
    ret << "//" << (host || ":::FAIL:::")
    if port
      ret << ":" << port.to_s
    end
  end
  if path
    ret << "/" if authority != true && discard == true
    ret << path.join("/")
  end
  if query
    ret << "?"
    ret << query.join("&")
  end
  if fragment
    ret << "#"
    ret << Array(fragment).join("") # XXX PET workaround
  end
  ret
end