Można napisać to inaczej - parsowanie signed_request

[code=ruby]# encoding: utf-8
class ApplicationController < ActionController::Base
#protect_from_forgery
protect_from_forgery
skip_before_filter :verify_authenticity_token, :only => [:parse_signed_request]

def chech_isset_app_on_page
redirect_to app_list_path unless params[:app_id] != nil && App.where([“block = ? AND active = ? AND page_id IN (?)”,
0, 1, Fanpage.where(:uid => session[‘page_id’]).pluck(:id)]).count > 0
end

def parse_signed_request
signed_request = Facebook::SignedRequest.new( params[:signed_request].to_s, secret: SECRET )
#session[‘signed_request’] = signed_request.data
#TODO aktualizujemy tabele user
if params[:signed_request]
::Rails.logger.info('Rozpoczynam parsowanie signed_request dla IP: '+request.remote_ip)
::Rails.logger.info('Signed_request przekazany z FB dla IP: '+params[:signed_request].to_s)
::Rails.logger.info('Sparsowane dane z signed_request dla IP: '+signed_request.data.inspect)

  if !session['user_id']
    session['user_id'] = signed_request.data[:user_id]
    session['user_id_app'] = User.where("uid = '"+signed_request.data[:user_id]+"'")[0].id if User.where(:uid => signed_request.data[:user_id]).count > 0
    ::Rails.logger.info('Zapisano informacje (user) dla IP: '+request.remote_ip+' session: '+session.inspect+' signed_request '+signed_request.data[:user_id].inspect)
  end
  
  if !session['oauth_token']
    session['oauth_token'] =  signed_request.data[:oauth_token]
  end

  if !session['page_id']
    session['page_id'] = signed_request.data[:page][:id]
    session['page_id_app'] =Fanpage.find_by_uid(signed_request.data[:page][:id]).id
    ::Rails.logger.info('Zapisano informacje (page) dla IP: '+request.remote_ip+' session: '+session.inspect+' signed_request '+signed_request.data[:page].inspect)
  end

end

end

end[/code]

Ja bym poszedł w tym kierunku:

[code=ruby]class ApplicationController < ActionController::Base

protect_from_forgery

skip_before_filter :verify_authenticity_token, :only => [:parse_signed_request]

def check_isset_app_on_page
found_application = params[:app_id] != nil && App.any_with_facebook_id?(session.page.facebook_id)
redirect_to app_list_path unless found_application
end

def parse_signed_request
signed_request = Facebook::SignedRequest.new( params[:signed_request].to_s, secret: SECRET )
return unless if params[:signed_request]

user_facebook_id = signed_request.data[:user_id]
oauth_token = signed_request.data[oauth_token]
page_facebook_id = signed_request.data[:page][:id]
session.store_signed_request(user_facebook_id, oauth_token, page_facebook_id)    

end

end[/code]
Klasy pomocnicze:

class SessionUser attr_accessors :id, :facebook_id end

class SessionPage attr_accessors :id, :facebook_id end

[code=ruby]class Session
attr_accessors :user, :page, :oauth_token

def initialize
@page = SessionPage.new
@user = SessionUser.new
end

def store_user(facebook_id)
@user.facebook_id = facebook_id
users = User.with_id(facebook_id).first
@user.id = users.first.id unless users.empty?
end

def store_page(facebook_id)
session.page.facebook_id = facebook_id
session.page.id = Fanpage.find_by_uid(facebook_id).id
end

def store_signed_request(user_facebook_id, oauth_token, page_facebook_id)
oauth_token = oauth_token unless session.oauth_token
store_user(user_facebook_id) unless session.user.id
store_page(page_facebook_id) unless session.page.id
end

end[/code]

[code=ruby]class Fanpage

def self.ids_from_facebook_id(facebook_id)
Fanpage.where(uid: facebook_id).pluck(:id)
end

end[/code]

[code=ruby]class App
scope :with_facebook_id, lambda { |facebook_id| where([“block = 0 AND active = 1 AND page_id IN (?)”, Fanpage.ids_from_facebook_id(facebook_id) }

def self.any_with_facebook_id?(facebook_id)
with_facebook_id(facebook_id).any?
end

end[/code]

[code=ruby]class User
scope :with_id, lambda { |user_id| where(uid: user_id }


end[/code]
P.S.
Witam wszystkich :slight_smile: