Created
December 30, 2010 19:02
-
-
Save dylanmei/760138 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
module Facebooker | |
module ControllerExtensions | |
module ClassMethods | |
def facebooker_service_for_api | |
include ControllerExtensions::InstanceMethods | |
end | |
end | |
module InstanceMethods | |
def facebooker_errors | |
@facebooker_errors || [] | |
end | |
def facebook_user_from_token(token) | |
return unless token | |
begin | |
user = ... | |
rescue OAuthException => e | |
@facebooker_errors << e.message | |
end | |
end | |
end | |
end | |
end | |
Api::BaseController.class_eval do | |
extend Facebooker::ControllerExtensions::ClassMethods | |
end | |
class Api::UsersController < Api::BaseController | |
include ThoughtfulAppAuth | |
resource_controller_for_api | |
facebooker_service_for_api | |
create.before do | |
facebook_user = facebook_user_from_token params[:access_token] | |
if facebook_user | |
object.facebook_id = facebooker.id | |
object.facebook_link = facebooker.link | |
end | |
end | |
create do | |
wants.json { render :json => object.to_json(object_serialization_options) } | |
failure.wants.json { | |
render :json => object_errors.merge(facebooker_errors).to_json, :status => 422 | |
} | |
end | |
def object_serialization_options | |
{ | |
:only => [ | |
# User fields | |
:id, :first_name, :last_name, :api_key, :email, :facebook_id, | |
# Address fields | |
:firstname, :lastname, :address1, :address2, :zipcode, :state_name, :city, :phone, :name], | |
:include => { | |
:bill_address => {:include => [:country, :state]}, | |
:ship_address => {:include => [:country, :state]} | |
} | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment