Class: Syncano::BatchQueue
- Inherits:
-
Object
- Object
- Syncano::BatchQueue
- 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
-
#responses ⇒ Object
readonly
Returns the value of attribute responses.
Instance Method Summary collapse
-
#<<(element) ⇒ Object
Alias for “add” method.
-
#add(element) ⇒ Object
Adds element to the queue and prune it if is full.
-
#count ⇒ Integer
Counts elements in the queue.
-
#full? ⇒ TrueClass, FalseClass
Checks if queue is full.
-
#initialize(client) ⇒ BatchQueue
constructor
Constructor for Syncano::BatchQueue.
-
#prune! ⇒ Array
Prunes queue and makes batch request to the api.
Constructor Details
#initialize(client) ⇒ BatchQueue
Constructor for Syncano::BatchQueue
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
#responses ⇒ Object
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
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
19 20 21 22 |
# File 'lib/syncano/batch_queue.rb', line 19 def add(element) self.queue << element prune! while full? end |
#count ⇒ Integer
Counts elements in the queue
32 33 34 |
# File 'lib/syncano/batch_queue.rb', line 32 def count queue.count end |
#full? ⇒ TrueClass, FalseClass
Checks if queue is full
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
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 |