Class: LC::Batch

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client = LC.client) ⇒ Batch

Returns a new instance of Batch.



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

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/leancloud/batch.rb', line 5

def client
  @client
end

#requestsObject (readonly)

Returns the value of attribute requests.



4
5
6
# File 'lib/leancloud/batch.rb', line 4

def requests
  @requests
end

Instance Method Details

#add_request(request) ⇒ Object



12
13
14
# File 'lib/leancloud/batch.rb', line 12

def add_request(request)
  @requests << request
end

#create_object(object) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/leancloud/batch.rb', line 16

def create_object(object)
  method = "POST"
  path = LC::Protocol.class_uri(object.class_name)
  body = object.safe_hash
  add_request({
    "method" => method,
    "path" => path,
    "body" => body
  })
end

#delete_object(object) ⇒ Object



38
39
40
41
42
43
# File 'lib/leancloud/batch.rb', line 38

def delete_object(object)
  add_request({
    "method" => "DELETE",
    "path" => LC::Protocol.class_uri(object.class_name, object.id)
  })
end

#run!Object



45
46
47
48
49
# File 'lib/leancloud/batch.rb', line 45

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

#update_object(object) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/leancloud/batch.rb', line 27

def update_object(object)
  method = "PUT"
  path = LC::Protocol.class_uri(object.class_name, object.id)
  body = object.safe_hash
  add_request({
    "method" => method,
    "path" => path,
    "body" => body
  })
end