require 'koala' module PlataformaSocial module Facebook def facebook_login load_oauth load_graph redirect = false if params[:code] begin @oauth.get_access_token(params[:code]) rescue Koala::Facebook::OAuthTokenRequestError redirect = true if parse_facebook_signed_request && !parse_facebook_signed_request['oauth_token'] end else redirect = true if parse_facebook_signed_request && !parse_facebook_signed_request['oauth_token'] end if parse_facebook_signed_request && parse_facebook_signed_request['oauth_token'] if !session[:not_verify_facebook_permissions] === true permissions = @graph.get_object('me', :fields => 'permissions')['permissions']['data'][0] session[:not_verify_facebook_permissions] = true PlataformaSocial.facebook_scope.split(',').each do |permission| redirect = true unless permissions[permission.strip] end end end render :text => %|| if redirect end def load_user_data facebook_data = facebook_user_data.symbolize_keys if facebook_user_data plataforma_social_data = plataforma_social_user_data.symbolize_keys if plataforma_social_user_data @user_data = { :facebook => facebook_data, :plataforma_social => plataforma_social_data } end def plataforma_social_user_data if params[:signed_request] data = Request.post("#{PlataformaSocial.domains('login')}/register", { :signed_request => params[:signed_request], :social_ref => params[:social_ref] }) { :reference => data['id'] } end end def facebook_user_data if load_graph @graph.get_object('me') end end def parse_facebook_signed_request load_oauth @oauth.parse_signed_request params[:signed_request] if params[:signed_request].present? end def load_oauth @oauth = Koala::Facebook::OAuth.new(PlataformaSocial.facebook_app_id, PlataformaSocial.facebook_app_secret, PlataformaSocial.facebook_callback_url) end def load_graph @graph = Koala::Facebook::API.new parse_facebook_signed_request['oauth_token'] if parse_facebook_signed_request.present? end end end class ActionController::Base include PlataformaSocial::Facebook end