Class: Gitw::Git::URL

Inherits:
Object
  • Object
show all
Defined in:
lib/gitw/git/url.rb

Overview

git repository url

Constant Summary collapse

SSH_RE =
%r{
       ^
       (?:(?<user>[^@/]+)@)?
       (?<host>[^:/]+)
       :(?!/)
       (?<path>.*?)
       $
}x.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_url) ⇒ URL

Returns a new instance of URL.



20
21
22
23
24
25
26
27
28
# File 'lib/gitw/git/url.rb', line 20

def initialize(raw_url)
  @raw_url = raw_url
  @url = nil
  @user = nil
  @host = nil
  @path = nil

  parse
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



18
19
20
# File 'lib/gitw/git/url.rb', line 18

def host
  @host
end

#pathObject (readonly)

Returns the value of attribute path.



18
19
20
# File 'lib/gitw/git/url.rb', line 18

def path
  @path
end

#raw_urlObject (readonly)

Returns the value of attribute raw_url.



18
19
20
# File 'lib/gitw/git/url.rb', line 18

def raw_url
  @raw_url
end

#urlObject (readonly)

Returns the value of attribute url.



18
19
20
# File 'lib/gitw/git/url.rb', line 18

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



18
19
20
# File 'lib/gitw/git/url.rb', line 18

def user
  @user
end

Instance Method Details

#parseObject

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gitw/git/url.rb', line 31

def parse
  if (match = SSH_RE.match(raw_url))
    @user = match[:user]
    @host = match[:host]
    @path = match[:path]
    @url = raw_url.clone
  elsif (uri = URI.parse(raw_url))
    @user = uri.user
    @host = uri.host
    @path = uri.path
    @url = uri.to_s
  end
  @path = @path.chomp('.git').sub(%r{^/+}, '').sub(%r{/+$}, '')
end

#path_basenameObject



51
52
53
# File 'lib/gitw/git/url.rb', line 51

def path_basename
  File.basename(path)
end

#path_dirnameObject

rubocop:enable Metrics/AbcSize, Metrics/MethodLength



47
48
49
# File 'lib/gitw/git/url.rb', line 47

def path_dirname
  File.dirname(path)
end

#to_sObject



55
56
57
# File 'lib/gitw/git/url.rb', line 55

def to_s
  url.to_s
end