Parsecom
Yet-Another Parse.com Library written in Pure Ruby
Usage
Preparing
Before using the library, you should import this and set your credentials on the library.
require 'parsecom'
Parse.credentials application_id:'YOUR APPID', api_key:'YOUR APIKEY'
Declaring Parse Classes
There are three ways to declare a parse class.
First, you can declare a ruby class inherited from Parse::Object. By using this way, you can add your own properties and methods to the class.
class GameScore < Parse::Object
# ..snip..
end
Secondly, you can also declare your parse class by calling the Parse::Object method.
Parse::Object(:GameScore)
It returns a parse class, so that you can call its class methods directly.
Parse::Object(:GameScore).find :limit => 3
Lastly, Parse::Object class provides create method for you to declare new class.
Parse::Object.create :GameScore
It may suitable for writing code in declarative style.
Creating Objects
To create new parse object, juse new and save the object.
game_score = GameScore.new
game_score.score = 1337
game_score.playerName = 'Sean Plott'
game_score.cheatMode = false
game_score.new? # => true
game_score.save
game_score.new? # => false
game_score.obj_id # => 'Ed1nuqPvcm'
Retrieving Objects
There are two ways to retrieve objects. One is using Query objects directly and another is using Parse::Object as a facade of a query object.
# useing Query object directly
query = Parse::Query.new GameScore
query.where :objectId => 'Ed1nuqPvcm'
results = query.invoke
# using Query object through Parse::Object
results = GameScore.find :where => {:objectId => 'Ed1nuqPvcm'}
# if you would like to find by objectId, you can easily pass it directly
result = GameScore.find 'Ed1nuqPvcm'
More complex query
# useing Query object directly
results = Parse::Query.new(GameScore).
limit(10).
order(score).
where do
column(:score).gte(1000).lte(3000)
column(:cheatMode).eq(false)
end.
invoke
# using Query object through Parse::Object
results = GameScore.find :limit => 10, :order => 'score',
:where => proc{
column(:score).gte(1000).lte(3000)
column(:cheatMode).eq(false)
}
To know more about retrieving objects, see spec/parse_query_spec.rb
Updating Objects
To update attributes, just update the attribute and save.
result = GameScore.find 'Ed1nuqPvcm'
result.score = 73453
result.save
If you want to update attributes without retrieving the object, you can use the Parse::Client object for it.
score = GameScore.new :objectId => 'Ed1nuqPvcm'
Parse::Client.default_client.update score, :score => 73453
Deleting Objects
TBD
Sign up
user = Parse::User.sign_up 'YOUR USERNAME', 'YOUR PASSWORD'
Log in
user = Parse::User.log_in 'YOUR USERNAME', 'YOUR PASSWORD'