Skip to content
Browse files

document how to make API requests

* added github_request(path)
  • Loading branch information...
1 parent 99c09e1 commit 3d0304b65b8586888e9016773e9362b2d277447a @atmos committed Jun 3, 2010
Showing with 17 additions and 10 deletions.
  1. +3 −1 Gemfile
  2. +4 −3 lib/sinatra/auth/github.rb
  3. +10 −6 spec/app.rb
View
4 Gemfile
@@ -2,7 +2,7 @@ source :gemcutter
group :runtime do
gem 'sinatra', '~>1.0'
- gem 'warden-github', '~>0.0.4'
+ gem 'warden-github', '~>0.0.5'
end
group :test do
@@ -14,6 +14,8 @@ group :test do
gem 'fakeweb', '>=1.2.8'
gem 'randexp', '>=0.1.4'
gem 'rack-test', '~>0.5.3', :require => 'rack/test'
+ gem 'ruby-debug'
+ gem 'rest-client', '~>1.5.1', :require => 'rest_client'
end
# vim:ft=ruby
View
7 lib/sinatra/auth/github.rb
@@ -32,8 +32,9 @@ def github_user
warden.user
end
- def relative_url_for(path)
- request.script_name + path
+ def github_request(path)
+ response = RestClient.get("https://github.com/api/v2/json/#{path}", {:accept => :json, :params => {:token => github_user.token}})
+ JSON.parse(response.body)
end
end
@@ -52,7 +53,7 @@ def self.registered(app)
app.get '/auth/github/callback' do
authenticate!
- redirect relative_url_for('/')
+ redirect url_for '/'
end
end
end
View
16 spec/app.rb
@@ -1,22 +1,26 @@
+require 'pp'
+require 'rest_client'
+
module Example
class App < Sinatra::Base
enable :sessions
- set :github_options, {:client_id => ENV['GH_CLIENT_ID'],
- :secret => ENV['GH_SECRET'] }
+ set :github_options, {:client_id => ENV['GH_CLIENT_ID'], :secret => ENV['GH_SECRET'] }
register Sinatra::Auth::Github
before do
authenticate!
end
- get '/' do
- "Hello There, #{github_user.name}!"
+ helpers do
+ def repos
+ github_request("repos/show/#{github_user.attribs['login']}")
+ end
end
- get '/another_route' do
- "Hello There, #{github_user.name}!"
+ get '/' do
+ "Hello There, #{github_user.name}!\n#{repos.inspect}"
end
get '/logout' do

0 comments on commit 3d0304b

Please sign in to comment.
Something went wrong with that request. Please try again.