The master branch has a lot of changes that the current release doesn't have.
The version on RubyGems (0.3.0) has its corresponding tag, so use those docs instead.
Alternatively, you can set up your Gemfile
as such:
gem 'parse-ruby-client', git: 'https://github.com/adelevie/parse-ruby-client.git'
and it'll use the current master branch in your app.
In Feb 2017 we merged breakging changes in preparation for a v1.0
release. After a year of deprecation notices, we felt it was reasonable to do now. Unfortunately, not being current on rubygems this was hard to do safely and we apologize for any headache this may cause.
If you want to freeze your build and continue using the (now unsupported) Parse.init
, change your gemfile to the following line:
gem 'parse-ruby-client', git: 'https://github.com/adelevie/parse-ruby-client.git', ref: '98596a04dfc30295c1d078c58c31b4cea299e8ca'
Thanks for sticking with us!
parse-ruby-client lets you interact with Parse using Ruby. There are many uses. For example:
- a web application can show data from Parse on a website.
- you can upload large amounts of data that will later be consumed in a mobile app.
- you can download recent data to run your own custom analytics.
- you can export all of your data if you no longer want to use Parse.
gem install parse-ruby-client
or add gem "parse-ruby-client"
to your Gemfile.
We support Ruby 2.1 and newer and JRuby 9000. Older versions are not supported, though the library might still work nonetheless.
require 'parse-ruby-client'
Parse.create :application_id => "<your_app_id>", # required
:host => 'http://localhost:1337', # required
:path => '/parse', # optional, defaults to '/parse'
:master_key => "<your_master_key>", # optional, defaults to nil
:api_key => "<your_api_key>", # optional, defaults to nil
:quiet => true | false, # optional, defaults to false
:get_method_override => true | false, # optional, defaults to true
Table of Contents generated with DocToc
- Summary
- Parse App Config Parameters
- Objects
- TODO: This method is not yet implemented.
- TODO: This method is not yet implemented.
- making a few GameScore objects
- TODO: There is no Ruby object representation of this type, yet. - Future data types and namespacing
- should look something like this: - Linking
- should look something like this:
- or - Unlinking
- should look something like this:
- should look something like this:
Application config parameters are read-only and must be set via the Parse web application. However, you can access the values with:
client.application_config # => {"welcomeMessage" => "Welcome to The Internet!", "winningNumber" => 42}
The design philosophy behind parse-ruby-client is to stay out of the way as much as possible. Parse Objects, at the most basic level, act like Ruby Hash
objects with Parse-specific methods tacked on.
game_score = client.object("GameScore")
game_score["score"] = 1337
game_score["playerName"] = "Sean Plott"
game_score["cheatMode"] = false
result = game_score.save
puts result
This will return:
{"score"=>1337,
"playerName"=>"Sean Plott",
"cheatMode"=>false,
"createdAt"=>"2013-01-19T21:01:33.562Z",
"objectId"=>"GeqPWJdNqp"}
The easiest way to retrieve Objects is with client.query
:
game_score_query = client.query("GameScore")
game_score_query.eq("objectId", "GeqPWJdNqp")
game_score = game_score_query.get
puts game_score
This will return:
[{"score"=>1337,
"playerName"=>"Sean Plott",
"createdAt"=>"2013-01-19T21:01:33.562Z",
"updatedAt"=>"2013-01-19T21:01:33.562Z",
"objectId"=>"GeqPWJdNqp"}]
Notice that this is an Array
of results. For more information on queries, see Queries.
When retrieving objects that have pointers to children, you can fetch child objects by setting the include
attribute. For instance, to fetch the object pointed to by the "game" key:
game_score_query = client.query("GameScore")
game_score_query.eq("objectId", "GeqPWJdNqp")
game_score_query.include = "game"
game_score = game_score_query.get
You can include multiple children pointers by separating key names by commas:
game_score_query.include = "game,genre"
To change the data on an object that already exists, just call Parse::Object#save
on it. Any keys you don't specify will remain unchanged, so you can update just a subset of the object's data. For example, if we wanted to change the score field of our object:
game_score = client.query("GameScore").eq("objectId", "GeqPWJdNqp").get.first
game_score["score"] = 73453
result = game_score.save
puts result
This will return:
{"score"=>73453,
"playerName"=>"Sean Plott",
"createdAt"=>"2013-01-19T21:01:33.562Z",
"updatedAt"=>"2013-01-19T21:16:34.395Z",
"objectId"=>"GeqPWJdNqp"}
To help with storing counter-type data, Parse provides the ability to atomically increment (or decrement) any number field. So, we can increment the score field like so:
game_score = client.query("GameScore").eq("objectId", "GeqPWJdNqp").get.first
game_score["score"] = Parse::Increment.new(1)
game_score.save
You can also use a negative amount to decrement.
To help with storing array data, there are three operations that can be used to atomically change an array field:
Parse::Object#array_add(field, value)
appends the given array of objects to the end of an array field.Parse::Object#array_add_unique(field, value)
adds only the given objects which aren't already contained in an array field to that field. The position of the insert is not guaranteed.Parse::Object#array_remove(field, value)
removes all instances of each given object from an array field.
Each method takes an array of objects to add or remove in the "objects" key. For example, we can add items to the set-like "skills" field like so:
game_score = client.query("GameScore").eq("objectId", "5iEEIxM4MW").get.first
game_score.array_add_unique("skills", ["flying", "kungfu"])
game_score.save
puts game_score["skills"]
This will return:
[["flying", "kungfu"]]
In order to update Relation types, Parse provides special operators to atomically add and remove objects to a relation. So, we can add an object to a relation like so:
game_score = client.query("GameScore").eq("objectId", "5iEEIxM4MW").get.first
player = client.query("Player").eq("objectId", "GLtvtEaGKa").get.first
game_score.array_add_relation("opponents", player.pointer)
game_score.save
game_score["opponents"] #=> #<Parse::ArrayOp:0x007fbe98931508 @operation="AddRelation", @objects=[Player:GLtvtEaGKa]>
game_score["opponents"].objects.first #=> Player:GLtvtEaGKa
To remove an object from a relation, you can do:
game_score.array_remove_relation("opponents", player.pointer)
To delete an object from the Parse Cloud, call Parse::Object#parse_delete
. For example:
game_score = client.query("GameScore").eq("objectId", "5iEEIxM4MW").get.first
game_score.parse_delete
client.query("GameScore").eq("objectId", "5iEEIxM4MW").get.length #=> 0
You can delete a single field from an object by using the Parse::Object#delete_field
operation:
# TODO: This method is not yet implemented.
To reduce the amount of time spent on network round trips, you can create, update, or delete several objects in one call, using the batch endpoint.
parse-ruby-client provides a "manual" way to construct Batch Operations, as well as some convenience methods. The commands are run in the order they are given. For example, to create a couple of GameScore objects using the "manual" style, use Parse::Batch#add_request
. #add_request
takes a Hash
with "method"
, "path"
, and "body"
keys that specify the HTTP command that would normally be used for that command.
batch = client.batch
batch.add_request({
"method" => "POST",
"path" => "/1/classes/GameScore"
"body" => {
"score" => 1337,
"playerName" => "Sean Plott"
}
})
batch.add_request({
"method" => "POST",
"path" => "/1/classes/GameScore"
"body" => {
"score" => 1338,
"playerName" => "ZeroCool"
}
})
batch.run!
Because manually constructing "path"
values is repetitive, you can use Parse::Batch#create_object
, Parse::Batch#update_object
, and Parse::Batch#delete_object
. Each of these methods takes an instance of Parse::Object
as the only argument. Then you just call Parse::Batch#run!
. For example:
batch = client.batch
# making a few GameScore objects and adding them to the batch operation.
[1, 2, 3, 4, 5].each do |i|
gs = Parse::Object.new("GameScore")
gs["score"] = "#{i}"
batch.create_object(gs)
end
batch.run!
The response from batch will be an Array
with the same number of elements as the input list. Each item in the Array
will be a Hash
with either the "success"
or "error"
field set. The value of success will be the normal response to the equivalent REST command:
{
"success" => {
"createdAt" => "2012-06-15T16:59:11.276Z",
"objectId" => "YAfSAWwXbL"
}
}
The value of "error"
will be a Hash
with a numeric code and error string:
{
"error" => {
"code" => 101,
"error" => "object not found for delete"
}
}
So far we have only used values that can be encoded with standard JSON. The Parse mobile client libraries also support dates, binary data, and relational data. In parse-ruby-client, these values are encoded as JSON hashes with the __type field set to indicate their type, so you can read or write these fields if you use the correct encoding. See https://github.com/adelevie/parse-ruby-client/blob/master/lib/parse/datatypes.rb for implementation details of several common data types.
Use Parse::Date::new
to create a date object:
date_time = DateTime.now
parse_date = Parse::Date.new(date_time)
Dates are useful in combination with the built-in createdAt and updatedAt fields. For example, to retrieve objects created since a particular time, just encode a Date in a comparison query:
game_score = client.query("GameScore").tap do |q|
q.greater_than("createdAt", Parse::Date.new(DateTime.now)) # query options explained in more detail later in this document
end.get.first
Parse::Date::new
can take a DateTime
, iso Hash
, or a String
that can be parsed by DateTime#parse
as the sole argument.
The Parse::Date
API is not set in stone and will likely change following the suggestions discussed here: #35. The current methods probably will not go away, but some newer, easier methods will be added.
Parse::Bytes
contains an attribute, base64
, which contains a base64 encoding of binary data. The specific base64 encoding is the one used by MIME, and does not contain whitespace.
data = "TG9va3MgbGlrZSB5b3UgZm91bmQgYW4gZWFzdGVyIEVnZy4gTWF5YmUgaXQn\ncyB0aW1lIHlvdSB0b29rIGEgTWluZWNyYWZ0IGJyZWFrPw==\n" # base64 encoded data
bytes = Parse::Bytes.new(data)
The Pointer
type is used when mobile code sets a PFObject
(iOS SDK) or ParseObject
(Android SDK) as the value of another object. It contains the className
and objectId
of the referred-to value.
pointer = Parse::Pointer.new({"className" => "gameScore", "objectId" => "GeqPWJdNqp"})
Pointers to user
objects have a className
of _User
. Prefixing with an underscore is forbidden for developer-defined classes and signifies the class is a special built-in.
If you already have a Parse::Object
, you can get its Pointer
very easily:
game_score.pointer
The Relation
type is used for many-to-many relations when the mobile uses PFRelation
(iOS SDK) or ParseRelation
(Android SDK) as a value. It has a className
that is the class name of the target objects.
Note: The REST API embeds the configuration parameters in a key called 'params' which is omitted for you by the client.
# TODO: There is no Ruby object representation of this type, yet.
Though this is something parse-ruby-client will take care for you, it's worth noting:
When more data types are added, they will also be represented as hashes with a __type
field set, so you may not use this field yourself on JSON objects.
Queries are created like so:
query = client.query("GameScore")
You can retrieve multiple objects at once by calling #get
:
query.get
The return value is an Array
of Parse::Object
instances:
[{"score"=>100,
"player"=>player:qPHDUbBbjj,
"createdAt"=>"2012-10-10T00:16:10.846Z",
"updatedAt"=>"2012-10-10T00:16:10.846Z",
"objectId"=>"6ff54A5OCY"},
{"score"=>1337,
"playerName"=>"Sean Plott",
"createdAt"=>"2013-01-05T22:51:56.033Z",
"updatedAt"=>"2013-01-05T22:51:56.033Z",
"objectId"=>"MpPBAHsqNg"},
{"score"=>1337,
"playerName"=>"Sean Plott",
"createdAt"=>"2013-01-05T22:53:22.609Z",
"updatedAt"=>"2013-01-05T22:53:22.609Z",
"objectId"=>"T1dj8cWwYJ"}]]
There are several ways to put constraints on the objects found, using various methods of Parse::Query
. The most basic is Parse::Query#eq
:
query = client.query("GameScore").eq("playerName", "Sean Plott")
Other constraint methods include:
`#less_than(field, value)` | Less Than |
`#less_eq(field, value)` | Less Than or Equal To |
`#greater_than(field, value)` | Greater Than |
`#greater_eq(field, value)` | Greater Than Or Equal To |
`#not_eq(field, value)` | Not Equal To |
`#value_in(field, values)` | Contained In |
`#value_not_in(field, values)` | Not Contained in |
`#exists(field, value=true)` | A value is set for the key |
`#contains_all(field, values)` | Contains all values in the array |
`Parse::Query#select` | TODO: `$select` not yet implemented. This matches a value for a key in the result of a different query |
For example, to retrieve scores between 1000 and 3000, including the endpoints, we could issue:
scores = client.query("GameScore").tap do |q|
q.greater_eq("score", 1000)
q.less_eq("score", 3000)
end.get
To retrieve scores equal to an odd number below 10, we could issue:
scores = client.query("GameScore").tap do |q|
q.value_in("score", [1,3,5,7,9])
end.get
To retrieve scores not by a given list of players we could issue:
scores = client.query("GameScore").tap do |q|
q.value_not_in("playerName", ["Jonathan Walsh","Dario Wunsch","Shawn Simon"])
end.get
To retrieve documents with the score set, we could issue:
scores = client.query("GameScore").tap do |q|
q.exists("score") # defaults to `true`
end.get
To retrieve documents without the score set, we could issue:
scores = client.query("GameScore").tap do |q|
q.exists("score", false)
end.get
If you have a class containing sports teams and you store a user's hometown in the user class, you can issue one query to find the list of users whose hometown teams have winning records. The query would look like:
users = client.query("_User").tap do |users_query|
users_query.eq("hometown", {
"$select" => {
"query" => {
"className" => "Team",
"where" => {
"winPct" => {"$gt" => 0.5}
}
},
"key" => "city"
}
})
end.get
Currently, there is no convenience method provided for $select
queries. However, they are still possible. This is a good example of the flexibility of parse-ruby-client. You usually do not need to wait for a feature to be added in order to user it. If you have a good idea on what a convencience method for this should look like, please file an issue, or even better, submit a pull request.
You can use the Parse::Query#order_by
method to specify a field to sort by. By default, everything is ordered ascending. Thus, to retrieve scores in ascending order:
scores = client.query("GameScore").tap do |q|
q.order_by = "score"
end.get
And to retrieve scores in descending order:
scores = client.query("GameScore").tap do |q|
q.order_by = "score"
q.order = :descending
end.get
You can sort by multiple fields by passing order a comma-separated list. Currently, there is no convenience method to accomplish this. However, you can still manually construct an order
string. To retrieve documents that are ordered by scores in ascending order and the names in descending order:
scores = client.query("GameScore").tap do |q|
q.order_by = "score,-name"
end.get
You can use the limit
and skip
parameters for pagination. limit
defaults to 100, but anything from 1 to 1000 is a valid limit. Thus, to retrieve 200 objects after skipping the first 400:
scores = client.query("GameScore").tap do |q|
q.limit = 200
q.skip = 400
end.get
You can use keys
to only get specified fields back. objectId
, createdAt
, and updatedAt
are always returned, and other fields are supplied as a comma separated string.
scores = Parse::Query.new("GameScore").tap do |q|
q.keys = "score,name"
end.get
All of these parameters can be used in combination with each other.
For keys with an array type, you can find objects where the key's array value contains 2 by:
randos = client.query("RandomObject").eq("arrayKey", 2).get
You can also query that the array contains multiple objects by using contains all, for example you can return objects that have the array values 2 AND 3 by:
randos = client.query("RandomObject").eq("arrayKey", [2, 3]).get
There are several ways to issue queries for relational data. For example, if each Comment
has a Post
object in its post
field, you can fetch comments for a particular Post
:
comments = client.query("Comment").tap do |q|
q.eq("post", Parse::Pointer.new({
"className" => "Post",
"objectId" => "8TOXdXf3tz"
}))
end.get
If you want to retrieve objects where a field contains an object that matches another query, you can use the Parse::Query#in_query(field, query=nil)
method. Note that the default limit of 100 and maximum limit of 1000 apply to the inner query as well, so with large data sets you may need to construct queries carefully to get the desired behavior. For example, imagine you have Post
class and a Comment
class, where each Comment
has a relation to its parent Post
. You can find comments on posts with images by doing:
comments = client.query("Comment").tap do |comments_query|
comments_query.in_query("post", client.query("Post").tap do |posts_query|
posts_query.exists("image")
end)
end.get
Note: You must pass an instance of Parse::Query
as the second argument for Parse::Query#query_in
. You cannot manually construct queries for this.
TODO: Implement this:
If you want to retrieve objects where a field contains an object that does not match another query, you can use the $notInQuery operator. Imagine you have Post class and a Comment class, where each Comment has a relation to its parent Post. You can find comments on posts without images by doing:
If you want to retrieve objects that are members of Relation
field of a parent object, you can use the Parse::Query#related_to(field, value)
method. Imagine you have a Post
class and User
class, where each Post
can be liked by many users. If the Users
that liked a Post was stored in a Relation
on the post under the key likes, you, can the find the users that liked a particular post by:
users = client.query("_User").tap do |q|
q.related_to("likes", Parse::Pointer.new({
"className" => "Post",
"objectId" => "8TOXdXf3tz"
}))
end.get
In some situations, you want to return multiple types of related objects in one query. You can do this by passing the field to include in the include
parameter. For example, let's say you are retrieving the last ten comments, and you want to retrieve their related posts at the same time:
comments = client.query("Comment").tap do |q|
q.order_by = "createdAt"
q.order = :descending
q.limit = 10
q.include = "post"
end.get
Instead of being represented as a Pointer
, the post
field is now expanded into the whole object. __type
is set to Object
and className
is provided as well. For example, a Pointer
to a Post
could be represented as:
{
"__type" => "Pointer",
"className" => "Post",
"objectId" => "8TOXdXf3tz"
}
When the query is issued with an include
parameter for the key holding this pointer, the pointer will be expanded to:
{
"__type" => "Object",
"className" => "Post",
"objectId" => "8TOXdXf3tz",
"createdAt" => "2011-12-06T20:59:34.428Z",
"updatedAt" => "2011-12-06T20:59:34.428Z",
"otherFields" => "willAlsoBeIncluded"
}
You can also do multi level includes using dot notation. If you wanted to include the post for a comment and the post's author as well you can do:
comments = client.query("Comment").tap do |q|
q.order_by = "createdAt"
q.order = :descending
q.limit = 10
q.include = "post.author"
end.get
You can issue a query with multiple fields included by passing a comma-separated list of keys as the include parameter:
comments = client.query("Comment").tap do |q|
q.include("post,author")
comments = Parse::Query.new("Comment").tap do |q|
q.include = "post,author"
end.get
If you are limiting your query, or if there are a very large number of results, and you want to know how many total results there are without returning them all, you can use the count
parameter. For example, if you only care about the number of games played by a particular player:
count = client.query("GameScore").tap do |q|
q.eq("playerName", "Jonathan Walsh")
q.limit = 0
q.count
end.get
With a nonzero limit, that request would return results as well as the count.
If you want to find objects that match one of several queries, you can use Parse::Query#or
method, with an Array
as its value. For instance, if you want to find players with either have a lot of wins or a few wins, you can do:
players = client.query("Player").tap do |q|
q.greater_than("wins", 150)
q.or(client.query("Player").tap do |or_query|
or_query.less_than("wins, 5")
end)
end.get
Many apps have a unified login that works across the mobile app and other systems. Accessing user accounts through parse-ruby-client lets you build this functionality on top of Parse.
In general, users have the same features as other objects, such as the flexible schema. The differences are that user objects must have a username and password, the password is automatically encrypted and stored securely, and Parse enforces the uniqueness of the username
and email
fields.
Signing up a new user differs from creating a generic object in that the username
and password
fields are required. The password field is handled differently than the others; it is encrypted when stored in the Parse Cloud and never returned to any client request.
You can ask Parse to verify user email addresses in your application settings page. With this setting enabled, all new user registrations with an email
field will generate an email confirmation at that address. You can check whether the user has verified their email
with the emailVerified
field.
To sign up a new user, create a new Parse::User
object and then call #save
on it:
user = client.user({
:username => "cooldude6",
:password => "p_n7!-e8",
:phone => "415-392-0202"
})
user.save
The response body is a Parse::User
object containing the objectId
, the createdAt
timestamp of the newly-created object, and the sessionToken
which can be used to authenticate subsequent requests as this user:
{"username"=>"cooldude6",
"phone"=>"415-392-0202",
"createdAt"=>"2013-01-31T15:22:40.339Z",
"objectId"=>"2bMfWZQ9Ob",
"sessionToken"=>"zrGuvs3psdndaqswhf0smupsodflkqbFdwRs"}
After you allow users to sign up, you need to let them log in to their account with a username and password in the future. To do this, call Parse::User#authenticate(username, password)
:
user = Parse::User.authenticate("cooldude6", "p_n7!-e8", client)
The response body is a Parse::User
object containing all the user-provided fields except password
. It also contains the createdAt
, updatedAt
, objectId
, and sessionToken
fields:
{"username"=>"cooldude6",
"phone"=>"415-392-0202",
"createdAt"=>"2013-01-31T15:22:40.339Z",
"updatedAt"=>"2013-01-31T15:22:40.339Z",
"objectId"=>"2bMfWZQ9Ob",
"sessionToken"=>"uvs3aspasdnlksdasqu178qaq0smupso"}
Enabling email verification in an application's settings allows the application to reserve part of its experience for users with confirmed email addresses. Email verification adds the emailVerified
field to the User
object. When a User
's email
is set or modified, emailVerified
is set to false. Parse then emails the user a link which will set emailVerified
to true
.
There are three emailVerified
states to consider:
-
true
- the user confirmed his or her email address by clicking on the link Parse emailed them.Users
can never have atrue
value when the user account is first created. -
false
- at the time theUser
object was last refreshed, the user had not confirmed his or her email address. IfemailVerified
isfalse
, consider refreshing theUser
object. -
missing - the
User
was created when email verification was off or theUser
does not have anemail
.
You can initiate password resets for users who have emails associated with their account. To do this, use Parse::User::reset_password
:
resp = Parse::User.reset_password("coolguy@iloveapps.com", client)
puts resp #=> {}
If successful, the response body is an empty Hash
object.
You can also retrieve the contents of a user object by using Parse::Query
. For example, to retrieve the user created above:
user = client.query("_User").eq("objectId", "2bMfWZQ9Ob").get.first
The response body is a Parse::User
object containing all the user-provided fields except password
. It also contains the createdAt
, updatedAt
, and objectId
fields:
{"username"=>"cooldude6",
"phone"=>"415-392-0202",
"createdAt"=>"2013-01-31T15:22:40.339Z",
"updatedAt"=>"2013-01-31T15:22:40.339Z",
"objectId"=>"2bMfWZQ9Ob"}
TODO: Implement this!
In normal usage, nobody except the user is allowed to modify their own data. To authenticate themselves, the user must add a X-Parse-Session-Token
header to the request with the session token provided by the signup or login method.
To change the data on a user that already exists, send a PUT request to the user URL. Any keys you don't specify will remain unchanged, so you can update just a subset of the user's data. username
and password
may be changed, but the new username must not already be in use.
For example, if we wanted to change the phone number for cooldude6:
user = client.query("_User").eq("objectId", "2bMfWZQ9Ob").get.first
user["phone"] = "415-369-6201"
user.save
Currently returns the following error:
Parse::ParseProtocolError: 206: Parse::UserCannotBeAlteredWithoutSessionError
You can retrieve multiple users at once by using Parse::Query
:
users = client.query("_User").get
The return value is an Array
of Parse::User
objects:
[{"username"=>"fake_person",
"createdAt"=>"2012-04-20T20:07:32.295Z",
"updatedAt"=>"2012-04-20T20:07:32.295Z",
"objectId"=>"AAVwfClOx9"},
{"username"=>"fake_person222",
"createdAt"=>"2012-04-20T20:07:32.946Z",
"updatedAt"=>"2012-04-20T20:07:32.946Z",
"objectId"=>"0W1Gj1CXqU"}]
All of the options for queries that work for regular objects also work for user objects, so check the section on Querying Objects for more details.
TODO: Implement this!
Proposed api:
To delete a user from the Parse Cloud, call #parse_delete
on it:
user.parse_delete
TODO: Implement this! See https://parseplatform.github.io/docs/rest/guide/#linking-users
Parse allows you to link your users with services like Twitter and Facebook, enabling your users to sign up or log into your application using their existing identities. This is accomplished through the sign-up and update REST endpoints by providing authentication data for the service you wish to link to a user in the authData field. Once your user is associated with a service, the authData for the service will be stored with the user and is retrievable by logging in.
authData is a JSON object with keys for each linked service containing the data below. In each case, you are responsible for completing the authentication flow (e.g. OAuth 1.0a) to obtain the information the the service requires for linking.
Facebook authData contents:
{
"facebook" => {
"id" => "user's Facebook id number as a string",
"access_token" => "an authorized Facebook access token for the user",
"expiration_date" => "token expiration date of the format: yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
}
}
Twitter authData contents:
{
"twitter" => {
"id" => "user's Twitter id number as a string",
"screen_name" => "user's Twitter screen name",
"consumer_key" => "your application's consumer key",
"consumer_secret" => "your application's consumer secret",
"auth_token" => "an authorized Twitter token for the user with your application",
"auth_token_secret" => "the secret associated with the auth_token"
}
}
Anonymous user authData contents:
{
"anonymous" => {
"id" => "random UUID with lowercase hexadecimal digits"
}
}
Todo: Implement this!
Signing a user up with a linked service and logging them in with that service uses the same POST request, in which the authData for the user is specified. For example, to sign up or log in with a user's Twitter account:
# should look something like this:
twitter_user = Parse::User::Twitter.new({
"id" => "12345678",
"screen_name" => "ParseIt",
"consumer_key" => "SaMpLeId3X7eLjjLgWEw",
"consumer_secret" => "SaMpLew55QbMR0vTdtOACfPXa5UdO2THX1JrxZ9s3c",
"auth_token" => "12345678-SaMpLeTuo3m2avZxh5cjJmIrAfx4ZYyamdofM7IjU",
"auth_token_secret" => "SaMpLeEb13SpRzQ4DAIzutEkCE2LBIm2ZQDsP3WUU"
})
twitter_user.save
Parse then verifies that the provided authData is valid and checks to see if a user is already associated with this data. If so, it returns a status code of 200 OK and the details (including a sessionToken for the user).
With a response body like:
{
"username" => "Parse",
"createdAt" => "2012-02-28T23:49:36.353Z",
"updatedAt" => "2012-02-28T23:49:36.353Z",
"objectId" => "uMz0YZeAqc",
"sessionToken" => "samplei3l83eerhnln0ecxgy5",
"authData" => {
"twitter" => {
"id" => "12345678",
"screen_name" => "ParseIt",
"consumer_key" => "SaMpLeId3X7eLjjLgWEw",
"consumer_secret" => "SaMpLew55QbMR0vTdtOACfPXa5UdO2THX1JrxZ9s3c",
"auth_token" => "12345678-SaMpLeTuo3m2avZxh5cjJmIrAfx4ZYyamdofM7IjU",
"auth_token_secret" => "SaMpLeEb13SpRzQ4DAIzutEkCE2LBIm2ZQDsP3WUU"
}
}
}
If the user has never been linked with this account, you will instead receive a status code of 201 Created, indicating that a new user was created.
The body of the response will contain the objectId, createdAt, sessionToken, and an automatically-generated unique username. For example:
{
"username" => "iwz8sna7sug28v4eyu7t89fij",
"createdAt" => "2012-02-28T23:49:36.353Z",
"objectId" => "uMz0YZeAqc",
"sessionToken" => "samplei3l83eerhnln0ecxgy5"
}
TODO: Implement this!
Linking an existing user with a service like Facebook or Twitter uses a PUT request to associate authData with the user. For example, linking a user with a Facebook account would use a request like this:
# should look something like this:
user = client.query("_User").eq("objectId", "2bMfWZQ9Ob").get.first
user.link_to_facebook!({
"id" => "123456789",
"access_token" => "SaMpLeAAibS7Q55FSzcERWIEmzn6rosftAr7pmDME10008bWgyZAmv7mziwfacNOhWkgxDaBf8a2a2FCc9Hbk9wAsqLYZBLR995wxBvSGNoTrEaL",
"expiration_date" => "2012-02-28T23:49:36.353Z"
})
# or
user.link_to_twitter!({...})
After linking your user to a service, you can authenticate them using matching authData.
TODO: Implement this!
Unlinking an existing user with a service also uses a PUT request to clear authData from the user by setting the authData for the service to null. For example, unlinking a user with a Facebook account would use a request like this:
# should look something like this:
user = client.query("_User").eq("objectId", "2bMfWZQ9Ob").get.first
user.unlink_from_facebook!
TODO: Implement this!
When you access Parse via the REST API key, access can be restricted by ACL just like in the iOS and Android SDKs. You can still read and modify acls via the REST API, just by accessing the "ACL" key of an object.
The ACL is formatted as a JSON object where the keys are either object ids or the special key "*" to indicate public access permissions. The values of the ACL are "permission objects", JSON objects whose keys are the permission name and the value is always true.
For example, if you want the user with id "3KmCvT7Zsb" to have read and write access to an object, plus the object should be publicly readable, that corresponds to an ACL of:
{
"3KmCvT7Zsb": {
"read": true,
"write": true
},
"*": {
"read": true
}
}
If you want to access your data ignoring all ACLs, you can use the master key provided on the Dashboard. Instead of the X-Parse-REST-API-Key header, set the X-Parse-Master-Key header. For backward compatibility, you can also do master-level authentication using HTTP Basic Auth, passing the application id as the username and the master key as the password. For security, the master key should not be distributed to end users, but if you are running code in a trusted environment, feel free to use the master key for authentication.
TODO: Implement this!
See https://parseplatform.github.io/docs/rest/guide/#roles
To upload a file to Parse, use Parse::File
. You must include the "Content-Type"
parameter when instantiating. Keep in mind that files are limited to 10 megabytes. Here's a simple example that'll create a file named hello.txt
containing a string:
file = client.file({
:body => "Hello World!",
:local_filename => "hello.txt",
:content_type => "text/plain"
})
file.save
The response body is a Hash
object containing the name of the file, which is the original file name prefixed with a unique identifier in order to prevent name collisions. This means, you can save files by the same name, and the files will not overwrite one another.
{"url"=>
"http://something.com/somewhere/98f06e15-d6e6-42a9-a9cd-7d28ec98052c-hello.txt",
"name"=>"98f06e15-d6e6-42a9-a9cd-7d28ec98052c-hello.txt"}
To upload an image, the syntax is a little bit different. Here's an example that will upload the image parsers.jpg from the current directory:
photo = client.file({
:body => IO.read("test/parsers.jpg"),
:local_filename => "parsers.jpg",
:content_type => "image/jpeg"
})
photo.save
After files are uploaded, you can associate them with Parse objects:
photo = client.file({
:body => IO.read("test/parsers.jpg"),
:local_filename => "parsers.jpg",
:content_type => "image/jpeg"
})
photo.save
player_profile = client.object("PlayerProfile").tap do |p|
p["name"] = "All the Parsers"
p["picture"] = photo
end.save
TODO: Implement this!
Parse allows you to send push notifications to iOS and Android devices.
Notifications by default are set for iOS and Android. You can set certain notifications to only be sent to iOS or Android by setting the type
to ios
or android
.
For config/installation: https://parseplatform.github.io/docs/rest/guide/#push-notification
To send a notification to the "Giants" channel, as given at: https://parseplatform.github.io/docs/rest/guide/#using-channels
data = { :alert => "This is a notification from Parse" }
push = client.push(data, "Giants")
push.type = "ios"
push.save
Without a specific channel, by default it sends to all installations.
To send a notification to installations where injuryReports
is true
, as given at: https://parseplatform.github.io/docs/rest/guide/#sending-pushes-to-queries
data = { :alert => "This is a notification from Parse" }
push = client.push(data)
push.type = "ios"
query = client.query(Parse::Protocol::CLASS_INSTALLATION).eq('injuryReports', true)
push.where = query.where
push.save
installation = client.installation.tap do |i|
i.device_token = 'mobile_app_device_token'
i.device_type = 'ios'
i.channels = ['my-channel-name']
end
installation.save
installation = client.installation('objectId').get
# Same as
installation = Parse::Installation.new('objectId', client)
installation.get
installation = client.installation('objectId')
installation.channels = ['', 'my-channel-name']
installation.badge = 5
installation.save
Parse allows you to associate real-world latitude and longitude coordinates with an object. Adding a GeoPoint data type to a class allows queries to take into account the proximity of an object to a reference point. This allows you to easily do things like find out which user is closest to another user or which places are closest to a user.
To associate a point with an object you will need to embed a GeoPoint data type into your object. This is done by using a JSON object with __type set to the string GeoPoint and numeric values being set for the latitude and longitude keys. For example, to create an object containing a point under the "location" key with a latitude of 40.0 degrees and -30.0 degrees longitude:
place = client.object("PlaceObject").tap do |p|
p["location"] = Parse::GeoPoint.new({
"latitude" => 40.0,
"longitude" => -30.0
})
end.save
TODO: Implement this!
Now that you have a bunch of objects with spatial coordinates, it would be nice to find out which objects are closest to a point. This can be done by using a GeoPoint data type with query on the field using $nearSphere. Getting a list of ten places that are closest to a user may look something like:
# should look something like this:
places = client.query("PlaceObject").tap do |q|
q.near("location", {
"latitude" => 30.0,
"longitude" => -20.0
})
end.get
See https://parseplatform.github.io/docs/rest/guide/#geo-queries for the rest of the geo query types to be implemented.
At the moment there are a couple of things to watch out for:
-
Each PFObject class may only have one key with a PFGeoPoint object.
-
Points should not equal or exceed the extreme ends of the ranges. Latitude should not be -90.0 or 90.0. Longitude should not be -180.0 or 180.0. Attempting to use GeoPoint's with latitude and/or longitude outside these ranges will cause an error.
This project would not be where it is today without the generous help provided by its many contributors:
- Adam Alpern (created this project) (https://github.com/aalpern) (https://www.gittip.com/on/github/aalpern/)
- Eric Jensen (https://github.com/ericcj) (https://www.gittip.com/on/github/ericcj/)
- Ben Cherry (https://github.com/bcherry) (https://www.gittip.com/bcherry/)
- Tikhon Bernstam (https://github.com/tikhon) (https://www.gittip.com/on/github/tikhon/)
- Tony Amoyal (https://github.com/tamoyal) (https://www.gittip.com/on/github/tamoyal/)
- Glenn Goodrich (https://github.com/ruprict) (https://www.gittip.com/on/github/ruprict/)
- Jeremy Schoenherr (https://github.com/jeremyschoenherr) (https://www.gittip.com/on/github/jeremyschoenherr/)
- Dean Perry (https://github.com/deanperry) (https://www.gittip.com/deanperry/)
- vircheck (https://www.gittip.com/on/github/vircheck/)
- Jacob Eiting (https://github.com/jeiting) (https://www.gittip.com/on/github/jeiting/)
- Guy Maliar (https://github.com/gmaliar) (https://www.gittip.com/on/github/gmaliar/)
- Ivan Fuller (https://github.com/ifuller) (https://www.gittip.com/on/github/ifuller/)
- Leandro S. (https://github.com/lsiqueira) (https://www.gittip.com/on/github/lsiqueira/)
- Brian Hammond (https://github.com/fictorial) (https://www.gittip.com/on/github/fictorial/)