Class: TsipParser::Uri

Inherits:
Object
  • Object
show all
Defined in:
lib/tsip_parser/uri.rb

Overview

‘TsipParser::Uri` is a TypedData wrapper around a Rust `tsip_parser::Uri`. Scalar accessors (`scheme`, `user`, …) are Rust methods defined by the native extension. This file adds the Ruby-side concerns: memoizing `params` / `headers` so mutations stick, plus serialization / equality.

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



70
71
72
73
74
# File 'lib/tsip_parser/uri.rb', line 70

def ==(other)
  other.is_a?(Uri) &&
    scheme == other.scheme && user == other.user &&
    host.downcase == other.host.downcase && port == other.port
end

#append_bracket_host(buf) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/tsip_parser/uri.rb', line 61

def append_bracket_host(buf)
  h = host
  if h.include?(":") && !h.start_with?("[")
    buf << "[" << h << "]"
  else
    buf << h
  end
end

#append_to(buf) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tsip_parser/uri.rb', line 31

def append_to(buf)
  buf << scheme << ":"
  u = user
  if u
    buf << u
    pw = password
    buf << ":" << pw if pw
    buf << "@"
  end
  append_bracket_host(buf)
  p = port
  buf << ":" << p.to_s if p
  params.each do |k, v|
    buf << ";" << k
    vs = v.to_s
    buf << "=" << vs unless vs.empty?
  end
  h = headers
  unless h.empty?
    buf << "?"
    first = true
    h.each do |k, v|
      buf << "&" unless first
      first = false
      buf << k << "=" << v.to_s
    end
  end
  buf
end

#hashObject



76
77
78
# File 'lib/tsip_parser/uri.rb', line 76

def hash
  [scheme, user, host.downcase, port].hash
end

#headersObject



17
18
19
20
# File 'lib/tsip_parser/uri.rb', line 17

def headers
  return @headers if defined?(@headers)
  @headers = _rust_headers
end

#paramsObject

Materialize the params Hash lazily. Once touched, subsequent reads —and any in-place mutations — hit the cached ivar, so tsip-core-style ‘uri.params = “tls”` patterns keep working.



12
13
14
15
# File 'lib/tsip_parser/uri.rb', line 12

def params
  return @params if defined?(@params)
  @params = _rust_params
end

#to_sObject



22
23
24
25
26
27
28
29
# File 'lib/tsip_parser/uri.rb', line 22

def to_s
  # Hot path: nothing has been touched, so the Rust-side `Display` impl
  # reproduces the canonical string without ever materializing a Hash.
  return _rust_to_s unless defined?(@params) || defined?(@headers)
  out = +""
  append_to(out)
  out
end