Facebook Rails starter kit
This gem leverages the gem: 'joey'. Joey in turn sits on top of 'koala' and uses the 'hashie' gem for easy hash access (as returned by the Facebook graph API via Koala).
In an initializer or similar "boot location" for your app facebook integration:
. = MyCool::FacebookAppConfig.instance
You can then define an app class that implements the following API.
Note: You may wan to load these values from a yaml file, and have the yaml filename as part of your
.gitignore. You might want to look at 'facebooker2' gem for inspiration here (fx to support multiple environments). You have to have (or create) a Facebook App that is linked to your Rails application.
class MyCool class FacebookAppConfig include Singleton # please change! def id '219868431409649' end # please change! def secret '7e5699f155df01d8e52b35c01dccd627' end # please change for staging/production def url 'http://localhost:3000' end # please customize (see Facebook permissions) # https://developers.facebook.com/docs/authentication/permissions/ def default_permissions ["publish_stream", "read_stream", "email"] end end end
facebook.yml file in
id: 219868431409649 secret: 7e5699f155df01d8e52b35c01dccd627 url: http://localhost:3000 default_permissions: ["publish_stream", "read_stream", "email"]
And here how to load it into the AppConfig singleton.
class MyCool class FacebookAppConfig include Singleton def loader @loader ||= ::. 'apis', 'facebook.yml' end def secret loader.load.secret end end end
Or using the special Delegator module included. This module requires a #config method to be defined like so:
class MyCool class FacebookAppConfig include Singleton include :: def config @config ||= ::. 'apis', 'facebook.yml' end end end
Here a call to any method not defined on this app config, will be delegated to the loader, which returns the yaml as a Hashie for direct method access. This would allow you to:
Facebook.app.secret # => value loaded from 'secret' entry in facebook.yml
Facebook access controllers
Facebook::Access::Helper can be included in controllers that require Facebook access (via signin).
class CampaignController < ApplicationController include :::: def signup fb_login end end
Some of the key methods made available are:
You can now access the Facebook graph API for the current (session) user.
For this to work, it requires a previous Facebook login which can be done fx via the Facebook login button (see fx 'facebook-social_plugins' gem). Alternatively use OAuth directly.
Facebook Graph API
fb_graph method returns a class with some nice convenience methods. The
graph api used is
Koala::Facebook::GraphAndRestAPI from the
joey gem, which uses Hashie (see `hashie gem) under the covers for easy access into the hashes returned.
The following methods are all prefixed with 'my_'
my_first_name and so on.
Currently only the following post API is provided
Facebook (and alternative OmniAuth) Authentication via Devise
In your ApplicationController or some other class/module included in your
can override the
new_user method if you need to.
# override as needed def new_user User.new end
Then simply include the
Facebook::Auth::Devise module in your controller.
class AuthenticationsController < InheritedResources::Base include :::: def new_user Profile.new end end
This module adds controller REST methods for:
The following translation keys must be defined in a locale file (or similar i18n translation):
You can easily customize this module to suit your app by overriding any of the following methods:
# in your User class class Profile has_many :authentications
In your console:
rails g model authentication uid:string provider:string user_id:integer
class Authentication < ActiveRecord::Base attr_accessible :create, :destroy, :index, :provider, :uid, :user_id belongs_to :user end
class CreateAuthentications < ActiveRecord::Migration def change create_table :authentications do |t| t.integer :user_id t.string :provider t.string :uid # the unique userid of the User supplied by the provider t.string :index t.string :create t.string :destroy t. end end end
Facebook Route setup
In your `routes.rb file
match 'facebook/registration' => 'registrations#create', :as => :new_user_registration # See http://railscasts.com/episodes/235-omniauth-part-1 match 'auth/:provider/callback' => 'authentications#create'
Contributing to facebook-rails-starterkit
- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
- Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
- Fork the project.
- Start a feature/bugfix branch.
- Commit and push until you are happy with your contribution.
- Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
- Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright (c) 2012 Kristian Mandrup. See LICENSE.txt for further details.