Permalink
Browse files

Merge pull request #2 from pjhyett/master

Updated to v3 API
  • Loading branch information...
2 parents 6539862 + 7091b3d commit ddb5fc4b7dbe1beb26231aab945358958120f4b7 @atmos committed Jun 22, 2011
Showing with 18 additions and 14 deletions.
  1. +14 −10 lib/sinatra/auth/github.rb
  2. +4 −4 spec/app.rb
View
24 lib/sinatra/auth/github.rb
@@ -36,20 +36,24 @@ def github_user
end
# API Requests
- def github_request(path)
- response = RestClient.get "https://github.com/api/v2/json/#{path}", :params => { :access_token => github_user.token }, :accept => :json
- JSON.parse(response.body)
+ def github_request(path, parse_response = true)
+ response = RestClient.get("https://api.github.com/#{path}", :params => { :access_token => github_user.token }, :accept => :json)
+ if parse_response
+ JSON.parse(response.body)
+ else
+ response
+ end
end
# Access Inquiries
def github_organization_access?(name)
- orgs = github_request("user/show/#{github_user.login}/organizations")["organizations"]
- orgs.map { |org| org["login"] }.include?(name)
+ response = github_request("orgs/#{name}/members/#{github_user.login}", false)
+ response.to_i == 204
end
- def github_organization_team_access?(name, team)
- members = github_request("teams/#{team}/members")["users"]
- members.map { |user| user["login"] }.include?(github_user.login)
+ def github_organization_team_access?(team)
+ response = github_request("teams/#{team}/members/#{github_user.login}", false)
+ response.to_i == 204
rescue RestClient::Unauthorized => e
false
end
@@ -60,9 +64,9 @@ def github_organization_authenticate!(name)
halt([401, "Unauthorized User"]) unless github_organization_access?(name)
end
- def github_organization_team_authenticate!(name, team)
+ def github_organization_team_authenticate!(team)
authenticate!
- halt([401, "Unauthorized User"]) unless github_organization_team_access?(name, team)
+ halt([401, "Unauthorized User"]) unless github_organization_team_access?(team)
end
def _relative_url_for(path)
View
8 spec/app.rb
@@ -14,7 +14,7 @@ class App < Sinatra::Base
helpers do
def repos
- github_request("repos/show/#{github_user.login}")
+ github_request("user/repos")
end
end
@@ -28,9 +28,9 @@ def repos
"Hello There, #{github_user.name}! You have access to the #{params['id']} organization."
end
- get '/orgs/:org_id/team/:id' do
- github_organization_team_authenticate!(params['org_id'], params['id'])
- "Hello There, #{github_user.name}! You have access to the #{params['id']} team under the #{params['org_id']} organization."
+ get '/teams/:id' do
+ github_organization_team_authenticate!(params['id'])
+ "Hello There, #{github_user.name}! You have access to the #{params['id']} team."
end
get '/logout' do

0 comments on commit ddb5fc4

Please sign in to comment.