Class: GitHubApi

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/git_hub_api.rb

Defined Under Namespace

Classes: Error, Repository, SSHPublicKey

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.



17
18
19
20
# File 'lib/git_hub_api.rb', line 17

def initialize(user, api_key)
  @user = user
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



15
16
17
# File 'lib/git_hub_api.rb', line 15

def api_key
  @api_key
end

#userObject

Returns the value of attribute user.



15
16
17
# File 'lib/git_hub_api.rb', line 15

def user
  @user
end

Class Method Details

.defaultObject



203
204
205
206
207
208
# File 'lib/git_hub_api.rb', line 203

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



191
192
193
194
195
196
# File 'lib/git_hub_api.rb', line 191

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

Repository Manipulation



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/git_hub_api.rb', line 24

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



168
169
170
# File 'lib/git_hub_api.rb', line 168

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



172
173
174
175
# File 'lib/git_hub_api.rb', line 172

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

Helper Methods



156
157
158
# File 'lib/git_hub_api.rb', line 156

def get(path, opts = {})
  self.class.get(full_path_for(path), :query => with_auth(opts))
end

#hash_get(h, k) ⇒ Object



198
199
200
201
# File 'lib/git_hub_api.rb', line 198

def hash_get(h, k)
  check_results!(h)
  h.is_a?(Hash) && h[k]
end

#keysObject



41
42
43
44
45
# File 'lib/git_hub_api.rb', line 41

def keys
  keys = hash_get(get("/user/keys"), "public_keys")
  return false if keys.nil?
  keys.map { |k| SSHPublicKey.new(k["title"], k["id"], k["key"]) }
end

#post(path, opts = {}) ⇒ Object



160
161
162
# File 'lib/git_hub_api.rb', line 160

def post(path, opts = {})
  self.class.post(full_path_for(path), :body => with_auth(opts))
end

#put(path, opts = {}) ⇒ Object



164
165
166
# File 'lib/git_hub_api.rb', line 164

def put(path, opts = {})
  self.class.put(full_path_for(path), :body => with_auth(opts))
end

#repositories(user = @user) ⇒ Object



53
54
55
56
# File 'lib/git_hub_api.rb', line 53

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



47
48
49
50
51
# File 'lib/git_hub_api.rb', line 47

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



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/git_hub_api.rb', line 177

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