Code Monkey home page Code Monkey logo

fb_rails's Introduction

FbRails

FbRails makes it easy to integrate your website with Facebook.

Setting up

Add the following to your configuration. This can go in config/application.rb, or customized in each environment in config/environments/.rb:

config.facebook = {
  :app_id     => 'my_app_id',
  :secret     => 'my_app_secret'
}

or

config.facebook.app_id = 'my_app_id'
config.facebook.secret = 'my_app_secret'

Include facebook Javascript and a login button:

<%= include_facebook_javascript %>
<%= fbml 'login-button' %>

FBRails API

FbRails adds an object called ‘fb’, which is available to all controllers and views:

Determine if the current user is connected through Facebook:

fb.connected?

Get the Facebook user id for the current user:

fb.uid

Get the Facebook user’s access token:

fb.access_token

Retrieve your Facebook application app_id or secret:

fb.app_id
fb.secret

Using the Graph API

Make a request to the Facebook Graph:

<% result = fb.graph.get 'me' %>
my name is <%= result['first_name'] %>

Post to the user’s wall:

fb.graph.post 'me/feed', :message => 'I like turtles'

Persisting the Facebook User:

Websites that interact with Facebook usually have a user model in a local database. This user is accessed with ‘fb.user’:

The current user is <%= fb.user.inspect %>

For this to work, FbRails assumes there is a model called ‘User’ with a column ‘fb_uid’. If your user model name is different than ‘User’, it can be configured:

config.facebook.user_class_name = 'Author'

If the fb_uid for the current user is not in the database, fb.user is an unsaved instance. One thing I like to do for new Facebook users is store them in a before filter:

if fb.connected? && fb.user.new_record?
  data = fb.graph.get('me')
  fb.user.first_name = data['first_name']
  fb.user.last_name = data['last_name']
  fb.user.save
end

Timeouts

Calls to the Graph API can timeout. If this occurs, an FbRails::TimeoutError occurs. Your application can rescue from these:

begin
  fb.get 'me'
rescue FbRails::TimeoutError => e
  ...
end

The timeout defaults to 60 seconds. This can be configured:

config.timeout = 10

Testing

Facebook requests can be mocked, so that your tests do not require real HTTP connections. This is done with FbRails::Mock:

FbRails::Mock.respond_to do |mock|
  mock.add 'me', 'first_name' => 'Matt', 'last_name' => 'Higgins'
end

Now, a call to fb.graph.get(‘me’) returns the above response.

fb_rails's People

Contributors

matthuhiggins avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

iamjoshbinder

fb_rails's Issues

Add graph request timeout

Make a configurable way to cancel an http request and raise a custom exception if a timeout occurs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.