Skip to content

Instantly share code, notes, and snippets.

@pwnall
Created April 28, 2012 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pwnall/2519877 to your computer and use it in GitHub Desktop.
Save pwnall/2519877 to your computer and use it in GitHub Desktop.
Multiple HTTP requests in a single Rails controller action

Whenever users sign into my application, or access its homepage, I check to see if I have recent Facebook data for them. If not, I synchronously fetch the data from Facebook. I'd do it asynchronously, but I don't want to add logic for rendering the homepage with incomplete information.

The controller code is the if statement that starts here: https://github.com/pwnall/mit_hack_2012/blob/master/app/controllers/session_controller.rb#L17

Facebook data is cached in the Profile model (each user has a profile), and in the UserFriendship model.

The model code that does the fetches is here: https://github.com/pwnall/mit_hack_2012/blob/master/app/models/profile.rb#L38

The code calls some code in the fbgraph gem to fetch profile information. It also calls the method below, which uses curl to follow redirects: https://github.com/pwnall/mit_hack_2012/blob/master/app/models/profile.rb#L74

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment