File: event.rb

package info (click to toggle)
ruby-docker-api 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 432 kB
  • sloc: ruby: 4,044; sh: 138; makefile: 5
file content (126 lines) | stat: -rw-r--r-- 2,826 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# This class represents a Docker Event.
class Docker::Event
  include Docker::Error

  # Represents the actor object nested within an event
  class Actor
    attr_accessor :ID, :Attributes

    def initialize(actor_attributes = {})
      [:ID, :Attributes].each do |sym|
        value = actor_attributes[sym]
        if value.nil?
          value = actor_attributes[sym.to_s]
        end
        send("#{sym}=", value)
      end

      if self.Attributes.nil?
        self.Attributes = {}
      end
    end

    alias_method :id, :ID
    alias_method :attributes, :Attributes
  end

  class << self
    include Docker::Error

    def stream(opts = {}, conn = Docker.connection, &block)
      conn.get('/events', opts, :response_block => lambda { |b, r, t|
        b.each_line do |line|
          block.call(new_event(line, r, t))
        end
      })
    end

    def since(since, opts = {}, conn = Docker.connection, &block)
      stream(opts.merge(:since => since), conn, &block)
    end

    def new_event(body, remaining, total)
      return if body.nil? || body.empty?
      json = Docker::Util.parse_json(body)
      Docker::Event.new(json)
    end
  end

  attr_accessor :Type, :Action, :time, :timeNano
  attr_reader :Actor
  # Deprecated interface
  attr_accessor :status, :from

  def initialize(event_attributes = {})
    [:Type, :Action, :Actor, :time, :timeNano, :status, :from].each do |sym|
      value = event_attributes[sym]
      if value.nil?
        value = event_attributes[sym.to_s]
      end
      send("#{sym}=", value)
    end

    if @Actor.nil?
      value = event_attributes[:id]
      if value.nil?
        value = event_attributes['id']
      end
      self.Actor = Actor.new(ID: value)
    end
  end

  def ID
    self.actor.ID
  end

  def Actor=(actor)
    return if actor.nil?
    if actor.is_a? Actor
      @Actor = actor
    else
      @Actor = Actor.new(actor)
    end
  end

  alias_method :type, :Type
  alias_method :action, :Action
  alias_method :actor, :Actor
  alias_method :time_nano, :timeNano
  alias_method :id, :ID

  def to_s
    if type.nil? && action.nil?
      to_s_legacy
    else
      to_s_actor_style
    end
  end

  private

  def to_s_legacy
    attributes = []
    attributes << "from=#{from}" unless from.nil?

    unless attributes.empty?
      attribute_string = "(#{attributes.join(', ')}) "
    end

    "Docker::Event { #{time} #{status} #{id} #{attribute_string}}"
  end

  def to_s_actor_style
    most_accurate_time = time_nano || time

    attributes = []
    actor.attributes.each do |attribute, value|
      attributes << "#{attribute}=#{value}"
    end

    unless attributes.empty?
      attribute_string = "(#{attributes.join(', ')}) "
    end

    "Docker::Event { #{most_accurate_time} #{type} #{action} #{actor.id} #{attribute_string}}"
  end
end