From dfc77a87fa52d20470cf92499d59025bffc43e7a Mon Sep 17 00:00:00 2001 From: Martin Emde Date: Thu, 14 Dec 2023 16:58:28 -0800 Subject: [PATCH] Modernize (#6) --- lib/gitable/scp_uri.rb | 21 ++++++------------ lib/gitable/uri.rb | 48 ++++++++++++++++++++++++++---------------- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/lib/gitable/scp_uri.rb b/lib/gitable/scp_uri.rb index 08ad654..8b71136 100644 --- a/lib/gitable/scp_uri.rb +++ b/lib/gitable/scp_uri.rb @@ -31,8 +31,8 @@ def self.parse(uri) # @return [String] The same path passed in. def path=(new_path) super - if new_path[0..0] != '/' # addressable adds a / but scp-style uris are altered by this behavior - @path = path.sub(%r|^/+|,'') + if new_path[0] != '/' # addressable adds a / but scp-style uris are altered by this behavior + @path = path.delete_prefix("/") @normalized_path = nil validate end @@ -45,14 +45,7 @@ def path=(new_path) # # @return [String] The URI as a string. def to_s - @uri_string ||= - begin - uri_string = "#{normalized_authority}:#{normalized_path}" - if uri_string.respond_to?(:force_encoding) - uri_string.force_encoding(Encoding::UTF_8) - end - uri_string - end + @uri_string ||= "#{normalized_authority}:#{normalized_path}".force_encoding(Encoding::UTF_8) end alias to_str to_s @@ -82,19 +75,19 @@ def scp? def validate return if @validation_deferred - if host.to_s.empty? + if host.nil? || host.empty? invalid! "Hostname segment missing" end - if !scheme.to_s.empty? + if scheme && !scheme.empty? invalid! "Scp style URI must not have a scheme" end - if !port.to_s.empty? + if port invalid! "Scp style URI cannot have a port" end - if path.to_s.empty? + if path.nil? || path.empty? invalid! "Absolute URI missing hierarchical segment" end diff --git a/lib/gitable/uri.rb b/lib/gitable/uri.rb index 16cc21a..9f25595 100644 --- a/lib/gitable/uri.rb +++ b/lib/gitable/uri.rb @@ -16,8 +16,8 @@ class URI < Addressable::URI # @raise [Gitable::URI::InvalidURIError] When the uri is *total* rubbish. # def self.parse(uri) - return nil if uri.nil? - return uri.dup if uri.kind_of?(self) + return unless uri + return uri.dup if uri.is_a?(self) # Copied from Addressable to speed up our parsing. # @@ -34,7 +34,7 @@ def self.parse(uri) uri = uri.to_str rescue TypeError, NoMethodError raise TypeError, "Can't convert #{uri.class} into String." - end if not uri.is_a? String + end # This Regexp supplied as an example in RFC 3986, and it works great. fragments = uri.scan(URIREGEX)[0] @@ -52,14 +52,14 @@ def self.parse(uri) end if host.nil? && uri =~ SCP_REGEXP - Gitable::ScpURI.new(:authority => $1, :path => $2) + Gitable::ScpURI.new(authority: $1, path: $2) else new( - :scheme => scheme, - :authority => authority, - :path => path, - :query => query, - :fragment => fragment + scheme: scheme, + authority: authority, + path: path, + query: query, + fragment: fragment ) end end @@ -92,7 +92,8 @@ def self.parse_when_valid(uri) # @raise [Gitable::URI::InvalidURIError] When the uri is *total* rubbish. # def self.heuristic_parse(uri) - return uri if uri.nil? || uri.kind_of?(self) + return unless uri + return uri if uri.is_a?(self) # Addressable::URI.heuristic_parse _does_ return the correct type :) gitable = super # boo inconsistency @@ -107,21 +108,25 @@ def self.heuristic_parse(uri) # # @return [Boolean] github.com is the host? def github? - !!normalized_host.to_s.match(/\.?github.com$/) + host_match?("github.com") end # Is this uri a gitlab uri? # # @return [Boolean] gitlab.com is the host? def gitlab? - !!normalized_host.to_s.match(/\.?gitlab.com$/) + host_match?("gitlab.com") end # Is this uri a bitbucket uri? # # @return [Boolean] bitbucket.org is the host? def bitbucket? - !!normalized_host.to_s.match(/\.?bitbucket.org$/) + host_match?("bitbucket.org") + end + + def host_match?(host) + normalized_host && normalized_host.include?(host) end # Create a web link uri for repositories that follow the github pattern. @@ -141,11 +146,16 @@ def to_web_uri(uri_scheme='https') # # @return [String] Project name without .git def project_name - basename.sub(/\.git\/?$/,'') + p = basename.delete_suffix("/") + p.delete_suffix!(".git") + p end def org_project - normalized_path.sub(/^\//,'').sub(/\.git\/?$/,'') + op = normalized_path.delete_prefix("/") + op.delete_suffix!("/") + op.delete_suffix!(".git") + op end # Detect local filesystem URIs. @@ -159,7 +169,9 @@ def local? # # @return [Boolean] Is the URI local def inferred_scheme - if normalized_scheme == 'file' || (normalized_scheme.to_s.empty? && normalized_host.to_s.empty?) + if normalized_scheme == 'file' + 'file' + elsif (normalized_scheme.nil? || normalized_scheme.empty?) && (normalized_host.nil? || normalized_host.empty?) 'file' else normalized_scheme @@ -170,7 +182,7 @@ def inferred_scheme # # @return [Boolean] true if the URI uses ssh? def ssh? - !!normalized_scheme.to_s.match(/ssh/) + !normalized_scheme.nil? && normalized_scheme.include?("ssh") end # Is this an scp formatted uri? (No, always) @@ -213,7 +225,7 @@ def equivalent?(other_uri) org_project == other.org_project else # if the path is absolute, we can assume it's the same for all users (so the user doesn't have to match). - normalized_path.sub(/\/$/,'') == other.normalized_path.sub(/\/$/,'') && + normalized_path.delete_suffix("/") == other.normalized_path.delete_suffix("/") && (path[0] == '/' || normalized_user == other.normalized_user) end end