require 'koala' require 'uri' 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 options = {} settings = { referral_code: params[:gm_referral_code], source: params[:utm_source], referrer: request.referer, signed_request: params[:signed_request] }.merge options facebook_data = facebook_user_data.try(:symbolize_keys) plataforma_social_data = plataforma_social_user_data(settings).try(:symbolize_keys) @user_data = { :facebook => facebook_data, :plataforma_social => plataforma_social_data } end def plataforma_social_user_data options = {} settings = { referral_code: params[:gm_referral_code], source: params[:utm_source], referrer: request.referer, signed_request: params[:signed_request] }.merge options if settings[:signed_request] response = Request.post("#{PlataformaSocial.domains('login')}/register", settings) data = {} data[:reference] = response['id'] if response.present? && response['id'] data 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 uri = URI.parse(PlataformaSocial.facebook_callback_url.to_s) callback_url = if uri && uri.scheme.present? uri.query = [uri.query, "gm_referral_code=#{params[:gm_referral_code]}", "utm_source=#{params[:utm_source]}"].compact.join('&') uri.to_s else nil end @oauth = Koala::Facebook::OAuth.new(PlataformaSocial.facebook_app_id, PlataformaSocial.facebook_app_secret, callback_url) end def load_graph @graph = Koala::Facebook::API.new parse_facebook_signed_request['oauth_token'] if parse_facebook_signed_request.present? && parse_facebook_signed_request['oauth_token'] end end end class ActionController::Base include PlataformaSocial::Facebook end