Backing up your DB on a Capistrano deployment

Yeah, it’s simple but useful. Since I’m using sqlite at the moment, it’s just a copy but you could run any command in a similar way. I’m also tagging the backup name with a date. Here’s a snip from my Capistrano deploy file….

# ...snip...
namespace :deploy do
  task :restart do
    t=Time.now
    run "cp /path/to/db.sqlite3 /path/to/backup/#{t.year}-#{t.month}-#{t.day}_#{t.hour}:#{t.min}:#{t.sec}_db.sqlite3"
    run "touch #{current_path}/tmp/restart.txt" 
  end
end
Posted: May 20th, 2010 | Author: | Filed under: Code | Tags: , , , , , | No Comments »

Facebook OAuth with Sinatra

This MOSTLY is working, so, here’s what I found when trying to connect to Facebook with OAuth. It needs some serious refactoring because a lot of this was done in an “investigative” trial and error way. It also looks like Facebook have improved their docs on this since I was first looking at them…

First, you need to initiate a connection:

    get '/:blog_name/facebook/oauth/create' do
      redirect "https://graph.facebook.com/oauth/authorize?client_id=##FACEBOOK API KEY##&redirect_uri=##CALLBACK URL##&scope=publish_stream,user_status,user_photos,user_about_me"
    end

where ##FACEBOOK API KEY## = your applications API key (mine is a 32 digit hex value) and ##CALLBACK URL## = the URL that will be processing the next step. Also, the scope value let’s you get more access to the user’s account. Check their extended permissions doc for more info.

Next, you need to process what comes back from Facebook. I am stashing what comes back in the DB (the FacebookOauthToken model). Also using the Mechanize gem which is pretty silly. Some of the code below is specific to my app, so ignore those bits…

    get '/:blog_name/facebook/oauth/callback' do
      if !params['code'].blank?
        url="https://graph.facebook.com/oauth/access_token"
        client_id="client_id=##CLIENT ID##"
        client_secret="client_secret=## CLIENT SECRET ##"
        code="code=#{URI.escape(params['code'])}"
        redirect_uri="redirect_uri=http://example.org/#{@blog.url_name}/facebook/oauth/callback"
 
        url="#{url}?#{client_id}&#{client_secret}&#{code}&#{redirect_uri}"
        begin
          res=open(url)
        rescue
          flash[:error]="There was a problem connecting to Facebook"
          redirect "/#{@blog.url_name}/"
        end
        read=res.read
        access_token=CGI.parse(read)['access_token']
        if !access_token.blank?
          @blog.facebook_oauth_token = FacebookOauthToken.new(:access_token=>access_token,:blog_name=>params[:blog_name])
          if @blog.save
            begin
              a=Mechanize.new
              res=a.get("https://graph.facebook.com/me/?access_token=#{access_token}")
              @blog.facebook_oauth_token.facebook_id=JSON(res.body)['id']
              @blog.save!
            rescue
              flash[:error]="There was a problem getting information from Facebook"
              redirect "/#{@blog.url_name}/"
            end
            flash[:notice]="Facebook connection created!"
            redirect "/#{@blog.url_name}/"
          else
            flash[:error]="There was a problem saving Facebook connection"
            redirect "/#{@blog.url_name}/"
          end
        else
          flash[:error]="There was a problem making Facebook connection"
          redirect "/#{@blog.url_name}/"
        end
      else
        flash[:error]="There was a problem connecting to Facebook"
        redirect "/#{@blog.url_name}/"
      end
    end

Where ##CLIENT ID## = your Application ID (mine is a 12 digit numeric value) and ## CLIENT SECRET ## = your Application Secret (mine is a 32 digit hex value).

Once you have an access_token you should be able to make calls to URLs like this:
"https://graph.facebook.com/me/?access_token=#{access_token}"
. The /me path is the connected user’s data and I’m grabbing their Facebook ID from the returned JSON for use elsewhere.

Posted: May 20th, 2010 | Author: | Filed under: Code | Tags: , , , , , , , | 1 Comment »

Been neglecting this blog ’cause I’ve been busy.

Going to start reviewing what went down with my little app though.

Posted: May 20th, 2010 | Author: | Filed under: Uncategorized | No Comments »