Giter Site home page Giter Site logo

Comments (2)

ericcj avatar ericcj commented on August 11, 2024

a simple API would be to expose save_request and parse_delete_request methods on Parse::Object that return the [uri, method, body] to be passed to Parse::Client.request by the actual save and parse_delete operations. that way you could batch.add(some_object.save_request); batch.add(another_obj.parse_delete_request)

from parse-ruby-client.

adelevie avatar adelevie commented on August 11, 2024

So this is what I've settled on for now:

# instantiate the batch
batch = Parse::Batch.new

# create
batch.create_object(Parse::Object.new("Foo"))

# update
foo = Parse::Query.new("Foo").get.first
foo["bar"] = "updated"
batch.update_object(foo)

# delete
Parse::Query.new("Foo").eg("old", true).each do |foo|
  batch.delete_object(foo)
end

# run the batch
batch.run!

I refactored Parse::Object#safe_json into Parse::Object#safe_hash. Now #safe_json just returns #safe_hash.to_json. With #safe_hash, I can construct a hash suitable for the body parameter of the batch request.

Here's the source behind Parse::Batch:

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

from parse-ruby-client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.