Class: Zaikio::Loom::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/zaikio/loom/event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, subject:, id: nil, link: nil, payload: nil, receiver: nil, timestamp: nil, version: nil) ⇒ Event

rubocop:disable Metrics/AbcSize, Metrics/ParameterLists



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/zaikio/loom/event.rb', line 10

def initialize(name, subject:, id: nil, link: nil, payload: nil, receiver: nil, timestamp: nil, version: nil) # rubocop:disable Metrics/AbcSize, Metrics/ParameterLists
  @event_name = name.to_s.count(".").zero? ? "#{configuration.apps.values.first.app_name}.#{name}" : name.to_s
  @id         = id || SecureRandom.uuid
  @link       = link
  @payload    = payload
  @receiver   = receiver
  @subject    = subject
  @timestamp  = timestamp
  @version    = version || configuration.version

  return if app_password

  configuration.logger.error("Zaikio::Loom is disabled – event password is missing")
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/zaikio/loom/event.rb', line 8

def id
  @id
end

#response_bodyObject (readonly)

Returns the value of attribute response_body.



8
9
10
# File 'lib/zaikio/loom/event.rb', line 8

def response_body
  @response_body
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



8
9
10
# File 'lib/zaikio/loom/event.rb', line 8

def status_code
  @status_code
end

Instance Method Details

#fireObject

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/zaikio/loom/event.rb', line 25

def fire # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  log_event

  return false unless app_password && configuration.host

  uri = URI("#{configuration.host}/api/v1/events")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"

  response = http.request(build_request(uri))

  @status_code   = response.code.to_i
  @response_body = response.body

  unless response.is_a?(Net::HTTPSuccess)
    raise Zaikio::Loom::Error.new(
      "Sending event failed (#{@status_code}): #{@response_body}",
      body:        @response_body,
      status_code: @status_code
    )
  end

  response.is_a?(Net::HTTPSuccess)
end

#to_hObject

rubocop:disable Metrics/MethodLength



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/zaikio/loom/event.rb', line 51

def to_h # rubocop:disable Metrics/MethodLength
  timestamp = @timestamp || Time.now.getutc

  {
    id:        @id,
    name:      @event_name,
    subject:   @subject,
    timestamp: timestamp.iso8601,
    receiver:  @receiver,
    version:   @version,
    payload:   @payload,
    link:      @link
  }.compact
end