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 parse_facebook_signed_request['expires'] > Time.now.to_i # Se o access_token do usuário não estiver expirado if !session[:not_verify_facebook_permissions] == true begin 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 rescue Koala::Facebook::AuthenticationError redirect = true # Remove o signed_request da URL já que ele está expirado e recarrega o objeto do @oauth if params[:callback_url].present? params[:callback_url].gsub!("signed_request=#{params[:signed_request]}", "") elsif request.present? request.fullpath.gsub!("signed_request=#{params[:signed_request]}", "") else PlataformaSocial.facebook_callback_url.to_s.gsub!("signed_request=#{params[:signed_request]}", "") end load_oauth end end else redirect = true end end if redirect render :text => %|| end end def load_user_data options = {} settings = { referral_code: params[:gm_referral_code], source: params[:utm_source], utm: { source: params[:utm_source], medium: params[:utm_medium], campaign: params[:utm_campaign] }, 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], utm: { source: params[:utm_source], medium: params[:utm_medium], campaign: params[:utm_campaign] }, 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 if params[:callback_url].present? callback_url = params[:callback_url] elsif request.present? callback_url = "#{request.protocol}#{request.host_with_port}#{request.fullpath}" else callback_url = PlataformaSocial.facebook_callback_url.to_s end uri = URI.parse(callback_url) callback_url = if uri && uri.scheme.present? uri.query = [ uri.query, "gm_referral_code=#{URI.encode(params[:gm_referral_code] || '')}", "utm_source=#{URI.encode(params[:utm_source] || '')}", "source=#{URI.encode(params[:utm_source] || '')}", "campaign=#{URI.encode(params[:utm_campaign] || '')}", "utm_campaign=#{URI.encode(params[:utm_campaign] || '')}", "medium=#{URI.encode(params[:utm_medium] || '')}", "utm_medium=#{URI.encode(params[:utm_medium] || '')}" ].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