Class: Gitable::ScpURI

Inherits:
URI
  • Object
show all
Defined in:
lib/gitable/scp_uri.rb

Constant Summary collapse

REGEXP =
%r|^([^:/?#]+):([^:?#]*)$|

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from URI

#authenticated?, #basename, #basename=, #extname=, #github?, heuristic_parse, #local?, #project_name, #set_git_extname, #to_web_uri

Class Method Details

.parse(uri) ⇒ Gitable::URI?

Parse an Scp style git repository URI into a URI object.

Parameters:

  • uri (Addressable::URI, #to_str)

    URI of a git repository.

Returns:

  • (Gitable::URI, nil)

    the URI object or nil if nil was passed in.

Raises:

  • (TypeError)

    The uri must respond to #to_str.

  • (Gitable::URI::InvalidURIError)

    When the uri is total rubbish.



26
27
28
29
30
31
32
33
34
35
# File 'lib/gitable/scp_uri.rb', line 26

def self.parse(uri)
  return uri if uri.nil? || uri.kind_of?(self)

  if scp?(uri)
    authority, path = uri.scan(REGEXP).flatten
    Gitable::ScpURI.new(:authority => authority, :path => path)
  else
    raise InvalidURIError, "Unable to parse scp style URI: #{uri}"
  end
end

.scp?(uri) ⇒ Boolean

Expected to be an scp style URI if Addressable interprets it wrong and it matches our scp regexp

nil host is an Addressable misunderstanding (therefore it might be scp style)

Returns:

  • (Boolean)


12
13
14
# File 'lib/gitable/scp_uri.rb', line 12

def self.scp?(uri)
  uri && uri.match(REGEXP) && Addressable::URI.parse(uri).normalized_host.nil?
end

Instance Method Details

#inferred_schemeString

Return the actual scheme even though we don’t show it

Returns:

  • (String)

    always ‘ssh’ for scp style URIs



77
78
79
# File 'lib/gitable/scp_uri.rb', line 77

def inferred_scheme
  'ssh'
end

#path=(new_path) ⇒ String

Keep URIs like this as they were input:

git@github.com:martinemde/gitable.git

Without breaking URIs like these:

[email protected]:/home/martinemde/gitable.git

Parameters:

  • new_path (String)

    The new path to be set.

Returns:

  • (String)

    The same path passed in.



48
49
50
51
52
53
54
55
56
# File 'lib/gitable/scp_uri.rb', line 48

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|^/+|,'')
    @normalized_path = normalized_path.sub(%r|^/+|,'')
    validate
  end
  path
end

#ssh?true

Scp style URIs are always ssh

Returns:

  • (true)

    always ssh



84
85
86
# File 'lib/gitable/scp_uri.rb', line 84

def ssh?
  true
end

#to_sString

Get the URI as a string in the same form it was input.

Taken from Addressable::URI.

Returns:

  • (String)

    The URI as a string.



63
64
65
66
67
68
69
70
71
72
# File 'lib/gitable/scp_uri.rb', line 63

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
end