Created
January 22, 2009 01:12
-
-
Save atduskgreg/50366 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# INSTRUCTIONS: | |
# - start thusly: ruby meebo_api_demo.rb -e production | |
# DEPENDENCIES: | |
# sudo gem install do_mysql dm-core | |
# sudo gem install sinatra | |
# sudo gem install launchy | |
# sudo gem install json | |
# suod gem install hoe | |
require 'rubygems' | |
require 'sinatra' | |
require 'dm-core' | |
require 'digest/md5' | |
require 'json' | |
if Sinatra.env == :production | |
puts "configuring db for production environment" | |
DataMapper.setup(:default, { | |
:adapter => 'mysql', | |
:host => 'localhost', | |
:username => '<my meebo db username>', | |
:password => '<my meebo db password>', | |
:database => 'my_legacy_db' | |
}) | |
else | |
DataMapper.setup(:default, 'mysql://localhost/meebo_auth_test') | |
end | |
class Member | |
include DataMapper::Resource | |
property :uid, Integer | |
property :firstname, String | |
property :lastname, String | |
def name | |
"#{self.firstname} #{self.lastname}" | |
end | |
end | |
class MembersFriend | |
include DataMapper::Resource | |
property :id, Integer, :key => true | |
property :uid, Integer | |
property :fid, Integer | |
property :approved, Boolean | |
def friend_uid | |
self.fid | |
end | |
def friend_name | |
Member.first( :uid => self.fid ).name | |
end | |
end | |
class Account | |
include DataMapper::Resource | |
property :id, Integer, :key => true | |
property :email, String | |
property :password, String | |
property :small_pic, String | |
property :member_type, String | |
def check_password pass | |
self.password == Digest::MD5.hexdigest( pass ) | |
end | |
def name | |
Member.first( :uid => self.id ).name | |
end | |
def friendships | |
MembersFriend.all( :uid => self.id, :approved => true ).collect do |mf| | |
{:uid => mf.friend_uid, :name => mf.friend_name} | |
end | |
end | |
end | |
# set the content type to json (except for hack for test harness) | |
before do | |
content_type 'application/json' unless request.env["REQUEST_PATH"] == "/" | |
end | |
error do | |
{:stat => "fail", :errorcode => 500, :msg => "Frankie has sloughed off this mortal coil..."}.to_json | |
end | |
# uncomment to turn on basic auth (holy shit this is slick!) | |
use Rack::Auth::Basic do |username, password| | |
username == '<my special username>' && password == '<my secret password>' | |
end | |
helpers do | |
def ok( params=nil ) | |
result = {:stat => "ok"} | |
result.merge!( {:data => params} ) if params | |
result.to_json | |
end | |
def couldnt_authenticate | |
{:stat => "ok", :authenticated => false}.to_json | |
end | |
def authenticate account | |
begin | |
if account && account.check_password( params["password"] ) | |
ok( {:authenticated => true, :uid => account.id , :name => account.name} ) | |
else | |
couldnt_authenticate | |
end | |
rescue Exception | |
{:stat => "fail", :errorcode => 500, :msg => "Died trying to authenticate the account."}.to_json | |
end | |
end | |
def exists? account | |
begin | |
if account | |
ok( :exists => true, :uid => account.id, :name => account.name ) | |
else | |
ok( :exsits => false ) | |
end | |
rescue Exception | |
{:stat => "fail", :errorcode => 500, :msg => "Died trying to find the account."}.to_json | |
end | |
end | |
end | |
#========= BEGIN URI HANDLING | |
post '/v1/authenticateusername' do | |
authenticate Account.first( :email => params["username"] ) | |
end | |
post '/v1/authenticateuid' do | |
authenticate Account.first( :id => params["uid"] ) | |
end | |
post '/v1/authenticatetoken' do | |
couldnt_authenticate | |
end | |
get '/v1/usernameexists' do | |
exists? Account.first( :email => params["username"] ) | |
end | |
get '/v1/uidexists' do | |
exists? Account.first( :id => params["uid"] ) | |
end | |
get '/v1/getfriends' do | |
account = Account.first( :id => params["uid"] ) | |
ok( {:friendresponse => "full", :friends => account.friendships} ) | |
end | |
get '/v1/getinfo' do | |
account = Account.first( :id => params["uid"] ) | |
ok( ( {:imageurl => "http://<my app name>.com/#{account.small_pic}"} if account.small_pic ) ) | |
end | |
post( '/v1/statuschange' ){ ok } | |
post( '/v1/notificationsread' ){ ok } | |
post( '/v1/addfriendrequest' ){ ok } | |
post( '/v1/blocked' ){ ok } | |
post( '/v1/abusereported' ){ ok } | |
get '/' do | |
erb <<-TEMPLATE | |
<html> | |
<head> | |
</head> | |
<body> | |
<h2>authenticateusername</h2> | |
<div class="textbox"> | |
<form action="/v1/authenticateusername" method="POST"> | |
Username: <input type="text" name="username" /> <br/> | |
Password: <input type="password" name="password" /> <br/> | |
<input type="hidden" name="test" value="main" /> | |
<input type="hidden" name="partner" value="<my app name>"> | |
<input type="submit" value="Run test" /> | |
</form> | |
</div> | |
<h2>authenticateuid</h2> | |
<div class="textbox"> | |
<form action="/v1/authenticateuid" method="POST"> | |
Uid: <input type="text" name="username" /> <br/> | |
Password: <input type="password" name="password" /> <br/> | |
<input type="hidden" name="test" value="main" /> | |
<input type="hidden" name="partner" value="<my app name>"> | |
<input type="submit" value="Run test" /> | |
</form> | |
</div> | |
<h2>authenticatetoken</h2> | |
<div class="textbox"> | |
<form action="/v1/authenticatetoken" method="POST"> | |
Uid: <input type="text" name="uid" /> <br/> | |
Token: <input type="text" name="token" /> <br/> | |
<input type="hidden" name="test" value="authenticatetoken" /> | |
<input type="hidden" name="partner" value="<my app name>"> | |
<input type="submit" value="Run test" /> | |
</form> | |
</div> | |
<h2>getinfo</h2> | |
<div class="textbox"> | |
<form action="/v1/getinfo" method="GET"> | |
Uid: <input type="text" name="uid" /> <br/> | |
<input type="hidden" name="test" value="authenticatetoken" /> | |
<input type="hidden" name="partner" value="<my app name>"> | |
<input type="submit" value="Run test" /> | |
</form> | |
</div> | |
<h2>usernameexists</h2> | |
<div class="textbox"> | |
<form action="/v1/usernameexists" method="GET"> | |
Username: <input type="text" name="username" /> <br/> | |
<input type="hidden" name="test" value="usernameexists" /> | |
<input type="hidden" name="partner" value="<my app name>"> | |
<input type="submit" value="Run test" /> | |
</form> | |
</div> | |
<h2>uidexists</h2> | |
<div class="textbox"> | |
<form action="/v1/uidexists" method="GET"> | |
Uid: <input type="text" name="uid" /> <br/> | |
<input type="hidden" name="test" value="uidexists" /> | |
<input type="hidden" name="partner" value="<my app name>"> | |
<input type="submit" value="Run test" /> | |
</form> | |
</div> | |
</body> | |
</html> | |
TEMPLATE | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment