Permalink
Browse files

Merge branch 'release/0.4.8'

  • Loading branch information...
2 parents 6dd3842 + 584acb5 commit 373fd6c78fcc8c69336c67c6bc68458de4705a95 @potomak committed Mar 24, 2012
Showing with 272 additions and 43 deletions.
  1. +48 −0 models/user.rb
  2. +68 −28 server.rb
  3. +155 −14 spec/server_spec.rb
  4. +1 −1 version.rb
View
@@ -1,3 +1,51 @@
+# A user.
+#
+# Example user structure
+#
+# {
+# "user_info" => {
+# "name" => "Giovanni Cappellotto",
+# "nickname" => "gcappellotto",
+# "email" => nil,
+# "image" => "http://graph.facebook.com/1207768639/picture?type=square",
+# "first_name" => "Giovanni",
+# "last_name" => "Cappellotto",
+# "urls" => {
+# "Website" => nil,
+# "Facebook" => "http://www.facebook.com/gcappellotto"
+# }
+# },
+# "extra" => {
+# "user_hash" => {
+# "username" => "gcappellotto",
+# "name" => "Giovanni Cappellotto",
+# "locale" => "en_US",
+# "verified" => true,
+# "updated_time" => "2012-01-19T14:25:00+0000",
+# "timezone" => 1,
+# "link" => "http://www.facebook.com/gcappellotto",
+# "gender" => "male",
+# "first_name" => "Giovanni",
+# "id" => "1207768639",
+# "last_name" => "Cappellotto",
+# "location" => {
+# "name" => "Venice, Italy",
+# "id" => "107933505906257"
+# },
+# "hometown" => {
+# "name" => "Venice, Italy",
+# "id" => "107933505906257"
+# },
+# }
+# },
+# "credentials" => {
+# "token" => "XXX",
+# "refresh_token" => ""
+# },
+# "uid" => "1207768639",
+# "provider" => "facebook"
+# }
+
class User
def initialize(user)
@user = user
View
@@ -38,45 +38,39 @@
def is_production?
:production == settings.environment
end
-
+
def logged_in?
- not @current_user.nil?
+ !@current_user.nil? && @current_user['uid']
end
end
before do
# authentication
- @current_user = User.find_by_key(session[:user]) if session[:user]
+ if params['uid'] && params['token']
+ user = User.find(params['uid'])
+ @current_user = user if user && user['credentials'] && user['credentials']['token'] == params[:token]
+ else
+ @current_user = User.find_by_key(session[:user]) if session[:user]
+ end
+
+ # respond with json if accepted
+ content_type :json if json_request?
+
# pagination
@current_page = (params[:page] || 1).to_i
@page = @current_page - 1
end
not_found do
- case request.accept
- when 'application/json'
- "not found".to_json
- else
- haml :'shared/not_found'
- end
+ json_request? ? "not found".to_json : haml(:'shared/not_found')
end
error 403 do
- case request.accept
- when 'application/json'
- "access forbidden".to_json
- else
- haml :'shared/access_forbidden'
- end
+ json_request? ? "access forbidden".to_json : haml(:'shared/access_forbidden')
end
error 500 do
- case request.accept
- when 'application/json'
- "application error".to_json
- else
- haml :'shared/application_error'
- end
+ json_request? ? "application error".to_json : haml(:'shared/application_error')
end
def json_request?
@@ -89,8 +83,12 @@ def clear_session
def auth_or_redirect(path)
unless logged_in?
- flash[:error] = 'Please log in to perform this operation'
- redirect path
+ if json_request?
+ halt 403
+ else
+ flash[:error] = 'Please log in to perform this operation'
+ redirect path
+ end
end
end
@@ -139,7 +137,6 @@ def auth_or_redirect(path)
#
get '/users/:id' do |id|
@user = User.find(id)
- content_type :json if json_request?
if @user
@drawings = Drawing.all(:user_id => id, :page => @page, :per_page => PER_PAGE, :host => request.host)
@@ -187,7 +184,6 @@ def auth_or_redirect(path)
#
get '/drawings/:id' do |id|
@drawing = Drawing.find(id)
- content_type :json if json_request?
if @drawing
case request.accept.first
@@ -207,7 +203,6 @@ def auth_or_redirect(path)
#
post '/drawings/:id/fork' do |id|
@drawing = Drawing.find(id)
- content_type :json if json_request?
if @drawing
begin
@@ -264,7 +259,7 @@ def auth_or_redirect(path)
begin
# get access to raw POST data
- data = JSON.parse(request.env["rack.input"].read)
+ data = JSON.parse(request.body.read)
# compose drawing id
id = "#{Drawing.generate_token}.#{data['image']['frames'] ? "gif" : "png"}"
# compose drawing object
@@ -286,7 +281,7 @@ def auth_or_redirect(path)
drawing.merge(:thumb => haml(:'drawings/thumb', :layout => false, :locals => {:drawing => drawing, :id => 0})).to_json
rescue => e
puts "ERROR: #{e}\n#{e.backtrace}"
- "Sorry, an error occurred while processing your request.".to_json
+ status 500
end
end
@@ -317,6 +312,51 @@ def auth_or_redirect(path)
end
#
+# POST /authorize
+#
+post '/authorize' do
+ content_type :json
+
+ begin
+ client = FBGraph::Client.new(:client_id => FACEBOOK['app_id'], :secret_id => FACEBOOK['app_secret'], :token => params[:token])
+ me = client.selection.me.info!
+
+ raise RuntimeError if params[:uid] != me.data.id
+
+ user = {
+ :key => User.key(me.data.id),
+ :uid => me.data.id,
+ :provider => 'facebook',
+ :user_info => {
+ :first_name => me.data.first_name,
+ :last_name => me.data.last_name,
+ :image => "http://graph.facebook.com/#{me.data.id}/picture?type=square"
+ },
+ :credentials => {
+ :token => params[:token]
+ }
+ }
+ rescue => e
+ puts "ERROR: #{e}\n#{e.backtrace}"
+ halt 403
+ end
+
+ @current_user = User.new(user).save
+ @drawings = Drawing.all(:user_id => me.data.id, :page => 0, :per_page => PER_PAGE, :host => request.host)
+
+ {
+ :uid => @current_user[:uid],
+ :first_name => @current_user[:user_info][:first_name],
+ :image => @current_user[:user_info][:image]
+ }.merge({
+ :drawings => {
+ :drawings => @drawings,
+ :meta => {:current_page => 1}
+ }
+ }).to_json
+end
+
+#
# GET /about
#
get '/about' do
Oops, something went wrong.

0 comments on commit 373fd6c

Please sign in to comment.