Class: Orchestrate::Event

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

Overview

Events are a way to associate Time-ordered data with a key.

Instance Attribute Summary collapse

Attributes collapse

Persistence collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stem, response = nil) ⇒ Object

Instantiates a new Event item. You generally don't want to call this yourself, but use accessor methods from the KeyValue and EventType.

Parameters:


67
68
69
70
71
72
73
74
# File 'lib/orchestrate/event.rb', line 67

def initialize(stem, response=nil)
  @type = stem
  @type_name = type.type
  @key = stem.kv_item
  @key_name = key.key
  @value = nil
  load_from_response(response) if response
end

Instance Attribute Details

#keyOrchestrate::KeyValue (readonly)

Returns The assoicated KeyValue item.

Returns:


40
41
42
# File 'lib/orchestrate/event.rb', line 40

def key
  @key
end

#key_nameString (readonly)

Returns The key of the associated item.

Returns:

  • (String)

    The key of the associated item.


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

def key_name
  @key_name
end

#last_request_timeTime (readonly)

Returns The time which the event was loaded.

Returns:

  • (Time)

    The time which the event was loaded.


56
57
58
# File 'lib/orchestrate/event.rb', line 56

def last_request_time
  @last_request_time
end

#ordinalInteger (readonly)

Returns The ordinal of the event.

Returns:

  • (Integer)

    The ordinal of the event.


53
54
55
# File 'lib/orchestrate/event.rb', line 53

def ordinal
  @ordinal
end

#refString (readonly)

Returns The immutable ref for the current value of this event.

Returns:

  • (String)

    The immutable ref for the current value of this event.


46
47
48
# File 'lib/orchestrate/event.rb', line 46

def ref
  @ref
end

#timestampInteger (readonly)

Returns The timestamp of the event, in number of milliseconds since epoch.

Returns:

  • (Integer)

    The timestamp of the event, in number of milliseconds since epoch.

See Also:


50
51
52
# File 'lib/orchestrate/event.rb', line 50

def timestamp
  @timestamp
end

#typeOrchestrate::EventType (readonly)

Returns The associated EventType.

Returns:


34
35
36
# File 'lib/orchestrate/event.rb', line 34

def type
  @type
end

#type_nameString (readonly)

Returns The associated name of the event type.

Returns:

  • (String)

    The associated name of the event type.


37
38
39
# File 'lib/orchestrate/event.rb', line 37

def type_name
  @type_name
end

#valueHash

Returns The value for the event.

Returns:

  • (Hash)

    The value for the event.


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

def value
  @value
end

Class Method Details

.from_bodyless_response(stem, value, response) ⇒ Event

Instantiates an Event from a value and response without a body.

Parameters:

  • stem (Orchestrate::EventType)

    The associated EventType.

  • value (Hash)

    The value for the event.

  • response (API::Response)

    The associated response for creating the event.

Returns:


12
13
14
15
16
# File 'lib/orchestrate/event.rb', line 12

def self.from_bodyless_response(stem, value, response)
  event = new(stem, response)
  event.value = value
  event
end

.from_listing(stem, listing, response = nil) ⇒ Event

Instantiates an Event from a value in a listing and an associated response.

Parameters:

  • stem (Orchestrate::EventType)

    The associated EventType

  • listing (Hash)

    The entry in the LIST result

  • response (API::Response) (defaults to: nil)

    The associated response for the list query.

Returns:


23
24
25
26
27
28
29
30
31
# File 'lib/orchestrate/event.rb', line 23

def self.from_listing(stem, listing, response=nil)
  event = new(stem)
  event.value = listing['value']
  event.instance_variable_set(:@timestamp, listing['path']['timestamp'])
  event.instance_variable_set(:@ordinal, listing['path']['ordinal'])
  event.instance_variable_set(:@ref, listing['path']['ref'])
  event.instance_variable_set(:@last_request_time, response.request_time) if response
  event
end

Instance Method Details

#<=>(other) ⇒ nil, ...

Equivalent to String#<=>. Compares by KeyValue, Type, Timestamp and Ordinal. Sorts newer timestamps before older timestamps. If timestamps are equivalent, sorts by ordinal. This behavior emulates the order which events are returned in a list events query from the Orchestrate API.

Parameters:

Returns:

  • (nil, -1, 0, 1)

93
94
95
96
97
98
99
100
101
# File 'lib/orchestrate/event.rb', line 93

def <=>(other)
  return nil unless other.kind_of?(Orchestrate::Event)
  return nil unless other.type == type
  if other.timestamp == timestamp
    other.ordinal <=> ordinal
  else
    other.timestamp > timestamp ? -1 : 1
  end
end

#==(other) ⇒ true, false Also known as: eql?

Equivalent to String#==. Compares by KeyValue, Type, Timestamp and Ordinal.

Parameters:

Returns:

  • (true, false)

79
80
81
82
83
84
# File 'lib/orchestrate/event.rb', line 79

def ==(other)
  other.kind_of?(Orchestrate::Event) && \
    other.type == type && \
    other.timestamp == timestamp && \
    other.ordinal == ordinal
end

#[](attr_name) ⇒ nil, ...

Accessor for a property on the event's value.

Parameters:

  • attr_name (#to_s)

    the attribute name to retrieve.

Returns:

  • (nil, true, false, Numeric, String, Array, Hash)

    The value for the attribute.


123
124
125
# File 'lib/orchestrate/event.rb', line 123

def [](attr_name)
  value[attr_name.to_s]
end

#[]=(attr_name, attr_val) ⇒ attr_val

Set an attribute on the event's value to a specified value.

Parameters:

  • attr_name (#to_s)

    The name of the attribute.

  • attr_val (#to_json)

    The new value for the attribute

Returns:

  • (attr_val)

131
132
133
# File 'lib/orchestrate/event.rb', line 131

def []=(attr_name, attr_val)
  value[attr_name.to_s] = attr_val
end

#pathString

Returns The full path of the event.

Returns:

  • (String)

    The full path of the event.


111
112
113
# File 'lib/orchestrate/event.rb', line 111

def path
  @path ||= "/#{key.path}/events/#{URI.escape(type_name)}/#{timestamp}/#{ordinal}"
end

#perform(api_method, *args) ⇒ API::Request

Performs a request using the associated API client, with collection_name, key, event_type, timestamp, and ordinal pre-filled.

Parameters:

  • api_method (Symbol)

    the method on API::Client to call.

  • args (#to_s, #to_json, Hash)

    The remaining arguments for the method being called.

Returns:

  • (API::Request)

211
212
213
# File 'lib/orchestrate/event.rb', line 211

def perform(api_method, *args)
  type.perform(api_method, timestamp, ordinal, *args)
end

#purgetrue, false

Deletes the event from Orchesrate using 'If-Match' with the current ref. Returns false if the event failed to delete because a new ref has been created since this Event was loaded.

Returns:

  • (true, false)

186
187
188
189
190
191
192
# File 'lib/orchestrate/event.rb', line 186

def purge
  begin
    purge!
  rescue API::RequestError
    false
  end
end

#purge!true

Deletes the event from Orchesrate using 'If-Match' with the current ref.

Returns:

  • (true)

Raises:


197
198
199
200
201
202
# File 'lib/orchestrate/event.rb', line 197

def purge!
  response = perform(:purge_event, ref)
  @ref = nil
  @last_request_time = response.request_time
  true
end

#savetrue, false

Saves the Event item to Orchestrate using 'If-Match' with the current ref. Sets the new ref for this value to the ref attribute. Returns false on failure to save, and rescues from all Orchestrate::API errors.

Returns:

  • (true, false)

142
143
144
145
146
147
148
# File 'lib/orchestrate/event.rb', line 142

def save
  begin
    save!
  rescue API::RequestError, API::ServiceError
    false
  end
end

#save!true

Saves the Event item to Orchestrate using 'If-Match' with the current ref. Sets the new ref for this value to the ref attribute. Raises an exception on failure to save.

Returns:

  • (true)

Raises:


156
157
158
159
160
# File 'lib/orchestrate/event.rb', line 156

def save!
  response = perform(:put_event, value, ref)
  load_from_response(response)
  true
end

#timeTime

Returns The timestamp of the event as a Time object.

Returns:

  • (Time)

    The timestamp of the event as a Time object.


116
117
118
# File 'lib/orchestrate/event.rb', line 116

def time
  @time ||= Time.at(@timestamp / 1000.0)
end

#to_sString

Returns A pretty-printed representation of the event.

Returns:

  • (String)

    A pretty-printed representation of the event.


104
105
106
# File 'lib/orchestrate/event.rb', line 104

def to_s
  "#<Orchestrate::Event collection=#{key.collection_name} key=#{key_name} type=#{type_name} time=#{time} ordinal=#{ordinal} ref=#{ref} last_request_time=#{last_request_time}>"
end

#update(merge) ⇒ true, false

Merges a set of values into the event's existing value and saves.

Parameters:

  • merge (#each_pair)

    The Hash-like to merge into #value. Keys will be stringified.

Returns:

  • (true, false)

165
166
167
168
169
170
171
# File 'lib/orchestrate/event.rb', line 165

def update(merge)
  begin
    update!(merge)
  rescue API::RequestError, API::ServiceError
    false
  end
end

#update!(merge) ⇒ true

Merges a set of values into the event's existing value and saves.

Parameters:

  • merge (#each_pair)

    The Hash-like to merge into #value. Keys will be stringified.

Returns:

  • (true)

Raises:


178
179
180
181
# File 'lib/orchestrate/event.rb', line 178

def update!(merge)
  merge.each_pair {|key, value| @value[key.to_s] = value }
  save!
end