Class: Syncano::BatchQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/syncano/batch_queue.rb

Overview

Class representing queues used in batch requests

Constant Summary collapse

REQUEST_LIMIT =

Limit for amount of batch operations send in one request

10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ BatchQueue

Constructor for Syncano::BatchQueue

Parameters:



10
11
12
13
14
15
# File 'lib/syncano/batch_queue.rb', line 10

def initialize(client)
  super()
  self.client = client
  self.queue = []
  self.responses = []
end

Instance Attribute Details

#responsesObject

Returns the value of attribute responses.



6
7
8
# File 'lib/syncano/batch_queue.rb', line 6

def responses
  @responses
end

Instance Method Details

#<<(element) ⇒ Object

Alias for “add” method

Parameters:



26
27
28
# File 'lib/syncano/batch_queue.rb', line 26

def <<(element)
  add(element)
end

#add(element) ⇒ Object

Adds element to the queue and prune it if is full

Parameters:



19
20
21
22
# File 'lib/syncano/batch_queue.rb', line 19

def add(element)
  self.queue << element
  prune! while full?
end

#countInteger

Counts elements in the queue

Returns:

  • (Integer)


32
33
34
# File 'lib/syncano/batch_queue.rb', line 32

def count
  queue.count
end

#full?TrueClass, FalseClass

Checks if queue is full

Returns:

  • (TrueClass, FalseClass)


38
39
40
# File 'lib/syncano/batch_queue.rb', line 38

def full?
  count >= REQUEST_LIMIT
end

#prune!Array

Prunes queue and makes batch request to the api

Returns:

  • (Array)

    collection of Syncano::Response objects



44
45
46
47
48
49
50
51
# File 'lib/syncano/batch_queue.rb', line 44

def prune!
  part = self.queue.slice!(0, 10)
  ::Jimson::Client.batch(client) do |batch_client|
    part.each do |element|
      element.perform!(batch_client)
    end
  end.collect { |response| self.responses << response }
end