Class: Textus::Value::Envelope

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/textus/value/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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/textus/value/envelope.rb', line 23

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

.extract_sources(meta) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/textus/value/envelope.rb', line 47

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



42
43
44
45
# File 'lib/textus/value/envelope.rb', line 42

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)


83
84
85
86
87
# File 'lib/textus/value/envelope.rb', line 83

def fetching?
  return false if freshness.nil?

  freshness.fetching == true
end

#stale?Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/textus/value/envelope.rb', line 77

def stale?
  return false if freshness.nil?

  freshness.stale == true
end

#to_h_for_wireObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/textus/value/envelope.rb', line 57

def to_h_for_wire
  h = {
    "protocol" => protocol,
    "key" => key,
    "lane" => lane,
    "owner" => owner,
    "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



55
# File 'lib/textus/value/envelope.rb', line 55

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