Skip to content

Instantly share code, notes, and snippets.

@pengwynn
Forked from therealadam/Gemfile
Created June 26, 2010 19:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pengwynn/454283 to your computer and use it in GitHub Desktop.
Save pengwynn/454283 to your computer and use it in GitHub Desktop.
begin
# Require preresolved locked gems
require ::File.expand_path('.bundle/environment', __FILE__)
rescue LoadError
# Fallback on resolving at runtime
require 'rubygems'
require 'bundler'
Bundler.setup
end
require 'sinatra/base'
require 'gowalla'
class App < Sinatra::Base
set :sessions, true
get '/' do
if session[:access_token]
redirect '/auth/gowalla/test'
else
"Why not authenticate with <a href=\'/auth/gowalla\'>Gowalla</a>?"
end
end
get '/auth/gowalla' do
redirect(client.web_server.
authorize_url(:redirect_uri => redirect_uri, :state => 1))
end
get '/auth/gowalla/callback' do
session[:access_token] = client.web_server.get_access_token(params[:code], :redirect_uri => redirect_uri).token
if session[:access_token]
redirect '/auth/gowalla/test'
else
"Error retrieving access token."
end
end
get '/auth/gowalla/test' do
unless client.needs_access?
u = client.user('pengwynn')
"Where's Wynn? He last checked in at #{u.last_checkins.first.spot.name}."
else
redirect '/auth/gowalla'
end
end
protected
def client
Gowalla::Client.new (
:api_key => "your_api_key",
:api_secret => "your_api_secret",
:access_token => session[:access_token]
)
end
def redirect_uri
uri = URI.parse(request.url)
uri.path = '/auth/gowalla/callback'
uri.query = nil
uri.to_s
end
end
run App
source :rubygems
gem 'sinatra', '1.0'
gem 'gowalla', '~> 0.2.0'
group :development do
gem 'shotgun'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment