Class: Parse::BatchOperation
- Inherits:
- Object
- Object
- Parse::BatchOperation
- Includes:
- Enumerable
- Defined in:
- lib/parse/client/batch.rb
Overview
This class provides a standard way to submit, manage and process batch operations for Parse::Objects and associations.
Batch requests are supported implicitly and intelligently through an extension of array. When an array of Parse::Object subclasses is saved, Parse-Stack will batch all possible save operations for the objects in the array that have changed. It will also batch save 50 at a time until all items in the array are saved. Note: Parse does not allow batch saving Parse::User objects.
songs = Songs.first 1000 #first 1000 songs
songs.each do |song|
# ... modify song ...
end
# will batch save 50 items at a time until all are saved.
songs.save
The objects do not have to be of the same collection in order to be supported in the batch request.
Instance Attribute Summary collapse
- #requests ⇒ Object
Returns the value of attribute requests.
- #responses ⇒ Array
The set of responses from this batch.
Instance Method Summary collapse
- #add(req) ⇒ Object
Add an additional request to this batch.
- #as_json(*args) ⇒ Hash
A formatted payload for the batch request.
- #change_requests ⇒ Object
This method is for interoperability with Parse::Object instances.
- #clear! ⇒ Array
Remove all requests in this batch.
- #client ⇒ Parse::Client
The client to be used for the request.
- #count ⇒ Integer
The number of requests in the batch.
- #each ⇒ Array
- #error? ⇒ Boolean
True if the request had an error.
- #initialize(reqs = nil) ⇒ BatchOperation constructor
A new instance of BatchOperation.
- #submit(segment = 50) ⇒ Array<Parse::Response> (also: #save)
Submit the batch operation in chunks until they are all complete.
- #success? ⇒ Boolean
True if the request was successful.
Constructor Details
#initialize(reqs = nil) ⇒ BatchOperation
Returns a new instance of BatchOperation.
52 53 54 55 56 57 | # File 'lib/parse/client/batch.rb', line 52 def initialize(reqs = nil) @requests = [] @responses = [] reqs = [reqs] unless reqs.is_a?(Enumerable) reqs.each { |r| add(r) } if reqs.is_a?(Enumerable) end |
Instance Attribute Details
#requests ⇒ Object
Returns the value of attribute requests.
# File 'lib/parse/client/batch.rb', line 39
|
#responses ⇒ Array
Returns the set of responses from this batch.
44 | # File 'lib/parse/client/batch.rb', line 44 attr_accessor :requests, :responses |
Instance Method Details
#add(req) ⇒ Array<Parse::Request> #add(batch) ⇒ Array<Parse::Request>
Add an additional request to this batch.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 | # File 'lib/parse/client/batch.rb', line 66 def add(req) if req.respond_to?(:change_requests) requests = req.change_requests.select { |r| r.is_a?(Parse::Request) } @requests += requests elsif req.is_a?(Array) requests = req.select { |r| r.is_a?(Parse::Request) } @requests += requests elsif req.is_a?(BatchOperation) @requests += req.requests if req.is_a?(BatchOperation) else @requests.push(req) if req.is_a?(Parse::Request) end @requests end |
#as_json(*args) ⇒ Hash
Returns a formatted payload for the batch request.
94 95 96 | # File 'lib/parse/client/batch.rb', line 94 def as_json(*args) { requests: requests }.as_json end |
#change_requests ⇒ Object
This method is for interoperability with Parse::Object instances.
83 84 85 | # File 'lib/parse/client/batch.rb', line 83 def change_requests @requests end |
#clear! ⇒ Array
Remove all requests in this batch.
105 106 107 | # File 'lib/parse/client/batch.rb', line 105 def clear! @requests.clear end |
#client ⇒ Parse::Client
Returns the client to be used for the request.
47 48 49 | # File 'lib/parse/client/batch.rb', line 47 def client @client ||= Parse::Client.client end |
#count ⇒ Integer
Returns the number of requests in the batch.
99 100 101 | # File 'lib/parse/client/batch.rb', line 99 def count @requests.count end |
#each ⇒ Array
88 89 90 91 | # File 'lib/parse/client/batch.rb', line 88 def each return enum_for(:each) unless block_given? @requests.each(&Proc.new) end |
#error? ⇒ Boolean
Returns true if the request had an error.
116 117 118 119 | # File 'lib/parse/client/batch.rb', line 116 def error? return false if @responses.empty? !success? end |
#submit(segment = 50) ⇒ Array<Parse::Response> Also known as: save
Submit the batch operation in chunks until they are all complete. In general, Parse limits requests in each batch to 50 and it is possible that a Parse::BatchOperation instance contains more than 50 requests. This method will slice up the array of request and send them based on the `segment` amount until they have all been submitted.
128 129 130 131 132 133 134 135 136 137 138 | # File 'lib/parse/client/batch.rb', line 128 def submit(segment = 50) @responses = [] @requests.uniq!(&:signature) @responses = @requests.each_slice(segment).to_a.threaded_map(2) do |slice| client.batch_request(BatchOperation.new(slice)) end @responses.flatten! #puts "Requests: #{@requests.count} == Response: #{@responses.count}" @requests.zip(@responses).each(&Proc.new) if block_given? @responses end |
#success? ⇒ Boolean
Returns true if the request was successful.
110 111 112 113 | # File 'lib/parse/client/batch.rb', line 110 def success? return false if @responses.empty? @responses.compact.all?(&:success?) end |