Created
August 16, 2012 14:46
-
-
Save danielholmstrom/3370729 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
require 'koala' | |
class Safeshare < Padrino::Application | |
register SassInitializer | |
register Padrino::Rendering | |
register Padrino::Mailer | |
register Padrino::Helpers | |
use Rack::Parser | |
enable :sessions | |
def fb_authenticator | |
@fb_authenticator ||= Koala::Facebook::OAuth.new(settings.facebook_app_id, settings.facebook_secret, url_for("/auth/facebook/callback")) | |
end | |
## | |
# Caching support | |
# | |
# register Padrino::Cache | |
# enable :caching | |
# | |
# You can customize caching store engines: | |
# | |
# set :cache, Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1)) | |
# set :cache, Padrino::Cache::Store::Memcache.new(::Dalli::Client.new('127.0.0.1:11211', :exception_retry_limit => 1)) | |
# set :cache, Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0)) | |
# set :cache, Padrino::Cache::Store::Memory.new(50) | |
# set :cache, Padrino::Cache::Store::File.new(Padrino.root('tmp', app_name.to_s, 'cache')) # default choice | |
# | |
## | |
# Application configuration options | |
# | |
# set :raise_errors, true # Raise exceptions (will stop application) (default for test) | |
# set :dump_errors, true # Exception backtraces are written to STDERR (default for production/development) | |
# set :show_exceptions, true # Shows a stack trace in browser (default for development) | |
# set :logging, true # Logging in STDOUT for development and file for production (default only for development) | |
# set :public_folder, "foo/bar" # Location for static assets (default root/public) | |
# set :reload, false # Reload application files (default in development) | |
# set :default_builder, "foo" # Set a custom form builder (default 'StandardFormBuilder') | |
# set :locale_path, "bar" # Set path for I18n translations (default your_app/locales) | |
# disable :sessions # Disabled sessions by default (enable if needed) | |
# disable :flash # Disables sinatra-flash (enabled by default if Sinatra::Flash is defined) | |
# layout :my_layout # Layout can be in views/layouts/foo.ext or views/foo.ext (default :application) | |
# | |
## | |
# You can configure for a specified environment like: | |
# | |
# configure :development do | |
# set :foo, :bar | |
# disable :asset_stamp # no asset timestamping for dev | |
# end | |
# | |
## | |
# You can manage errors like: | |
# | |
# error 404 do | |
# render 'errors/404' | |
# end | |
# | |
# error 505 do | |
# render 'errors/505' | |
# end | |
# | |
# | |
error Koala::Facebook::APIError do | |
session[:fb_access_token] = nil | |
redirect "/auth/facebook" | |
end | |
before do | |
# Always serve over HTTPS in production | |
if settings.environment == :production && request.scheme != 'https' | |
redirect "https://#{request.env['HTTP_HOST']}" | |
end | |
# Setup facebook | |
if settings.environment == :production | |
@fb_graph = Koala::Facebook::API.new(session[:fb_access_token]) | |
@fb_app = @fb_graph.get_object(settings.facebook_app_id) | |
if session[:fb_access_token] | |
@fb_user = @fb_graph.get_object("me") | |
@fb_friends = @fb_graph.get_connections('me', 'friends') | |
else | |
logger.info 'fb_access_token is NOT set' | |
end | |
else | |
setup_mock() | |
end | |
end | |
get '/' do | |
@entries = [] | |
if @fb_user | |
@entries = Entry.where(:fb_owner_id=>@fb_user['id']) | |
end | |
render :index | |
end | |
# used by Canvas apps - redirect the POST to be a regular GET | |
post '/' do | |
redirect '/' | |
end | |
get '/auth/facebook' do | |
session[:fb_access_token] = nil | |
redirect fb_authenticator.url_for_oauth_code(:permissions => settings.facebook_scope) | |
end | |
get '/auth/facebook/callback' do | |
logger.info 'Setting fb_access_token' | |
session[:fb_access_token] = fb_authenticator.get_access_token(params[:code]) | |
redirect '/' | |
end | |
def setup_mock | |
# Setup mock data during development | |
logger.info 'Setting up mock' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment