Class: Textus::Protocol::Envelope
- Inherits:
-
Dry::Struct
- Object
- Dry::Struct
- Textus::Protocol::Envelope
- Defined in:
- lib/textus/protocol/envelope.rb
Class Method Summary collapse
- .build(key:, mentry:, path:, meta:, body:, etag:, content: nil, freshness: nil) ⇒ Object
- .extract_sources(meta) ⇒ Object
- .extract_uid(meta) ⇒ Object
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(), sources: extract_sources(), etag:, schema_ref: mentry.schema, 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() v = .is_a?(Hash) ? ["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() v = .is_a?(Hash) ? ["uid"] : nil v.is_a?(String) ? v : nil end |
Instance Method Details
#fetching? ⇒ Boolean
54 |
# File 'lib/textus/protocol/envelope.rb', line 54 def fetching? = freshness.is_a?(Hash) && freshness["fetching"] == true |
#stale? ⇒ Boolean
52 |
# File 'lib/textus/protocol/envelope.rb', line 52 def stale? = freshness.is_a?(Hash) && freshness["stale"] == true |
#to_h_for_wire ⇒ Object
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" => , "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)) |