This repository has been archived by the owner on Oct 16, 2019. It is now read-only.
/
user.rb
162 lines (146 loc) · 5.16 KB
/
user.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
require 'yajl'
require 'octokit'
require 'rest-client'
module Warden
module Github
module Oauth
class User < Struct.new(:attribs, :token)
def login
attribs['login']
end
def name
attribs['name']
end
def gravatar_id
attribs['gravatar_id']
end
def email
attribs['email']
end
def company
attribs['company']
end
# See if the user is a public member of the named organization
#
# name - the organization name
#
# Returns: true if the user is publicized as an org member
def publicized_organization_member?(org_name)
members = github_request("orgs/#{org_name}/public_members")
members.map { |org| org["login"] }.include?(login)
rescue RestClient::Forbidden, RestClient::Unauthorized, RestClient::ResourceNotFound => e
false
end
# See if the user is a member of the named organization
#
# name - the organization name
#
# Returns: true if the user has access, false otherwise
def organization_member?(org_name)
orgs = github_request("orgs/#{org_name}/members")
orgs.map { |org| org["login"] }.include?(login)
rescue RestClient::Forbidden, RestClient::Unauthorized, RestClient::ResourceNotFound => e
false
end
# See if the user is a member of the team id
#
# team_id - the team's id
#
# Returns: true if the user has access, false otherwise
def team_member?(team_id)
members = github_request("teams/#{team_id}/members")
members.map { |user| user["login"] }.include?(login)
rescue RestClient::Forbidden, RestClient::Unauthorized, RestClient::ResourceNotFound => e
false
end
# Send a V3 API PUT request to path and parses the response body
#
# path - the path on api.github.com to hit
# params - extra params for calling the api
#
def get(path, params)
github_request(path, params)
end
# Send a V3 API PUT request to path and parses the response body
#
# path - the path on api.github.com to hit
# params - extra params for calling the api
#
def post(path, params)
headers = {:Authorization => "token #{token}", :content_type => :json, :accept => :json}
res = RestClient.post("#{github_api_uri}/#{path}", params.to_json, headers)
Yajl.load(res)
end
# Send a V3 API PUT request to path and parses the response body
#
# path - the path on api.github.com to hit
# params - extra params for calling the api
#
def put(path, params)
headers = {:Authorization => "token #{token}", :content_type => :json, :accept => :json}
res = RestClient.put("#{github_api_uri}/#{path}", params.to_json, headers)
Yajl.load(res)
end
# Send a V3 API DELETE request to path and parses the response body
#
# path - the path on api.github.com to hit
# params - extra params for calling the api
#
def delete(path, params)
headers = {:Authorization => "token #{token}", :content_type => :json, :accept => :json}
res = RestClient.delete("#{github_api_uri}/#{path}", params.to_json, headers)
Yajl.load(res)
end
# Access the GitHub API from Octokit
#
# Octokit is a robust client library for the GitHub API
# https://github.com/pengwynn/octokit
#
# Returns a cached client object for easy use
def api
@api ||= Octokit::Client.new(:login => login, :oauth_token => token)
end
# Send a V3 API GET request to path and parse the response body
#
# path - the path on api.github.com to hit
# params - extra params for calling the api
#
# Returns a parsed JSON response
#
# Examples
# github_request("/user")
# # => { 'login' => 'atmos', ... }
#
# github_request("/user/repos", {:page => 2})
# # => [ { 'name' => 'gollum' ... } ]
def github_request(path, params = {})
Yajl.load(github_raw_request(path, params))
end
# Send a V3 API GET request to path
#
# path - the path on api.github.com to hit
#
# Returns a rest client response object
#
# Examples
# github_raw_request("/user")
# # => RestClient::Response
#
# github_raw_request("/user/repos", {:page => 3})
# # => RestClient::Response
def github_raw_request(path, params = {})
headers = {:Authorization => "token #{token}", :accept => :json}
RestClient.get("#{github_api_uri}/#{path}", headers.merge(:params => params))
end
private
def github_api_uri
if ENV['GITHUB_OAUTH_API_DOMAIN']
ENV['GITHUB_OAUTH_API_DOMAIN']
else
"https://api.github.com"
end
end
end
end
end
end