Skip to content
This repository has been archived by the owner on Nov 10, 2017. It is now read-only.

Commit

Permalink
Use GitHub API v3
Browse files Browse the repository at this point in the history
  • Loading branch information
pjhyett committed Jun 20, 2011
1 parent 6539862 commit 7091b3d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
24 changes: 14 additions & 10 deletions lib/sinatra/auth/github.rb
Expand Up @@ -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
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions spec/app.rb
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 7091b3d

Please sign in to comment.