Skip to content

Instantly share code, notes, and snippets.

View Gregg's full-sized avatar
🙄
Contemplating why Github needs a status bar.

Gregg Pollack Gregg

🙄
Contemplating why Github needs a status bar.
View GitHub Profile
# httperf with sessions
$ httperf --wsess=10,5,2 --rate=1 --timeout=5 \
--server=localhost --port=3000 --uri=/posts
# httperf with a file of uris
$ httperf --wsesslog=10,2,path.txt --rate=1 \
--timeout=5, --session-cookie
--server=localhost --port=3000 --uri=/posts

Building a Facebook application with Rails and hosting it in Heroku

Introduction

Facebook has given a big step in simplicity with its Graph API and the creation of the Open Graph protocol, now more than ever it's easier to read and write data from and to the socalled social graph.

You could turn your webpage into a fully-featured Facebook-like page, just like if you were inside Facebook, you can give your users the ability to sign in with their Facebook credentials, customize your users' experience with parameters taken from their Facebook profiles, you could add a Like button to every object in your page as images, songs, articles, etc., tell your users which friends of theirs have liked your content, and a lot of things more, oh, I forgot to mention something, all of this with total ease. Yes, I know you could do it with Facebook Connect, but I said "with ease".

Building a Facebook application and hosting it in Heroku

Introduction

With Facebook's Graph API and the creation of the Open Graph protocol, it is now easier then ever before to read and write data from and to the "social graph". Here's a few of the possibilities:

  • You could turn your webpage into a fully-featured Facebook-like page, just like if you were inside Facebook.
@Gregg
Gregg / BP-FatModelSkinnyController.rb
Created January 14, 2011 20:48
Fat Model / Skinny Controller
# A user has many tweets.. and sometimes we want to retweet.
class TweetsController < ApplicationController
def retweet
tweet = Tweet.find(params[:id])
if tweet.user == current_user
flash[:notice] = "Sorry, you can't retweet your own tweets"
elsif tweet.retweets.where(:user_id => current_user.id).present?
@Gregg
Gregg / scope_on_create.rb
Created January 14, 2011 21:07
Use scope access
# Even when you create objects they should be properly scoped... Remember this example?
t = Tweet.new
t.status = "RT #{@tweet.user.name}: #{@tweet.status}"
t.original_tweet = @tweet
t.user = current_user
t.save
# A simplified version of this, properly scoped might look like this:
# If we found this in a controller
def index
@tweets = Tweet.find(:all, :conditions => {:user_id => current_user.id}, :order => 'created_at desc', :limit => 10)
@trending = Topic.find(:all, :conditions => ["started_trending > ?", 1.day.ago], :order => 'mentions desc', :limit => 5)
...
end
# The first thing that's wrong with it, is it's not using the nice new syntax Rails 3 gives us.
# Look at the http://twitter.com/settings/account page. Here you have some user settings, and some account settings that you might (perhaps) want to keep in a separate table.
class User < ActiveRecord::Base
has_one :account_setting, :dependent => :destroy
end
# Then in the user form
# <%= form_for(@user) do |f| %>
# <div class="field">
# So we have a form that just sends an email to us
# <h1>Contact Us</h1>
# <%= form_for :contact, :url => 'send_email' do |f| %>
# <div class="field">
# <%= f.label :name %><br />
# <%= f.text_field :name %>
# </div>
# <div class="field">
# <%= f.label :email %><br />
# if you look at your logged in twitter page, there's a crapload of information. You might expect a controller like:
def index
@followers_tweets = current_user.followers_tweets.limit(20)
@recent_tweet = current_user.tweets.first
@following = current_user.following.limit(5)
@followers = current_user.followers.limit(5)
@recent_favorite = current_user.favorite_tweets.first
@recent_listed = current_user.recently_listed.limit(5)
# You know REST... yeah... This is not RESTful
class UsersController < ApplicationController
def subscribe_mailing_list
current_user.subscribe(params[:mailing_list])
redirect_to users_path, :notice => "You've been subscribed"
end
def unsubscribe_mailing_list