Class: GitHubApi
- Inherits:
-
Object
show all
- Includes:
- HTTParty
- Defined in:
- lib/git_hub_api.rb
Defined Under Namespace
Classes: Error, Repository
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(user, api_key) ⇒ GitHubApi
Returns a new instance of GitHubApi.
15
16
17
18
|
# File 'lib/git_hub_api.rb', line 15
def initialize(user, api_key)
@user = user
@api_key = api_key
end
|
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
13
14
15
|
# File 'lib/git_hub_api.rb', line 13
def api_key
@api_key
end
|
#user ⇒ Object
Returns the value of attribute user.
13
14
15
|
# File 'lib/git_hub_api.rb', line 13
def user
@user
end
|
Class Method Details
.default ⇒ Object
197
198
199
200
201
202
|
# File 'lib/git_hub_api.rb', line 197
def self.default
return @@default if defined?(@@default) && @@default.present?
user = `git config --global github.user`.strip
token = `git config --global github.token`.strip
@@default = self.new(user, token)
end
|
Instance Method Details
#check_results!(res) ⇒ Object
185
186
187
188
189
190
|
# File 'lib/git_hub_api.rb', line 185
def check_results!(res)
if res.is_a?(Hash) && res["error"].present?
error_msg = res["error"].to_a.map { |h| h["error"] }.join(", ")
raise Error, error_msg
end
end
|
#create_repository(details = {}) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/git_hub_api.rb', line 22
def create_repository(details = {})
name = details[:name]
description = details[:description]
homepage = details[:homepage]
is_public = (!!details.fetch(:public, false) ? 1 : 0)
if [name, description, homepage].any? { |v| v.blank? }
raise ArgumentError, "You must provide atleast a name, description and a homepage"
end
results = post('repos/create', {
:name => name,
:description => description,
:homepage => homepage,
:public => is_public
})
hash_get(results, "repository")
end
|
#delete(path, opts = {}) ⇒ Object
162
163
164
|
# File 'lib/git_hub_api.rb', line 162
def delete(path, opts = {})
self.class.delete(full_path_for(path), :body => with_auth(opts))
end
|
#full_path_for(path, version = 2, format = 'yaml') ⇒ Object
166
167
168
169
|
# File 'lib/git_hub_api.rb', line 166
def full_path_for(path, version = 2, format = 'yaml')
return path if path =~ /^https?\:\/\//i
File.join("/api/v#{version}/#{format}", path)
end
|
#get(path, opts = {}) ⇒ Object
150
151
152
|
# File 'lib/git_hub_api.rb', line 150
def get(path, opts = {})
self.class.get(full_path_for(path), :query => with_auth(opts))
end
|
#hash_get(h, k) ⇒ Object
192
193
194
195
|
# File 'lib/git_hub_api.rb', line 192
def hash_get(h, k)
check_results!(h)
h.is_a?(Hash) && h[k]
end
|
#post(path, opts = {}) ⇒ Object
154
155
156
|
# File 'lib/git_hub_api.rb', line 154
def post(path, opts = {})
self.class.post(full_path_for(path), :body => with_auth(opts))
end
|
#put(path, opts = {}) ⇒ Object
158
159
160
|
# File 'lib/git_hub_api.rb', line 158
def put(path, opts = {})
self.class.put(full_path_for(path), :body => with_auth(opts))
end
|
#repositories(user = @user) ⇒ Object
45
46
47
48
|
# File 'lib/git_hub_api.rb', line 45
def repositories(user = @user)
res = hash_get(get("repos/show/#{user}"), "repositories")
res.respond_to?(:map) ? res.map { |h| Repository.new(h, self) } : []
end
|
#repository(name, user = @user) ⇒ Object
39
40
41
42
43
|
# File 'lib/git_hub_api.rb', line 39
def repository(name, user = @user)
results = get("repos/show/#{user}/#{name}")
repo = hash_get(results, "repository")
repo ? Repository.new(repo, self) : nil
end
|
#with_auth(opts) ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/git_hub_api.rb', line 171
def with_auth(opts)
auth = {
:login => @user,
:token => @api_key
}
if opts.is_a?(Hash)
opts.merge(auth)
else
params = opts.to_s.strip
params << "&" if params != ""
params << auth.to_params
end
end
|