Class: GitInit::GitUrl

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ GitUrl

Returns a new instance of GitUrl.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/git_init.rb', line 11

def initialize(opts={})

  # convert url if needed
  if opts.is_a?(String)
    opts = GitUrl.parse_url(opts)
  end

  # set instance vars
  opts.each do |key, value|
    self.send("#{key}=".to_sym, value)
  end
end

Instance Attribute Details

#basenameObject

Returns the value of attribute basename.



9
10
11
# File 'lib/git_init.rb', line 9

def basename
  @basename
end

#domainObject

Returns the value of attribute domain.



9
10
11
# File 'lib/git_init.rb', line 9

def domain
  @domain
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/git_init.rb', line 9

def path
  @path
end

#protoObject

Returns the value of attribute proto.



9
10
11
# File 'lib/git_init.rb', line 9

def proto
  @proto
end

#typeObject

Returns the value of attribute type.



9
10
11
# File 'lib/git_init.rb', line 9

def type
  @type
end

#userObject

Returns the value of attribute user.



9
10
11
# File 'lib/git_init.rb', line 9

def user
  @user
end

Class Method Details

.detect_type(url) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/git_init.rb', line 72

def GitUrl.detect_type(url)

  # Simple git url
  return :git if (/^git:\/\//.match(url))

  # http(s)
  return :http if (/^https?:\/\//.match(url))

  # ssh
  return :ssh if (/:/.match(url))

  raise "Unknown type of url #{url}"

end

.parse_url(url) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/git_init.rb', line 40

def GitUrl.parse_url(url)

  ret_val = {}

  # Detect type
  ret_val[:type] = detect_type(url)

  # Parse ssh url
  if ret_val[:type] == :ssh
    m = /^(([^@]+)@)?([^:]+):(.*\.git$)/.match(url)
    ret_val[:user] = m[2]
    ret_val[:domain] = m[3]
    ret_val[:path] = File.dirname(m[4])
    ret_val[:basename] = File.basename(m[4])
  elsif ret_val[:type] == :http
    m = /^(https?):\/\/([^\/]+)\/(.*\.git$)/.match(url)
    ret_val[:proto] = m[1]
    ret_val[:domain] = m[2]
    ret_val[:path] = File.dirname(m[3])
    ret_val[:basename] = File.basename(m[3])
  elsif ret_val[:type] == :git
    m = /^git:\/\/([^\/]+)\/(.*\.git$)/.match(url)
    ret_val[:domain] = m[1]
    ret_val[:path] = File.dirname(m[2])
    ret_val[:basename] = File.basename(m[2])
  end


  return ret_val

end

Instance Method Details

#urlObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/git_init.rb', line 24

def url
  if type == :http
    "#{self.proto}://#{self.domain}/#{File.join(self.path,self.basename)}"
  elsif type == :ssh
    if self.user.nil?
      user = ""
    else
      user = "#{self.user}@"
    end
    "#{user}#{self.domain}:#{File.join(self.path,self.basename)}"
  elsif type == :git
    "git://#{self.domain}/#{File.join(self.path,self.basename)}"
  end
end