Created
August 5, 2011 23:02
-
-
Save craigmartin/1128733 to your computer and use it in GitHub Desktop.
example sinatra app building web services for social networking services to connect to several other apps - this is the base app, I have been refactoring it, so it may be broken.
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
require 'rubygems' | |
gem 'rest-client', '=1.6.1' | |
%w(./config haml sinatra digest/md5 rack-flash json rest-client ./models).each { |lib| require lib} | |
set :sessions, true | |
set :show_exceptions, false | |
set :environment, :development | |
use Rack::Flash | |
get "/" do | |
if session[:userid].nil? then | |
@token = "http://#{env["HTTP_HOST"]}/after_login" | |
haml :login | |
else | |
@all = @user.feed | |
haml :landing | |
end | |
end | |
get "/logout" do | |
session[:userid] = nil | |
redirect "/" | |
end | |
# called by RPX after the login completes | |
post "/after_login" do | |
profile = get_user_profile_with params[:token] | |
user = User.find(profile["identifier"]) | |
if user.new_record? | |
photo = profile["photo"].nil? ? "http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(profile["email"])}" : profile["photo"] | |
unless user.update_attributes({:nickname => profile["identifier"].hash.to_s(36), :email => profile["email"], :photo_url => photo, :provider => profile["provider"]}) | |
flash[:error] = user.errors.values.join(",") | |
redirect "/" | |
end | |
session[:userid] = user.id | |
redirect "/user/profile/change" | |
else | |
session[:userid] = user.id | |
redirect "/" | |
end | |
end | |
%w(pages friends photos messages events groups comments user helpers).each {|feature| load "#{feature}.rb"} | |
error NoMethodError do | |
session[:userid] = nil | |
redirect "/" | |
end | |
before do | |
@user = User.get(session[:userid]) if session[:userid] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment