Class: Parse::Batch

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

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Batch

Returns a new instance of Batch.



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

def initialize &block
  @blocks = []
  @blocks << block if block
end

Instance Method Details

#add_request(&block) ⇒ Object



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

def add_request &block
  @blocks << block if block
end

#runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/parse/batch.rb', line 18

def run
  default_client = Parse::Client.default
  default_http_client = default_client.http_client
  batch_http_client = Parse::BatchHttpClient.new default_http_client.host
  default_client.http_client = batch_http_client
  begin
    @blocks.map &:call
  ensure
    default_client.http_client = default_http_client
  end

  default_client.call_api :post, 'batch', %Q|{"requests":#{batch_http_client.requests.to_json}}| do |responses|
    responses.each.with_index do |r, i|
      if after_block = batch_http_client.after_blocks[i]
        if r['success']
          after_block.call r['success']
        elsif r['error']
          # do something
          raise StandardError.new(r['error'].to_s)
        end
      end
    end
  end
end

#run!Object



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

def run!
  Parse::Client.default.use_master_key do
    run
  end
end