Class: Polyn::Event

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#dataString (readonly)

Returns the data.

Returns:

  • (String)

    the data



55
56
57
# File 'lib/polyn/event.rb', line 55

def data
  @data
end

#datacontenttypeString

Returns the data content type.

Returns:

  • (String)

    the data content type



51
52
53
# File 'lib/polyn/event.rb', line 51

def datacontenttype
  @datacontenttype
end

#idString (readonly)

Returns event id.

Returns:

  • (String)

    event id



35
36
37
# File 'lib/polyn/event.rb', line 35

def id
  @id
end

#polyndataHash (readonly)

as well as additional metadata

Returns:

  • (Hash)

    Represents the information about the client that published the event



64
65
66
# File 'lib/polyn/event.rb', line 64

def polyndata
  @polyndata
end

#polyntraceArray (readonly)

Returns Previous events that led to this one.

Returns:

  • (Array)

    Previous events that led to this one



59
60
61
# File 'lib/polyn/event.rb', line 59

def polyntrace
  @polyntrace
end

#sourceString (readonly)

Returns event source.

Returns:

  • (String)

    event source



43
44
45
# File 'lib/polyn/event.rb', line 43

def source
  @source
end

#specversionString (readonly)

Returns the cloud event version.

Returns:

  • (String)

    the cloud event version



31
32
33
# File 'lib/polyn/event.rb', line 31

def specversion
  @specversion
end

#timeString (readonly)

Returns time of event creation.

Returns:

  • (String)

    time of event creation



47
48
49
# File 'lib/polyn/event.rb', line 47

def time
  @time
end

#typeString (readonly)

Returns event type.

Returns:

  • (String)

    event type



39
40
41
# File 'lib/polyn/event.rb', line 39

def type
  @type
end

Class Method Details

.domainObject



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.validate_message_name!(type)
  "#{domain}.#{Polyn::Naming.trim_domain_prefix(type)}"
end

Instance Method Details

#to_hObject



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