Class: Orchestrate::EventType::TimeSlice

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/orchestrate/event_source.rb

Overview

Manages events in a specified duration of time.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, bounds = nil) ⇒ TimeSlice

Instantiates a new TimeSlice

Parameters:

  • type (EventType)

    The associated EventType

  • bounds (nil, String, Integer, Time, Date, Hash) (defaults to: nil)

    Boundaries for the Time If nil, no boundaries are set. Used to enumerate over all events. If String, Integer, Time, Date, used as :start option, below. If Hash, see options.

Options Hash (bounds):

  • :start (nil, String, Integer, Time, Date)

    Used as the 'startEvent' key.

  • :after (nil, String, Integer, Time, Date)

    Used as the 'afterEvent' key.

  • :before (nil, String, Integer, Time, Date)

    Used as the 'beforeEvent' key.

  • :end (nil, String, Integer, Time, Date)

    Used as the 'endEvent' key.

  • :limit (Integer) — default: 100

    The number of results to return.



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/orchestrate/event_source.rb', line 193

def initialize(type, bounds=nil)
  @type = type
  @type_name = type.type
  @kv_item = type.kv_item
  if bounds.kind_of?(Hash)
    @bounds = bounds
  else
    @bounds = {}
    @bounds[:start] = bounds if bounds
  end
  @bounds[:limit] ||= 100
end

Instance Attribute Details

#kv_itemKeyValue (readonly)

Returns The associated KeyValue.

Returns:

  • (KeyValue)

    The associated KeyValue



180
181
182
# File 'lib/orchestrate/event_source.rb', line 180

def kv_item
  @kv_item
end

#typeEventType (readonly)

Returns The associated EventType.

Returns:



174
175
176
# File 'lib/orchestrate/event_source.rb', line 174

def type
  @type
end

#type_nameString (readonly)

Returns The associated event type name.

Returns:

  • (String)

    The associated event type name



177
178
179
# File 'lib/orchestrate/event_source.rb', line 177

def type_name
  @type_name
end

Instance Method Details

#<<(body) ⇒ Orchestrate::Event Also known as: push

Creates a new Event of the given type for the associated KeyValue.

Parameters:

  • body (#to_json)

    The value for the event.

Returns:



218
219
220
221
# File 'lib/orchestrate/event_source.rb', line 218

def <<(body)
  response = perform(:post_event, body, @bounds[:start])
  Event.from_bodyless_response(type, body, response)
end

#[](ordinal) ⇒ Orchestrate::Event

Retrieves a single ID by timestamp and ordinal. Uses the timestamp value from the associated bounds, by start, before, after, or end in that order.

Parameters:

  • ordinal (Integer, #to_s)

    The ordinal value for the event to retrieve.

Returns:



229
230
231
232
233
234
235
236
237
# File 'lib/orchestrate/event_source.rb', line 229

def [](ordinal)
  begin
    timestamp = @bounds[:start] || @bounds[:before] || @bounds[:after] || @bounds[:end]
    response = perform(:get_event, timestamp, ordinal)
    Event.new(type, response)
  rescue API::NotFound
    nil
  end
end

#after(bound) ⇒ EventType::TimeSlice

Sets the exclusive start boundary for enumeration over events. Overwrites any value given to #start

Parameters:

Returns:



295
296
297
298
299
# File 'lib/orchestrate/event_source.rb', line 295

def after(bound)
  @bounds[:after] = extract_bound_from(bound)
  @bounds.delete(:start)
  self
end

#before(bound) ⇒ EventType::TimeSlice

Sets the exclusive end boundary for enumeration over events. Overwrites any value given to #end

Parameters:

Returns:



305
306
307
308
309
# File 'lib/orchestrate/event_source.rb', line 305

def before(bound)
  @bounds[:before] = extract_bound_from(bound)
  @bounds.delete(:end)
  self
end

#each {|event| ... } ⇒ Object

Iterates over events belonging to the KeyValue of the specified Type within the given bounds. Used as the basis for enumerable methods. Events are provided in reverse chronological order by timestamp and ordinal value.

Yield Parameters:

Raises:



249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/orchestrate/event_source.rb', line 249

def each(&block)
  @response = perform(:list_events, @bounds)
  return enum_for(:each) unless block_given?
  raise ResultsNotReady.new if type.kv_item.collection.app.inside_parallel?
  loop do
    @response.results.each do |listing|
      yield Event.from_listing(type, listing, @response)
    end
    break unless @response.next_link
    @response = @response.next_results
  end
  @response = nil
end

#end(bound) ⇒ EventType::TimeSlice

Sets the inclusive end boundary for enumeration over events. Overwrites any value given to #before

Parameters:

Returns:



315
316
317
318
319
# File 'lib/orchestrate/event_source.rb', line 315

def end(bound)
  @bounds[:end] = extract_bound_from(bound)
  @bounds.delete(:before)
  self
end

#lazyObject

Creates a Lazy Enumerator for the EventList. If called inside the app's #in_parallel block, will prefetch results.

Returns:

  • Enumerator::Lazy



266
267
268
269
# File 'lib/orchestrate/event_source.rb', line 266

def lazy
  return each.lazy if type.kv_item.collection.app.inside_parallel?
  super
end

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

Calls a method on the associated API Client, with collection, key and event_type being provided by EventType.

Parameters:

  • api_method (Symbol)

    The method to call

  • args (#to_s, #to_json, Hash)

    The arguments for the method being called.

Returns:



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

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

#start(bound) ⇒ EventType::TimeSlice

Sets the inclusive start boundary for enumeration over events. Overwrites any value given to #before.

Parameters:

Returns:



285
286
287
288
289
# File 'lib/orchestrate/event_source.rb', line 285

def start(bound)
  @bounds[:start] = extract_bound_from(bound)
  @bounds.delete(:after)
  self
end

#take(count) ⇒ Array

Returns the first n items. Equivalent to Enumerable#take. Sets the limit parameter on the query to Orchestrate, so we don't ask for more than is needed.

Parameters:

  • count (Integer)

    The number of events to limit to.

Returns:

  • (Array)


275
276
277
278
279
# File 'lib/orchestrate/event_source.rb', line 275

def take(count)
  count = 1 if count < 1
  @bounds[:limit] = count > 100 ? 100 : count
  super(count)
end