Class: GitHubApi::Repository

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, api) ⇒ Repository

Returns a new instance of Repository.



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

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.



60
61
62
# File 'lib/git_hub_api.rb', line 60

def api
  @api
end

#attributesObject

Returns the value of attribute attributes.



60
61
62
# File 'lib/git_hub_api.rb', line 60

def attributes
  @attributes
end

#nameObject

Returns the value of attribute name.



60
61
62
# File 'lib/git_hub_api.rb', line 60

def name
  @name
end

#ownerObject

Returns the value of attribute owner.



60
61
62
# File 'lib/git_hub_api.rb', line 60

def owner
  @owner
end

Instance Method Details

#add_collaborator(name) ⇒ Object



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

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



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

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

#add_key(title, key) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/git_hub_api.rb', line 133

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"], k["key"]) }
end

#collaboratorsObject

User Manipulation



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

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

#destroyObject



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

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



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

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



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

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



127
128
129
130
131
# File 'lib/git_hub_api.rb', line 127

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"], k["key"]) }
end

#private?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/git_hub_api.rb', line 75

def private?
  !!@attributes.private
end

#public?Boolean

Returns:

  • (Boolean)


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

def public?
  !private?
end

#remove_collaborator(name) ⇒ Object



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

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

#remove_hook(url) ⇒ Object



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

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

#remove_key(key_id) ⇒ Object



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

def remove_key(key_id)
end

#tagsObject

Misc. Information



148
149
150
# File 'lib/git_hub_api.rb', line 148

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