Class: Gitrob::Github::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/gitrob/github/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, name, http_client) ⇒ Repository

Returns a new instance of Repository.



6
7
8
# File 'lib/gitrob/github/repository.rb', line 6

def initialize(owner, name, http_client)
  @owner, @name, @http_client = owner, name, http_client
end

Instance Attribute Details

#http_clientObject (readonly)

Returns the value of attribute http_client.



5
6
7
# File 'lib/gitrob/github/repository.rb', line 5

def http_client
  @http_client
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/gitrob/github/repository.rb', line 5

def name
  @name
end

#ownerObject (readonly)

Returns the value of attribute owner.



5
6
7
# File 'lib/gitrob/github/repository.rb', line 5

def owner
  @owner
end

Instance Method Details

#contentsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gitrob/github/repository.rb', line 10

def contents
  if !@contents
    @contents = []
    response = JSON.parse(http_client.do_get("/repos/#{owner}/#{name}/git/trees/master?recursive=1").body)
    response['tree'].each do |object|
      next unless object['type'] == 'blob'
      @contents << Blob.new(object['path'], object['size'], self)
    end
  end
  @contents
rescue HttpClient::ClientError => ex
  if ex.status == 409 || ex.status == 404
    @contents = []
  else
    raise ex
  end
end

#descriptionObject



36
37
38
# File 'lib/gitrob/github/repository.rb', line 36

def description
  info['description']
end

#full_nameObject



28
29
30
# File 'lib/gitrob/github/repository.rb', line 28

def full_name
  [owner, name].join('/')
end

#save_to_database!(organization, user = nil) ⇒ Object



56
57
58
59
60
# File 'lib/gitrob/github/repository.rb', line 56

def save_to_database!(organization, user = nil)
  self.to_model(organization, user).tap { |m| m.save }
rescue DataMapper::SaveFailureError => e
  puts e.resource.errors.inspect
end

#to_model(organization, user = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gitrob/github/repository.rb', line 44

def to_model(organization, user = nil)
  Gitrob::Repo.new(
    :name         => self.name,
    :owner_name   => self.owner,
    :description  => self.description,
    :website      => self.website,
    :url          => self.url,
    :organization => organization,
    :user         => user
  )
end

#urlObject



32
33
34
# File 'lib/gitrob/github/repository.rb', line 32

def url
  info['html_url']
end

#websiteObject



40
41
42
# File 'lib/gitrob/github/repository.rb', line 40

def website
  info['homepage']
end