Class: Gemview::GitRepo

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

Constant Summary collapse

HOSTS =
[
  GITHUB = :github,
  GITLAB = :gitlab,
  CODEBERG = :codeberg
].freeze
HTTPS_PORT =
443

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_uri:, changelog_uri:, git_host:, version:) ⇒ GitRepo

Returns a new instance of GitRepo.

Parameters:

  • base_uri (String)

    base Git repo uri for ‘HOSTS`

  • changelog_uri (String, nil)

    from the gem metadata

  • git_host (Symbol)

    from ‘HOSTS`

  • version (String)

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
# File 'lib/gemview/git_repo.rb', line 54

def initialize(base_uri:, changelog_uri:, git_host:, version:)
  raise ArgumentError, "Invalid host: #{git_host}" unless HOSTS.include?(git_host)

  @base_uri = base_uri.dup.freeze
  @changelog_uri = changelog_uri.dup.freeze
  @git_host = git_host
  @version = version.dup.freeze
end

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



48
49
50
# File 'lib/gemview/git_repo.rb', line 48

def base_uri
  @base_uri
end

#changelog_uriObject (readonly)

Returns the value of attribute changelog_uri.



48
49
50
# File 'lib/gemview/git_repo.rb', line 48

def changelog_uri
  @changelog_uri
end

#git_hostObject (readonly)

Returns the value of attribute git_host.



48
49
50
# File 'lib/gemview/git_repo.rb', line 48

def git_host
  @git_host
end

#versionObject (readonly)

Returns the value of attribute version.



48
49
50
# File 'lib/gemview/git_repo.rb', line 48

def version
  @version
end

Class Method Details

.from_urls(homepage_uri:, source_code_uri:, changelog_uri:, version:) ⇒ Gemview::GitRepo?

Parameters:

  • homepage_uri (String, nil)
  • source_code_uri (String, nil)
  • changelog_uri (String, nil)
  • version (String)

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gemview/git_repo.rb', line 18

def self.from_urls(homepage_uri:, source_code_uri:, changelog_uri:, version:)
  [homepage_uri, source_code_uri, changelog_uri].compact.each do |uri|
    base_uri, git_host = parse_base_uri(uri)
    if base_uri && git_host
      return new(
        base_uri: base_uri,
        changelog_uri: changelog_uri,
        git_host: git_host,
        version: version
      )
    end
  end
  nil
end

.parse_base_uri(uri) ⇒ base_uri as `String` and git_host as `Symbol`

Returns or nil if unsuccessful.

Parameters:

  • (String)

Returns:



35
36
37
38
39
40
41
42
43
44
# File 'lib/gemview/git_repo.rb', line 35

def self.parse_base_uri(uri)
  github_base_uri = uri[%r{^https?://github\.com/[^/]+/[^/]+}, 0]
  return [github_base_uri, GITHUB] if github_base_uri

  gitlab_base_uri = uri[%r{^https?://gitlab\.com/[^/]+/[^/]+}, 0]
  return [gitlab_base_uri, GITLAB] if gitlab_base_uri

  codeberg_base_uri = uri[%r{^https?://codeberg\.org/[^/]+/[^/]+}, 0]
  [codeberg_base_uri, CODEBERG] if codeberg_base_uri
end

Instance Method Details

#changelogString?

Returns:

  • (String, nil)


77
78
79
80
81
82
83
84
85
86
# File 'lib/gemview/git_repo.rb', line 77

def changelog
  return @changelog if defined?(@changelog)

  filenames = [changelog_filename, "CHANGELOG.md"].compact.uniq
  filenames.each do |filename|
    break if (@changelog = fetch_raw_file(filename))
  end

  @changelog
end

#changelog?Boolean

Returns:

  • (Boolean)


74
# File 'lib/gemview/git_repo.rb', line 74

def changelog? = !defined?(@changelog) || !changelog.nil?

#readmeString?

Returns:

  • (String, nil)


67
68
69
70
71
# File 'lib/gemview/git_repo.rb', line 67

def readme
  return @readme if defined?(@readme)

  @readme = fetch_raw_file("README.md")
end

#readme?Boolean

Returns:

  • (Boolean)


64
# File 'lib/gemview/git_repo.rb', line 64

def readme? = !defined?(@readme) || !readme.nil?