Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Updated to v3 #2

Merged
merged 1 commit into from

2 participants

@pjhyett

You might wanna double check the tests on your end, because I couldn't get them to run correctly, but these API calls seem to work fine.

edit: just noticed I did this on master, sorry for any hassle.

@atmos
Owner

Hah! I did this over the weekend and hadn't push it up yet. I'll double check that they match up and I need to add basic return_to style behavior as well. Should ship it all soon though.

@atmos atmos merged commit ddb5fc4 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 20, 2011
  1. @pjhyett

    Use GitHub API v3

    pjhyett authored
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.