Skip to content

Commit

Permalink
StardardRb updates
Browse files Browse the repository at this point in the history
  • Loading branch information
martinemde committed Dec 15, 2023
1 parent c8f2a2e commit e23c260
Show file tree
Hide file tree
Showing 6 changed files with 495 additions and 490 deletions.
20 changes: 10 additions & 10 deletions lib/gitable/scp_uri.rb
@@ -1,24 +1,24 @@
require 'addressable/uri'
require 'gitable/uri'
# frozen_string_literal: true

require "addressable/uri"
require "gitable/uri"

module Gitable
class ScpURI < Gitable::URI

##
# Deprecated: This serves no purpose. You might as well just parse the URI.
def self.scp?(uri)
$stderr.puts "DEPRECATED: Gitable::ScpURI.scp?. You're better off parsing the URI and checking #scp?."
warn "DEPRECATED: Gitable::ScpURI.scp?. You're better off parsing the URI and checking #scp?."
Gitable::URI.parse(uri).scp?
end

##
# Deprecated: This serves no purpose. Just use Gitable::URI.parse.
def self.parse(uri)
$stderr.puts "DEPRECATED: Gitable::ScpURI.parse just runs Gitable::URI.parse. Please use this directly."
warn "DEPRECATED: Gitable::ScpURI.parse just runs Gitable::URI.parse. Please use this directly."
Gitable::URI.parse(uri)
end


# Keep URIs like this as they were input:
#
# git@github.com:martinemde/gitable.git
Expand All @@ -31,7 +31,7 @@ def self.parse(uri)
# @return [String] The same path passed in.
def path=(new_path)
super
if new_path[0] != '/' # addressable adds a / but scp-style uris are altered by this behavior
if new_path[0] != "/" # addressable adds a / but scp-style uris are altered by this behavior
@path = path.delete_prefix("/")
@normalized_path = nil
validate
Expand All @@ -47,13 +47,13 @@ def path=(new_path)
def to_s
@uri_string ||= "#{normalized_authority}:#{normalized_path}".force_encoding(Encoding::UTF_8)
end
alias to_str to_s
alias_method :to_str, :to_s

# Return the actual scheme even though we don't show it
#
# @return [String] always 'ssh' for scp style URIs
def inferred_scheme
'ssh'
"ssh"
end

# Scp style URIs are always ssh
Expand Down Expand Up @@ -95,7 +95,7 @@ def validate
end

def invalid!(reason)
raise InvalidURIError, "#{reason}: '#{to_s}'"
raise InvalidURIError, "#{reason}: '#{self}'"
end
end
end
49 changes: 25 additions & 24 deletions lib/gitable/uri.rb
@@ -1,9 +1,11 @@
require 'addressable/uri'
# frozen_string_literal: true

require "addressable/uri"

module Gitable
class URI < Addressable::URI
SCP_REGEXP = %r|^([^:/?#]+):([^:?#]*)$|
URIREGEX = %r|^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$|
SCP_REGEXP = %r{^([^:/?#]+):([^:?#]*)$}
URIREGEX = %r{^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$}

##
# Parse a git repository URI into a URI object.
Expand All @@ -25,7 +27,7 @@ def self.parse(uri)
# convert it to a string, then parse the string.
# We do the check this way because we don't want to accidentally
# cause a missing constant exception to be thrown.
if uri.class.name =~ /^URI\b/
if /^URI\b/.match?(uri.class.name)
uri = uri.to_s
end

Expand All @@ -38,15 +40,15 @@ def self.parse(uri)

# This Regexp supplied as an example in RFC 3986, and it works great.
fragments = uri.scan(URIREGEX)[0]
scheme = fragments[1]
scheme = fragments[1]
authority = fragments[3]
path = fragments[4]
query = fragments[6]
fragment = fragments[8]
path = fragments[4]
query = fragments[6]
fragment = fragments[8]
host = nil

if authority
host = authority.gsub(/^([^\[\]]*)@/, '').gsub(/:([^:@\[\]]*?)$/, '')
host = authority.gsub(/^([^\[\]]*)@/, "").gsub(/:([^:@\[\]]*?)$/, "")
else
authority = scheme
end
Expand Down Expand Up @@ -126,7 +128,7 @@ def bitbucket?
end

def host_match?(host)
normalized_host && normalized_host.include?(host)
normalized_host&.include?(host)
end

# Create a web link uri for repositories that follow the github pattern.
Expand All @@ -137,9 +139,9 @@ def host_match?(host)
#
# @param [String] Scheme of the web uri (smart defaults)
# @return [Addressable::URI] https://#{host}/#{path_without_git_extension}
def to_web_uri(uri_scheme='https')
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/?$#, ''))
Addressable::URI.new(scheme: uri_scheme, host: normalized_host, port: normalized_port, path: normalized_path.sub(%r{\.git/?$}, ""))
end

# Tries to guess the project name of the repository.
Expand All @@ -162,17 +164,17 @@ def org_project
#
# @return [Boolean] Is the URI local
def local?
inferred_scheme == 'file'
inferred_scheme == "file"
end

# Scheme inferred by the URI (URIs without hosts or schemes are assumed to be 'file')
#
# @return [Boolean] Is the URI local
def inferred_scheme
if normalized_scheme == 'file'
'file'
if normalized_scheme == "file"
"file"
elsif (normalized_scheme.nil? || normalized_scheme.empty?) && (normalized_host.nil? || normalized_host.empty?)
'file'
"file"
else
normalized_scheme
end
Expand Down Expand Up @@ -226,15 +228,15 @@ def equivalent?(other_uri)
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.delete_suffix("/") == other.normalized_path.delete_suffix("/") &&
(path[0] == '/' || normalized_user == other.normalized_user)
(path[0] == "/" || normalized_user == other.normalized_user)
end
end

# Dun da dun da dun, Inspector Gadget.
#
# @return [String] I'll get you next time Gadget, NEXT TIME!
def inspect
"#<#{self.class.to_s} #{to_s}>"
"#<#{self.class} #{self}>"
end

# Set an extension name, replacing one if it exists.
Expand All @@ -247,9 +249,8 @@ def inspect
# @param [String] New extension name
# @return [String] extname result
def extname=(new_ext)
return nil if basename.to_s.empty?
self.basename = "#{basename.sub(%r#\.git/?$#, '')}.#{new_ext.sub(/^\.+/,'')}"
extname
return if basename.to_s.empty?
self.basename = "#{basename.sub(%r{\.git/?$}, "")}.#{new_ext.sub(/^\.+/, "")}"
end

# Set the '.git' extension name, replacing one if it exists.
Expand All @@ -265,7 +266,7 @@ def set_git_extname
# Addressable does basename wrong when there's no basename.
# It returns "/" for something like "http://host.com/"
def basename
super == "/" ? "" : super
(super == "/") ? "" : super
end

# Set the basename, replacing it if it exists.
Expand All @@ -279,11 +280,11 @@ def basename=(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
self.path = rpath.sub(%r{#{Regexp.escape(base.reverse)}}, new_basename.reverse).reverse
end
basename
end
end
end

require 'gitable/scp_uri'
require "gitable/scp_uri"
8 changes: 6 additions & 2 deletions spec/describe_uri.rb
Expand Up @@ -21,8 +21,12 @@ def it_sets(parts)
end
end

def method_missing(*args, &block)
@example_group.send(*args, &block)
def method_missing(...)
@example_group.send(...)
end

def respond_to_missing?(...)
@example_group.respond_to_missing?(...)
end
end
end

0 comments on commit e23c260

Please sign in to comment.