Sha256: 96936dbaef12f5cb458b66522b6d131662b79e6109e90b1397f7fde6ecbde906

Contents?: true

Size: 1.02 KB

Versions: 7

Compression:

Stored size: 1.02 KB

Contents

require 'json'

module Parse
  class Batch
    attr_reader :requests

    def initialize
      @requests ||= []
    end

    def add_request(request)
      @requests << request
    end

    def create_object(object)
      method = "POST"
      path = Parse::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 = Parse::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" => Parse::Protocol.class_uri(object.class_name, object.id)
      })
    end

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


  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
parse-ruby-client-0.1.15 lib/parse/batch.rb
parse-ruby-client-0.1.14 lib/parse/batch.rb
parse-ruby-client-0.1.13 lib/parse/batch.rb
parse-ruby-client-0.1.12 lib/parse/batch.rb
parse-ruby-client-0.1.11 lib/parse/batch.rb
parse-ruby-client-0.1.10 lib/parse/batch.rb
parse-ruby-client-0.1.9 lib/parse/batch.rb