Sha256: dab293df3b468fdf7534dc092139a17f8a23f40e4fb2a35a66d036806d0fa31b

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

# -*- encoding : utf-8 -*-
module AV
  class Batch
    attr_reader :requests
    attr_reader :client

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

    def add_request(request)
      @requests << request
    end

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

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

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

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

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
leancloud-ruby-client-0.1.1 lib/leancloud/batch.rb
leancloud-ruby-client-0.1.0 lib/leancloud/batch.rb