Comments (2)
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.
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)
- Why does this require bundler to run? HOT 2
- How to initialize Parse key HOT 3
- Parse Retirement HOT 10
- Can't use github master version HOT 5
- "both URI are relative" when configuring after self-hosted Parse server HOT 4
- unable to create client from console HOT 1
- Google OAuth Configuration
- client key shouldn't be required HOT 3
- Query can end up doing a POST! HOT 10
- Remove support for Ruby 1.9 and 2.0 HOT 4
- Running with local parse server HOT 2
- client.query - error client is undefined method in the contoller HOT 7
- back4app URL HOT 6
- Switch off parse.com HOT 3
- Switch tests to a hosted Parse server
- Multiple Coveralls comments
- Future of this project HOT 2
- Does Parse::Cloud::Function.new Still Work? HOT 2
- user = User.authenticate stopped working HOT 5
- NoMethodError occuered during retry request. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from parse-ruby-client.