Class: Hackmac::GithubSource

Inherits:
Object
  • Object
show all
Includes:
Tins::StringVersion
Defined in:
lib/hackmac/github_source.rb

Overview

A class that provides functionality for fetching and processing release information from GitHub repositories

The GithubSource class enables interaction with GitHub’s API to retrieve release data for a specified repository

It parses release information to identify the latest version and provides methods for downloading associated assets

Examples:

source = Hackmac::GithubSource.new('owner/repo', auth: ['user', 'token'])
# Fetches latest release information and allows asset downloads

Constant Summary collapse

GITHUB_API_URL =
'https://api.github.com/repos/%s/releases'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(github, auth: nil, suffix: nil) ⇒ GithubSource

The initialize method sets up a GithubSource instance by fetching and parsing release information from the GitHub API.

This method takes a GitHub repository identifier and optional authentication credentials, then retrieves all releases for that repository via the GitHub API. It processes the release data to find the highest version number, extracts relevant metadata about that release, and stores the release information for later use in downloading assets.

Parameters:

  • github (String)

    the GitHub repository identifier in the format “owner/repo”

  • auth (Array<String>, nil) (defaults to: nil)

    optional basic authentication credentials [username, token]

  • suffix (String, nil) (defaults to: nil)

    optional suffix to filter asset names when downloading



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hackmac/github_source.rb', line 38

def initialize(github, auth: nil, suffix: nil)
  @github  = github
  @auth    = auth
  @suffix  = (Regexp.quote(suffix) if suffix)
  , repo = github.split(?/)
  @name = repo
  releases = URI.open(
    GITHUB_API_URL % github,
    http_basic_authentication: auth) { |o|
    JSON.parse(o.read, object_class: JSON::GenericObject)
  }
  if max_version = releases.map { |r|
      next unless r.tag_name.include?(?.)
      tag = r.tag_name.delete '^.0-9'
      begin
        [ Version.new(tag), r ]
      rescue ArgumentError
      end
    }.compact.max_by(&:first)
  then
    @version, @release = max_version
  end
end

Instance Attribute Details

#authArray<String>? (readonly)

The auth reader method provides access to the auth attribute that was set during object initialization.

This method returns the value of the auth instance variable, which typically represents authentication credentials used for accessing protected resources or services.

Returns:

  • (Array<String>, nil)

    the auth value stored in the instance variable, or nil if not set



100
101
102
# File 'lib/hackmac/github_source.rb', line 100

def auth
  @auth
end

#githubString (readonly)

The github reader method provides access to the github attribute that was set during object initialization.

This method returns the value of the github instance variable, which typically represents the GitHub repository identifier in the format “owner/repo” associated with the object.

Returns:

  • (String)

    the github value stored in the instance variable



90
91
92
# File 'lib/hackmac/github_source.rb', line 90

def github
  @github
end

#nameString (readonly)

The name reader method provides access to the name attribute that was set during object initialization.

This method returns the value of the name instance variable, which typically represents the descriptive identifier or label associated with the object.

Returns:

  • (String)

    the name value stored in the instance variable



70
71
72
# File 'lib/hackmac/github_source.rb', line 70

def name
  @name
end

#versionString? (readonly)

The version reader method provides access to the version attribute that was set during object initialization.

This method returns the value of the version instance variable, which typically represents the semantic version number associated with the object’s current state or configuration.

Returns:

  • (String, nil)

    the version value stored in the instance variable, or nil if not set



80
81
82
# File 'lib/hackmac/github_source.rb', line 80

def version
  @version
end

Instance Method Details

#download_assetArray<String, String>?

The download_asset method retrieves a compressed asset file from a GitHub release by finding the appropriate asset based on a suffix filter and downloading its contents as binary data

Returns:

  • (Array<String, String>, nil)

    returns an array containing the asset filename and downloaded data if successful, or nil if no suitable asset is found or if there is no release information available



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/hackmac/github_source.rb', line 110

def download_asset
  @release or return
  asset = @release.assets.find { |a| a.name =~ /#@suffix.*\.(zip|tar\.gz)\z/i } or return
  data = URI.open(
    (GITHUB_API_URL % github) + ("/assets/%s" % asset.id),
    'Accept' => 'application/octet-stream',
    http_basic_authentication: auth,
    &:read
  )
  return asset.name, data
end

#inspectString

The inspect method returns a string representation of the object that includes its class name and string value

Returns:

  • (String)

    a formatted string containing the object’s class name and its string representation



127
128
129
# File 'lib/hackmac/github_source.rb', line 127

def inspect
  "#<#{self.class}: #{to_s}>"
end

#to_sString

The to_s method returns a string representation of the object by combining its name and version attributes into a single space-separated string.

Returns:

  • (String)

    a formatted string containing the name and version separated by a space



136
137
138
# File 'lib/hackmac/github_source.rb', line 136

def to_s
  "#{name} #{version}"
end