Class: Orchestrate::Event
- Inherits:
-
Object
- Object
- Orchestrate::Event
- Defined in:
- lib/orchestrate/event.rb
Overview
Events are a way to associate Time-ordered data with a key.
Instance Attribute Summary collapse
-
#key ⇒ Orchestrate::KeyValue
readonly
The assoicated KeyValue item.
-
#key_name ⇒ String
readonly
The key of the associated item.
-
#last_request_time ⇒ Time
readonly
The time which the event was loaded.
-
#ordinal ⇒ Integer
readonly
The ordinal of the event.
-
#ref ⇒ String
readonly
The immutable ref for the current value of this event.
-
#timestamp ⇒ Integer
readonly
The timestamp of the event, in number of milliseconds since epoch.
-
#type ⇒ Orchestrate::EventType
readonly
The associated EventType.
-
#type_name ⇒ String
readonly
The associated name of the event type.
-
#value ⇒ Hash
The value for the event.
Attributes collapse
-
#[](attr_name) ⇒ nil, ...
Accessor for a property on the event's value.
-
#[]=(attr_name, attr_val) ⇒ attr_val
Set an attribute on the event's value to a specified value.
-
#path ⇒ String
The full path of the event.
-
#time ⇒ Time
The timestamp of the event as a Time object.
Persistence collapse
-
#purge ⇒ true, false
Deletes the event from Orchesrate using 'If-Match' with the current ref.
-
#purge! ⇒ true
Deletes the event from Orchesrate using 'If-Match' with the current ref.
-
#save ⇒ true, false
Saves the Event item to Orchestrate using 'If-Match' with the current ref.
-
#save! ⇒ true
Saves the Event item to Orchestrate using 'If-Match' with the current ref.
-
#update(merge) ⇒ true, false
Merges a set of values into the event's existing value and saves.
-
#update!(merge) ⇒ true
Merges a set of values into the event's existing value and saves.
Class Method Summary collapse
-
.from_bodyless_response(stem, value, response) ⇒ Event
Instantiates an Event from a value and response without a body.
-
.from_listing(stem, listing, response = nil) ⇒ Event
Instantiates an Event from a value in a listing and an associated response.
Instance Method Summary collapse
-
#<=>(other) ⇒ nil, ...
Equivalent to
String#<=>
. -
#==(other) ⇒ true, false
(also: #eql?)
Equivalent to
String#==
. -
#initialize(stem, response = nil) ⇒ Object
constructor
Instantiates a new Event item.
-
#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.
-
#to_s ⇒ String
A pretty-printed representation of the event.
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.
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
#key ⇒ Orchestrate::KeyValue (readonly)
Returns The assoicated KeyValue item.
40 41 42 |
# File 'lib/orchestrate/event.rb', line 40 def key @key end |
#key_name ⇒ String (readonly)
Returns The key of the associated item.
43 44 45 |
# File 'lib/orchestrate/event.rb', line 43 def key_name @key_name end |
#last_request_time ⇒ Time (readonly)
Returns 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 |
#ordinal ⇒ Integer (readonly)
Returns The ordinal of the event.
53 54 55 |
# File 'lib/orchestrate/event.rb', line 53 def ordinal @ordinal end |
#ref ⇒ String (readonly)
Returns The immutable ref for the current value of this event.
46 47 48 |
# File 'lib/orchestrate/event.rb', line 46 def ref @ref end |
#timestamp ⇒ Integer (readonly)
Returns The timestamp of the event, in number of milliseconds since epoch.
50 51 52 |
# File 'lib/orchestrate/event.rb', line 50 def @timestamp end |
#type ⇒ Orchestrate::EventType (readonly)
Returns The associated EventType.
34 35 36 |
# File 'lib/orchestrate/event.rb', line 34 def type @type end |
#type_name ⇒ String (readonly)
Returns The associated name of the event type.
37 38 39 |
# File 'lib/orchestrate/event.rb', line 37 def type_name @type_name end |
#value ⇒ Hash
Returns 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.
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.
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.
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. == other.ordinal <=> ordinal else other. > ? -1 : 1 end end |
#==(other) ⇒ true, false Also known as: eql?
Equivalent to String#==
. Compares by KeyValue, Type, Timestamp and Ordinal.
79 80 81 82 83 84 |
# File 'lib/orchestrate/event.rb', line 79 def ==(other) other.kind_of?(Orchestrate::Event) && \ other.type == type && \ other. == && \ other.ordinal == ordinal end |
#[](attr_name) ⇒ nil, ...
Accessor for a property on the event's value.
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.
131 132 133 |
# File 'lib/orchestrate/event.rb', line 131 def []=(attr_name, attr_val) value[attr_name.to_s] = attr_val end |
#path ⇒ String
Returns 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)}/#{}/#{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.
211 212 213 |
# File 'lib/orchestrate/event.rb', line 211 def perform(api_method, *args) type.perform(api_method, , ordinal, *args) end |
#purge ⇒ true, 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.
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.
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 |
#save ⇒ true, 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.
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.
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 |
#time ⇒ Time
Returns 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_s ⇒ String
Returns 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.
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.
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 |