Permalink
Browse files

adding very *limited* support for User.

  • Loading branch information...
adelevie committed Feb 25, 2012
1 parent 3ad1e12 commit 5162175b49f6b5ef31ff2538af1dae313285b565
Showing with 93 additions and 1 deletion.
  1. +1 −0 lib/parse-ruby-client.rb
  2. +9 −1 lib/parse/object.rb
  3. +4 −0 lib/parse/protocol.rb
  4. +38 −0 lib/parse/user.rb
  5. +41 −0 test/test_user.rb
View
@@ -21,3 +21,4 @@
require 'parse/datatypes'
require 'parse/util'
require 'parse/protocol'
+require 'parse/user'
View
@@ -5,7 +5,7 @@
module Parse
# Represents an individual Parse API object.
- class Object < Hash
+ class Object < Hash
attr_reader :parse_object_id
attr_reader :class_name
attr_reader :created_at
@@ -70,6 +70,13 @@ def save
if data
parse data
end
+
+ if @class_name == Parse::Protocol::CLASS_USER
+ self.delete("password")
+ self.delete(:username)
+ self.delete(:password)
+ end
+
self
end
@@ -82,6 +89,7 @@ def refresh
parse data
end
end
+
self
end
View
@@ -92,6 +92,9 @@ module Protocol
# The class name for User objects, when referenced by a Pointer.
CLASS_USER = "_User"
+ USER_LOGIN_URI = "/#{VERSION}/login"
+
+ KEY_USER_SESSION_TOKEN = "sessionToken"
# URI Helpers
# ----------------------------------------
@@ -105,6 +108,7 @@ def Protocol.class_uri(class_name, object_id = nil)
"/#{VERSION}/classes/#{class_name}"
end
end
+
# Construct a uri referencing a given Parse user
# instance or the users category.
View
@@ -0,0 +1,38 @@
+require 'parse/protocol'
+require 'parse/client'
+require 'parse/error'
+require 'parse/object'
+
+module Parse
+ class User < Parse::Object
+
+ def self.authenticate(username, password)
+ body = {
+ "username" => username,
+ "password" => password
+ }
+ response = Parse.client.request(Parse::Protocol::USER_LOGIN_URI, :get, body)
+
+ new(response)
+ end
+
+ def initialize(data = nil)
+ @class_name = Parse::Protocol::CLASS_USER
+
+ #stringify symbol keys
+ data["username"] = data[:username] if data[:username]
+ data["password"] = data[:password] if data[:password]
+
+ if data
+ parse data
+ end
+
+ self
+ end
+
+ def uri
+ Protocol.user_uri @parse_object_id
+ end
+
+ end
+end
View
@@ -0,0 +1,41 @@
+require 'helper'
+
+class TestUser < Test::Unit::TestCase
+
+ def setup
+ Parse.init
+ end
+
+ def test_save
+ username = rand.to_s
+ data = {
+ :username => username,
+ :password => "topsecret"
+ }
+ user = Parse::User.new data
+ user.save
+ assert_equal user[Parse::Protocol::KEY_OBJECT_ID].class, String
+ assert_equal user[Parse::Protocol::KEY_CREATED_AT].class, String
+ end
+
+ def test_login
+ u = "alan" + rand(10000000000000).to_s
+ data = {
+ :username => u,
+ :password => "secret"
+ }
+ user = Parse::User.new(data)
+ user.save
+
+ puts user.inspect
+
+ assert_equal user["username"], u
+ assert_equal user[Parse::Protocol::KEY_USER_SESSION_TOKEN].class, String
+
+ login = Parse::User.authenticate(u, "secret")
+
+ #assert_equal login["username"], "alan123"
+ #assert_equal login["sessionToken"].class, String
+ end
+
+end

0 comments on commit 5162175

Please sign in to comment.