Class: Textus::Protocol::Envelope

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/textus/protocol/envelope.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(key:, mentry:, path:, meta:, body:, etag:, content: nil, freshness: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/textus/protocol/envelope.rb', line 20

def self.build(key:, mentry:, path:, meta:, body:, etag:, content: nil, freshness: nil)
  new(
    protocol: Textus::PROTOCOL,
    key:,
    lane: mentry.lane,
    path:,
    format: mentry.format,
    uid: extract_uid(meta),
    sources: extract_sources(meta),
    etag:,
    schema_ref: mentry.schema,
    meta: meta || {},
    body:,
    content:,
    freshness:,
  )
end

.extract_sources(meta) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/textus/protocol/envelope.rb', line 61

def self.extract_sources(meta)
  v = meta.is_a?(Hash) ? meta["sources"] : nil
  return nil unless v.is_a?(Array) && !v.empty?

  valid = v.select { |s| s.is_a?(String) || (s.is_a?(Hash) && s["key"].is_a?(String)) }
  valid.empty? ? nil : valid
end

.extract_uid(meta) ⇒ Object



56
57
58
59
# File 'lib/textus/protocol/envelope.rb', line 56

def self.extract_uid(meta)
  v = meta.is_a?(Hash) ? meta["uid"] : nil
  v.is_a?(String) ? v : nil
end

Instance Method Details

#fetching?Boolean

Returns:

  • (Boolean)


54
# File 'lib/textus/protocol/envelope.rb', line 54

def fetching? = freshness.is_a?(Hash) && freshness["fetching"] == true

#stale?Boolean

Returns:

  • (Boolean)


52
# File 'lib/textus/protocol/envelope.rb', line 52

def stale? = freshness.is_a?(Hash) && freshness["stale"] == true

#to_h_for_wireObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/textus/protocol/envelope.rb', line 40

def to_h_for_wire
  h = {
    "protocol" => protocol, "key" => key, "lane" => lane,
    "path" => path, "format" => format, "_meta" => meta,
    "body" => body, "etag" => etag, "schema_ref" => schema_ref, "uid" => uid
  }
  h["sources"] = sources if sources
  h["content"] = content unless content.nil?
  freshness&.to_h_for_wire&.each { |k, v| h[k] = v }
  h
end

#with(**attrs) ⇒ Object



38
# File 'lib/textus/protocol/envelope.rb', line 38

def with(**attrs) = self.class.new(to_h.merge(attrs))