Class: GithubApi::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/github-api/repo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, data) ⇒ Repo

Returns a new instance of Repo.



6
7
8
9
10
11
12
13
14
# File 'lib/github-api/repo.rb', line 6

def initialize(user, data)
  @user = user
  @data = data
  @name = data["name"]
  @trees = []
  @commits = []
  @refs = []
  @blobs = []
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/github-api/repo.rb', line 4

def data
  @data
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/github-api/repo.rb', line 4

def name
  @name
end

Instance Method Details

#blob(sha) ⇒ Object

Blobs




108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/github-api/repo.rb', line 108

def blob(sha)
  # TODO: it may be smart to just load all blobs at one time
  blo = @blobs.find { |b| b.data["sha"] == sha }
  if blo.nil?
    response = GithubApi::HTTP.get("/repos/#{@user.data['login']}/#{@name}/git/blobs/" + sha,
      :headers => {
        'Content-Type' => 'application/vnd.github.beta.raw' 
      }
    ).parsed_response
    blo = GithubApi::Blob.new(response)
    @blobs << blo
  end
  blo
end

#commit(sha) ⇒ Object

Commits




50
51
52
53
54
55
56
57
58
59
# File 'lib/github-api/repo.rb', line 50

def commit(sha)
  # TODO: it may be smart to just load all commits at one time
  com = @commits.find { |c| c.data["sha"] == sha }
  if com.nil?
    response = GithubApi::HTTP.get("/repos/#{@user.data['login']}/#{@name}/git/commits/" + sha).parsed_response
    com = GithubApi::Commit.new(self, response)
    @commits << com
  end
  com
end

#create_initial_commit(sha, message) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/github-api/repo.rb', line 61

def create_initial_commit(sha, message)
  response = GithubApi::HTTP.post("/repos/#{@user.data['login']}/#{@name}/git/commits", 
    :query => {
      :access_token => @user.token
    },
    :body => {
      :message => message,
      :tree => sha
    }.to_json
  ).parsed_response
  
  @commits << GithubApi::Commit.new(self, response)
  @commits.last
end

#create_ref(name, sha) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/github-api/repo.rb', line 90

def create_ref(name, sha)
  response = GithubApi::HTTP.post("/repos/#{@user.data['login']}/#{@name}/git/refs", 
    :query => {
      :access_token => @user.token
    },
    :body => {
      :ref => name,
      :sha => sha
    }.to_json
  ).parsed_response
  
  @refs << GithubApi::Reference.new(self, response)
  @refs.last
end

#create_tree(blobs) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/github-api/repo.rb', line 31

def create_tree(blobs)
  # TODO: Check if blobs have path names in them. It is required
  # Throw error if not
  response = GithubApi::HTTP.post("/repos/#{@user.data['login']}/#{@name}/git/trees", 
    :query => {
      :access_token => @user.token
    },
    :body => {
      :tree => blobs.map { |blob| blob.to_hash }
    }.to_json
  ).parsed_response
  
  @trees << Tree.new(self, response)
  @trees.last
end

#ref(name) ⇒ Object

References




79
80
81
82
83
84
85
86
87
88
# File 'lib/github-api/repo.rb', line 79

def ref(name)
  # TODO: it may be smart to just load all refs at one time
  reference = @refs.find { |r| r.data["ref"] == name }
  if reference.nil?
    response = GithubApi::HTTP.get("/repos/#{@user.data['login']}/#{@name}/git/refs/" + name).parsed_response
    reference = GithubApi::Reference.new(self, response)
    @refs << reference
  end
  reference
end

#tree(sha) ⇒ Object

Trees




19
20
21
22
23
24
25
26
27
28
29
# File 'lib/github-api/repo.rb', line 19

def tree(sha)
  puts "SHA: #{sha}"
  # TODO: it may be smart to just load all trees at one time
  tre = @trees.find { |t| t.data["sha"] == sha }
  if tre.nil?
    response = GithubApi::HTTP.get("/repos/#{@user.data['login']}/#{@name}/git/trees/" + sha).parsed_response
    tre = GithubApi::Tree.new(self, response)
    @trees << tre
  end
  tre
end