Class: Gitable::URI
- Inherits:
-
Addressable::URI
- Object
- Addressable::URI
- Gitable::URI
- Defined in:
- lib/gitable/uri.rb
Direct Known Subclasses
Class Method Summary collapse
-
.heuristic_parse(uri) ⇒ Gitable::URI?
Attempts to make a copied URL bar into a git repository URI.
-
.parse(uri) ⇒ Gitable::URI?
Parse a git repository URI into a URI object.
Instance Method Summary collapse
-
#authenticated? ⇒ Boolean
Detect URIs that will require some sort of authentication.
-
#basename ⇒ Object
Addressable does basename wrong when there’s no basename.
-
#basename=(new_basename) ⇒ String
Set the basename, replacing it if it exists.
-
#extname=(new_ext) ⇒ String
Set an extension name, replacing one if it exists.
-
#github? ⇒ Boolean
Is this uri a github uri?.
-
#inferred_scheme ⇒ Boolean
Scheme inferred by the URI (URIs without hosts or schemes are assumed to be ‘file’).
-
#interactive_authenticated? ⇒ Boolean
Detect URIs that will require interactive authentication.
-
#local? ⇒ Boolean
Detect local filesystem URIs.
-
#project_name ⇒ String
Tries to guess the project name of the repository.
-
#set_git_extname ⇒ String
Set the ‘.git’ extension name, replacing one if it exists.
-
#ssh? ⇒ Boolean
Detect URIs that connect over ssh.
-
#to_web_uri(uri_scheme = 'https') ⇒ Addressable::URI
Create a web uri for repositories that follow the github pattern.
Class Method Details
.heuristic_parse(uri) ⇒ Gitable::URI?
Attempts to make a copied URL bar into a git repository URI.
First line of defense is for URIs without .git as a basename:
-
Change the scheme from http:// to git://
-
Add .git to the basename
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/gitable/uri.rb', line 43 def self.heuristic_parse(uri) return uri if uri.nil? || uri.kind_of?(self) # Addressable::URI.heuristic_parse _does_ return the correct type :) gitable = super # boo inconsistency if gitable.github? gitable.extname = "git" end gitable end |
.parse(uri) ⇒ Gitable::URI?
Parse a git repository URI into a URI object.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/gitable/uri.rb', line 16 def self.parse(uri) return uri if uri.nil? || uri.kind_of?(self) # addressable::URI.parse always returns an instance of Addressable::URI. add = super # >:( at inconsistency if Gitable::ScpURI.scp?(uri) Gitable::ScpURI.parse(uri) else new(add.omit(:password,:query,:fragment).to_hash) end end |
Instance Method Details
#authenticated? ⇒ Boolean
Detect URIs that will require some sort of authentication
108 109 110 |
# File 'lib/gitable/uri.rb', line 108 def authenticated? ssh? || (!normalized_user.nil? && normalized_password.nil?) end |
#basename ⇒ Object
Addressable does basename wrong when there’s no basename. It returns “/” for something like “host.com/”
146 147 148 |
# File 'lib/gitable/uri.rb', line 146 def basename super == "/" ? "" : super end |
#basename=(new_basename) ⇒ String
Set the basename, replacing it if it exists.
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/gitable/uri.rb', line 154 def basename=(new_basename) base = basename if base.to_s.empty? self.path += new_basename else rpath = normalized_path.reverse # replace the last occurrence of the basename with basename.ext self.path = rpath.sub(%r|#{Regexp.escape(base.reverse)}|, new_basename.reverse).reverse end basename end |
#extname=(new_ext) ⇒ String
Set an extension name, replacing one if it exists.
If there is no basename (i.e. no words in the path) this method call will be ignored because it is likely to break the uri.
Use the public method #set_git_extname unless you actually need some other ext
128 129 130 131 132 |
# File 'lib/gitable/uri.rb', line 128 def extname=(new_ext) return nil if basename.to_s.empty? self.basename = "#{basename.sub(%r#\.git/?$#, '')}.#{new_ext.sub(/^\.+/,'')}" extname end |
#github? ⇒ Boolean
Is this uri a github uri?
58 59 60 |
# File 'lib/gitable/uri.rb', line 58 def github? !!normalized_host.to_s.match(/\.?github.com$/) end |
#inferred_scheme ⇒ Boolean
Scheme inferred by the URI (URIs without hosts or schemes are assumed to be ‘file’)
90 91 92 93 94 95 96 |
# File 'lib/gitable/uri.rb', line 90 def inferred_scheme if normalized_scheme == 'file' || (normalized_scheme.to_s.empty? && normalized_host.to_s.empty?) 'file' else normalized_scheme end end |
#interactive_authenticated? ⇒ Boolean
Detect URIs that will require interactive authentication
115 116 117 |
# File 'lib/gitable/uri.rb', line 115 def interactive_authenticated? authenticated? && !ssh? end |
#local? ⇒ Boolean
Detect local filesystem URIs.
83 84 85 |
# File 'lib/gitable/uri.rb', line 83 def local? inferred_scheme == 'file' end |
#project_name ⇒ String
Tries to guess the project name of the repository.
76 77 78 |
# File 'lib/gitable/uri.rb', line 76 def project_name basename.sub(/\.git$/,'') end |
#set_git_extname ⇒ String
Set the ‘.git’ extension name, replacing one if it exists.
If there is no basename (i.e. no words in the path) this method call will be ignored because it is likely to break the uri.
140 141 142 |
# File 'lib/gitable/uri.rb', line 140 def set_git_extname self.extname = "git" end |
#ssh? ⇒ Boolean
Detect URIs that connect over ssh
101 102 103 |
# File 'lib/gitable/uri.rb', line 101 def ssh? !!normalized_scheme.to_s.match(/ssh/) end |
#to_web_uri(uri_scheme = 'https') ⇒ Addressable::URI
Create a web uri for repositories that follow the github pattern. This probably won’t work for all git hosts, so it’s a good idea to use this in conjunction with #github? to help ensure correct links.
68 69 70 71 |
# File 'lib/gitable/uri.rb', line 68 def to_web_uri(uri_scheme='https') return nil if normalized_host.to_s.empty? Addressable::URI.new(:scheme => uri_scheme, :host => normalized_host, :port => normalized_port, :path => normalized_path.sub(%r#\.git/?#, '')) end |