11# frozen_string_literal: true
22
33# This class represents a Docker Event.
4+ # @see https://github.com/moby/moby/blob/master/api/types/events/events.go
5+ # @see https://docs.docker.com/reference/api/engine/version/v1.49/#tag/System/operation/SystemEvents
46class Docker ::Event
57 include Docker ::Error
68
79 # Represents the actor object nested within an event
810 class Actor
9- attr_accessor :ID , :Attributes
11+ attr_reader :info
1012
11- def initialize ( actor_attributes = { } )
12- [ :ID , :Attributes ] . each do |sym |
13- value = actor_attributes [ sym ]
14- if value . nil?
15- value = actor_attributes [ sym . to_s ]
16- end
17- send ( "#{ sym } =" , value )
18- end
13+ def initialize ( actor_data = { } )
14+ @info = actor_data . transform_keys { |k | k . downcase . to_sym }
15+ end
1916
20- if self . Attributes . nil?
21- self . Attributes = { }
22- end
17+ def id
18+ info [ :id ]
2319 end
20+ alias_method :ID , :id
2421
25- alias_method :id , :ID
26- alias_method :attributes , :Attributes
22+ def attributes
23+ info [ :attributes ] || { }
24+ end
25+ alias_method :Attributes , :attributes
2726 end
2827
2928 class << self
@@ -43,52 +42,61 @@ def since(since, opts = {}, conn = Docker.connection, &block)
4342
4443 def new_event ( body , remaining , total )
4544 return if body . nil? || body . empty?
46- json = Docker ::Util . parse_json ( body )
47- Docker ::Event . new ( json )
45+ info = Docker ::Util . parse_json ( body )
46+ Docker ::Event . new ( info )
4847 end
4948 end
5049
51- attr_accessor :Type , :Action , :time , :timeNano
52- attr_reader :Actor
53- # Deprecated interface
54- attr_accessor :status , :from
55-
56- def initialize ( event_attributes = { } )
57- [ :Type , :Action , :Actor , :time , :timeNano , :status , :from ] . each do |sym |
58- value = event_attributes [ sym ]
59- if value . nil?
60- value = event_attributes [ sym . to_s ]
61- end
62- send ( "#{ sym } =" , value )
63- end
50+ attr_reader :info
6451
65- if @Actor . nil?
66- value = event_attributes [ :id ]
67- if value . nil?
68- value = event_attributes [ 'id' ]
69- end
70- self . Actor = Actor . new ( ID : value )
71- end
52+ def initialize ( event_data = { } )
53+ @info = event_data . transform_keys { |k | k . downcase . to_sym }
7254 end
7355
74- def ID
75- self . actor . ID
56+ def action
57+ info [ :action ]
7658 end
59+ alias_method :Action , :action
7760
78- def Actor = ( actor )
79- return if actor . nil?
80- if actor . is_a? Actor
81- @Actor = actor
82- else
83- @Actor = Actor . new ( actor )
84- end
61+ def actor
62+ @actor = Actor . new ( info [ :actor ] || { } ) if !defined? @actor
63+ @actor
64+ end
65+ alias_method :Actor , :actor
66+
67+ def from
68+ # @deprecated Use `actor.attributes['image']` instead
69+ # Only applicable to container events. See Docker docs for details.
70+ info [ :from ] || actor . attributes [ 'image' ]
71+ end
72+
73+ def id
74+ # @deprecated Use `actor.id` instead
75+ info [ :id ] || actor . id
76+ end
77+
78+ def scope
79+ info [ :scope ]
80+ end
81+
82+ def status
83+ # @deprecated Use `action` instead
84+ info [ :status ] || action
8585 end
8686
87- alias_method :type , :Type
88- alias_method :action , :Action
89- alias_method :actor , :Actor
90- alias_method :time_nano , :timeNano
91- alias_method :id , :ID
87+ def time
88+ info [ :time ]
89+ end
90+
91+ def time_nano
92+ info [ :timenano ]
93+ end
94+ alias_method :timeNano , :time_nano
95+
96+ def type
97+ info [ :type ]
98+ end
99+ alias_method :Type , :type
92100
93101 def to_s
94102 if type . nil? && action . nil?
0 commit comments