Class: Parse::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/batch.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client = nil) ⇒ Batch

Returns a new instance of Batch.



9
10
11
12
# File 'lib/parse/batch.rb', line 9

def initialize(client = nil)
  @client = client || Parse.client
  @requests ||= []
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



7
8
9
# File 'lib/parse/batch.rb', line 7

def client
  @client
end

#requestsObject (readonly)

Returns the value of attribute requests.



6
7
8
# File 'lib/parse/batch.rb', line 6

def requests
  @requests
end

Instance Method Details

#add_request(request) ⇒ Object



14
15
16
# File 'lib/parse/batch.rb', line 14

def add_request(request)
  @requests << request
end

#class_uri(*args) ⇒ Object



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

def class_uri(*args)
  ::File.join(client.path, Parse::Protocol.class_uri(*args))
end

#create_object(object) ⇒ Object



18
19
20
21
22
23
# File 'lib/parse/batch.rb', line 18

def create_object(object)
  method = 'POST'
  path = class_uri(object.class_name)
  body = object.safe_hash
  add_request('method' => method, 'path' => path, 'body' => body)
end

#delete_object(object) ⇒ Object



32
33
34
35
# File 'lib/parse/batch.rb', line 32

def delete_object(object)
  add_request(
    'method' => 'DELETE', 'path' => class_uri(object.class_name, object.id))
end

#run!Object



41
42
43
44
45
# File 'lib/parse/batch.rb', line 41

def run!
  uri = Parse::Protocol.batch_request_uri
  body = { requests: @requests }.to_json
  @client.request(uri, :post, body)
end

#update_object(object) ⇒ Object



25
26
27
28
29
30
# File 'lib/parse/batch.rb', line 25

def update_object(object)
  method = 'PUT'
  path = class_uri(object.class_name, object.id)
  body = object.safe_hash
  add_request('method' => method, 'path' => path, 'body' => body)
end