Class: Polyn::Event
- Inherits:
-
Object
- Object
- Polyn::Event
- Defined in:
- lib/polyn/event.rb
Overview
Represents an event. Events follow the [Cloudevents](github.com/cloudevents) specification.
Defined Under Namespace
Classes: UnsupportedVersionError
Constant Summary collapse
- CLOUD_EVENT_VERSION =
"1.0"
Instance Attribute Summary collapse
-
#data ⇒ String
readonly
The data.
-
#datacontenttype ⇒ String
The data content type.
-
#id ⇒ String
readonly
Event id.
-
#polyndata ⇒ Hash
readonly
as well as additional metadata.
-
#polyntrace ⇒ Array
readonly
Previous events that led to this one.
-
#source ⇒ String
readonly
Event source.
-
#specversion ⇒ String
readonly
The cloud event version.
-
#time ⇒ String
readonly
Time of event creation.
-
#type ⇒ String
readonly
Event type.
Class Method Summary collapse
- .domain ⇒ Object
-
.full_source(source = nil) ⇒ Object
Get the Event ‘source` prefixed with reverse domain name.
-
.full_type(type) ⇒ Object
Get the Event ‘type` prefixed with reverse domain name.
Instance Method Summary collapse
-
#initialize(hash) ⇒ Event
constructor
A new instance of Event.
- #to_h ⇒ Object
Constructor Details
#initialize(hash) ⇒ Event
Returns a new instance of Event.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/polyn/event.rb', line 66 def initialize(hash) @specversion = hash.key?(:specversion) ? hash[:specversion] : "1.0" unless Gem::Dependency.new("", "~> #{CLOUD_EVENT_VERSION}").match?("", @specversion) raise UnsupportedVersionError, "Unsupported version: '#{hash[:specversion]}'" end @id = hash.fetch(:id, SecureRandom.uuid) @type = self.class.full_type(hash.fetch(:type)) @source = self.class.full_source(hash[:source]) @time = hash.fetch(:time, Time.now.utc.iso8601) @data = hash.fetch(:data) @datacontenttype = hash.fetch(:datacontenttype, "application/json") @polyndata = { clientlang: "ruby", clientlangversion: RUBY_VERSION, clientversion: Polyn::VERSION, } end |
Instance Attribute Details
#data ⇒ String (readonly)
Returns the data.
55 56 57 |
# File 'lib/polyn/event.rb', line 55 def data @data end |
#datacontenttype ⇒ String
Returns the data content type.
51 52 53 |
# File 'lib/polyn/event.rb', line 51 def datacontenttype @datacontenttype end |
#id ⇒ String (readonly)
Returns event id.
35 36 37 |
# File 'lib/polyn/event.rb', line 35 def id @id end |
#polyndata ⇒ Hash (readonly)
as well as additional metadata
64 65 66 |
# File 'lib/polyn/event.rb', line 64 def polyndata @polyndata end |
#polyntrace ⇒ Array (readonly)
Returns Previous events that led to this one.
59 60 61 |
# File 'lib/polyn/event.rb', line 59 def polyntrace @polyntrace end |
#source ⇒ String (readonly)
Returns event source.
43 44 45 |
# File 'lib/polyn/event.rb', line 43 def source @source end |
#specversion ⇒ String (readonly)
Returns the cloud event version.
31 32 33 |
# File 'lib/polyn/event.rb', line 31 def specversion @specversion end |
#time ⇒ String (readonly)
Returns time of event creation.
47 48 49 |
# File 'lib/polyn/event.rb', line 47 def time @time end |
#type ⇒ String (readonly)
Returns event type.
39 40 41 |
# File 'lib/polyn/event.rb', line 39 def type @type end |
Class Method Details
.domain ⇒ Object
125 126 127 |
# File 'lib/polyn/event.rb', line 125 def self.domain Polyn.configuration.domain end |
.full_source(source = nil) ⇒ Object
Get the Event ‘source` prefixed with reverse domain name
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/polyn/event.rb', line 101 def self.full_source(source = nil) root = Polyn.configuration.source_root parts = [domain, root] combine = lambda do |items| items.map { |part| Polyn::Naming.dot_to_colon(part) }.join(":") end name = combine.call(parts) if source Polyn::Naming.validate_source_name!(source) source = source.gsub(/(#{name}){1}:?/, "") parts << source unless source.empty? end combine.call(parts) end |
.full_type(type) ⇒ Object
Get the Event ‘type` prefixed with reverse domain name
120 121 122 123 |
# File 'lib/polyn/event.rb', line 120 def self.full_type(type) Polyn::Naming.(type) "#{domain}.#{Polyn::Naming.trim_domain_prefix(type)}" end |
Instance Method Details
#to_h ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/polyn/event.rb', line 86 def to_h { "specversion" => specversion, "id" => id, "type" => type, "source" => source, "time" => time, "data" => Utils::Hash.deep_stringify_keys(data), "datacontenttype" => datacontenttype, "polyndata" => Utils::Hash.deep_stringify_keys(polyndata), } end |