Class: Parse::BatchOperation

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(reqs = nil) ⇒ BatchOperation

Returns a new instance of BatchOperation.

Parameters:



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

#requestsObject

Returns the value of attribute requests.

# File 'lib/parse/client/batch.rb', line 39

#responsesArray

Returns the set of responses from this batch.

Returns:

  • (Array)

    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.

Overloads:



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.

Returns:

  • (Hash)

    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_requestsObject

This method is for interoperability with Parse::Object instances.

See Also:

  • Object#change_requests


83
84
85
# File 'lib/parse/client/batch.rb', line 83

def change_requests
  @requests
end

#clear!Array

Remove all requests in this batch.

Returns:



105
106
107
# File 'lib/parse/client/batch.rb', line 105

def clear!
  @requests.clear
end

#clientParse::Client

Returns the client to be used for the request.

Returns:



47
48
49
# File 'lib/parse/client/batch.rb', line 47

def client
  @client ||= Parse::Client.client
end

#countInteger

Returns the number of requests in the batch.

Returns:

  • (Integer)

    the number of requests in the batch.



99
100
101
# File 'lib/parse/client/batch.rb', line 99

def count
  @requests.count
end

#eachArray

Returns:



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.

Returns:

  • (Boolean)

    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.

Parameters:

  • segment (Integer) (defaults to: 50)

    the number of requests to send in each batch. Default 50.

Returns:



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.

Returns:

  • (Boolean)

    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