Class: GitHubApi::Repository

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

Defined Under Namespace

Classes: SSHPublicKey

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, api) ⇒ Repository

Returns a new instance of Repository.



56
57
58
59
60
61
# File 'lib/git_hub_api.rb', line 56

def initialize(attributes, api)
  @name       = attributes[:name]
  @owner      = attributes[:owner]
  @api        = api
  @attributes = OpenStruct.new(attributes)
end

Instance Attribute Details

#apiObject

Returns the value of attribute api.



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

def api
  @api
end

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#ownerObject

Returns the value of attribute owner.



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

def owner
  @owner
end

Instance Method Details

#add_collaborator(name) ⇒ Object



84
85
86
87
88
# File 'lib/git_hub_api.rb', line 84

def add_collaborator(name)
  return if name.blank?
  res = api.post("repos/collaborators/#{@name}/add/#{name}")
  api.hash_get(res, "collaborators")
end

#add_hook(url) ⇒ Object



109
110
111
112
# File 'lib/git_hub_api.rb', line 109

def add_hook(url)
  self.hooks = (hooks + [url])
  hooks.include?(url)
end

#add_key(title, key) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/git_hub_api.rb', line 127

def add_key(title, key)
  result = api.get("repos/key/#{@name}/add", {
    :title => title,
    :key   => key
  })
  keys = api.hash_get(result, "public_keys")
  return false if keys.nil?
  keys.map { |k| SSHPublicKey.new(k["title"], k["id"]) }
end

#collaboratorsObject

User Manipulation



79
80
81
82
# File 'lib/git_hub_api.rb', line 79

def collaborators
  res = api.get("repos/show/#{@owner}/#{@name}/collaborators")
  api.hash_get(res, "collaborators")
end

#destroyObject



63
64
65
66
67
# File 'lib/git_hub_api.rb', line 63

def destroy
  token  = api.post("repos/delete/#{@name}")["delete_token"]
  result = api.post("repos/delete/#{@name}", :delete_token => token)
  result.is_a?(Hash) && result["status"] == "deleted"
end

#hooksObject

Hooks



97
98
99
100
# File 'lib/git_hub_api.rb', line 97

def hooks
  response = Hpricot(api.get("https://github.com/#{@owner}/#{@name}/edit/hooks").to_s)
  response.search("//input[@name=urls[]]").map { |e| e[:value] }.compact
end

#hooks=(list) ⇒ Object



102
103
104
105
106
107
# File 'lib/git_hub_api.rb', line 102

def hooks=(list)
  list         = [*list].compact.uniq
  query_string = list.map { |u| "urls[]=#{URI.escape(u)}" }.join("&")
  post_url     = "https://github.com/#{@owner}/#{@name}/edit/postreceive_urls"
  api.post(post_url, query_string)
end

#keysObject

Keys



121
122
123
124
125
# File 'lib/git_hub_api.rb', line 121

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

#private?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/git_hub_api.rb', line 69

def private?
  !!@attributes.private
end

#public?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/git_hub_api.rb', line 73

def public?
  !private?
end

#remove_collaborator(name) ⇒ Object



90
91
92
93
# File 'lib/git_hub_api.rb', line 90

def remove_collaborator(name)
  res = api.post("repos/collaborators/#{@name}/remove/#{name}")
  api.hash_get(res, "collaborators")
end

#remove_hook(url) ⇒ Object



114
115
116
117
# File 'lib/git_hub_api.rb', line 114

def remove_hook(url)
  self.hooks = (hooks - [url])
  !hooks.include?(url)
end

#remove_key(key_id) ⇒ Object



137
138
# File 'lib/git_hub_api.rb', line 137

def remove_key(key_id)
end

#tagsObject

Misc. Information



142
143
144
# File 'lib/git_hub_api.rb', line 142

def tags
  api.hash_get(api.get("repos/show/#{@owner}/#{@name}/tags"), "tags")
end