Sha256: b32d8bc2f25c084dfe27a362cb49cc4a7d061001f80a7d430823c3c322ee87c7

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

require 'json'

module Parse
  class Batch
    attr_reader :requests
    attr_reader :client

    def initialize(client = Parse.client)
      @client = client
      @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
      @client.request(uri, :post, body)
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
parse-ruby-client-0.2.0 lib/parse/batch.rb