Class: Twig::GithubRepo

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

Overview

Represents a Git repository that is hosted on GitHub. Usage:

Twig::GithubRepo.new do |gh_repo|
  puts gh_repo.username
  puts gh_repo.repository
end

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ GithubRepo

Returns a new instance of GithubRepo.

Yields:

  • (_self)

Yield Parameters:



13
14
15
16
17
18
19
20
21
# File 'lib/twig/github.rb', line 13

def initialize
  abort 'Current directory is not a git repository.' unless Twig.repo?

  if origin_url.empty? || !github_repo? || username.empty? || repository.empty?
    abort 'This does not appear to be a GitHub repository.'
  end

  yield(self)
end

Instance Method Details

#github_repo?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/twig/github.rb', line 31

def github_repo?
  gh_url_prefix = 'https://github.com'
  uri = URI.parse(gh_url_prefix)
  origin_url.include?(uri.host)
end

#origin_urlObject



23
24
25
# File 'lib/twig/github.rb', line 23

def origin_url
  @origin_url ||= Twig.run('git config remote.origin.url')
end

#origin_url_partsObject



27
28
29
# File 'lib/twig/github.rb', line 27

def origin_url_parts
  @origin_url_parts ||= origin_url.split(/[\/:]/)
end

#repositoryObject



41
42
43
# File 'lib/twig/github.rb', line 41

def repository
  @repo ||= origin_url_parts[-1].sub(/\.git$/, '') || ''
end

#usernameObject



37
38
39
# File 'lib/twig/github.rb', line 37

def username
  @username ||= origin_url_parts[-2] || ''
end