Skip to content

Instantly share code, notes, and snippets.

@ewoodh2o
Created March 20, 2012 01:45
Show Gist options
  • Save ewoodh2o/2129806 to your computer and use it in GitHub Desktop.
Save ewoodh2o/2129806 to your computer and use it in GitHub Desktop.
Sample Like Gate Coupon in Rails
class FacebookTabController < ApplicationController
ssl_allowed :show, :channel
before_filter :load_auth, :except => [ :channel ]
def show
render is_fan? ? 'coupon' : 'like_gate'
end
## Facebook Channel File
## Assists with cross-site XHR and IFRAME issues
def channel
headers['Pragma'] = 'public'
headers['Cache-Control'] = "max-age=#{10*365*24*60*60}"
headers['Expires'] = 10.years.from_now.utc.to_s(:rfc1123)
render :text => "<script src=\"//connect.facebook.net/en_US/all.js\"></script>"
end
#################################################
## Facebook Helpers
#################################################
def facebook_tab_config
@@_fb_tab_config ||= YAML.load(File.open(File.join(Rails.root, 'config', 'facebook_tab.yml')))
if Rails.env.development?
if request.host.include?('elliott')
@@_fb_tab_config['dev-elliott'].symbolize_keys
end
else
@@_fb_tab_config[Rails.env].symbolize_keys
end
end
helper_method :facebook_tab_config
def is_fan?
return false unless @auth.present?
@auth.data['page']['liked']
end
helper_method :is_fan?
##################################################
## FB Authorization Filter
##################################################
private
def load_auth
return unless params[:signed_request].present?
@auth = FbGraph::Auth.new(facebook_tab_config[:app_id], facebook_tab_config[:app_secret])
@auth.from_signed_request(params[:signed_request])
end
end
<!-- You can put your main landing page content here. -->
<div id="coupon">
<div id="deal">
<div class="amount">10% Off</div>
<div class="sub">Your next purchase</div>
<div class="details">Coupon Code: <span class="code">fblike2012</span></div>
</div>
<a id="redeem" href="http://<%= facebook_tab_config[:domain] %>/?from=facebook_coupon" target="_top">Redeem</a>
</div>
<%= render :partial => 'marketing' %>
<div id="call-to-action">Like us to receive a free coupon!</div>
<%= render :partial => 'marketing' %>
<!DOCTYPE html>
<html>
<head>
<title>Your Like-Gate Coupon on Facebook</title>
<%= stylesheet_link_tag 'facebook_tab' %>
</head>
<body>
<%= yield %>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<%= facebook_tab_config[:app_id] %>', // App ID
channelURL : '//<%= facebook_tab_config[:domain] %>/facebook-tab/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
FB.Canvas.setAutoGrow();
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
</body>
</html>
dev-elliott:
app_id: '123456'
app_secret: 'abcdef'
domain: app.elliott.pipe.example.com
staging:
app_id: '123456'
app_secret: 'abcdef'
domain: www.app.com
production:
app_id: '123456'
app_secret: 'abcdef'
domain: www.app.com
YourApplication::Application.routes.draw do
match 'facebook-tab', :to => 'facebook_tab#show', :via => [ :get, :post ]
match 'facebook-tab/channel', :to => 'facebook_tab#channel', :via => [ :get, :post ]
# ... rest of your routes
end
source 'http://rubygems.org'
gem 'rails', '3.0.10'
# ... include whatever you need, but the following is pertinent
gem 'fb_graph'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment