Class: Hackmac::GithubSource
- Inherits:
-
Object
- Object
- Hackmac::GithubSource
- 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
Constant Summary collapse
- GITHUB_API_URL =
'https://api.github.com/repos/%s/releases'
Instance Attribute Summary collapse
-
#auth ⇒ Array<String>?
readonly
The auth reader method provides access to the auth attribute that was set during object initialization.
-
#github ⇒ String
readonly
The github reader method provides access to the github attribute that was set during object initialization.
-
#name ⇒ String
readonly
The name reader method provides access to the name attribute that was set during object initialization.
-
#version ⇒ String?
readonly
The version reader method provides access to the version attribute that was set during object initialization.
Instance Method Summary collapse
-
#download_asset ⇒ Array<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.
-
#initialize(github, auth: nil, suffix: nil) ⇒ GithubSource
constructor
The initialize method sets up a GithubSource instance by fetching and parsing release information from the GitHub API.
-
#inspect ⇒ String
The inspect method returns a string representation of the object that includes its class name and string value.
-
#to_s ⇒ String
The to_s method returns a string representation of the object by combining its name and version attributes into a single space-separated string.
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.
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) _account, 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
#auth ⇒ Array<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.
100 101 102 |
# File 'lib/hackmac/github_source.rb', line 100 def auth @auth end |
#github ⇒ String (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.
90 91 92 |
# File 'lib/hackmac/github_source.rb', line 90 def github @github end |
#name ⇒ String (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.
70 71 72 |
# File 'lib/hackmac/github_source.rb', line 70 def name @name end |
#version ⇒ String? (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.
80 81 82 |
# File 'lib/hackmac/github_source.rb', line 80 def version @version end |
Instance Method Details
#download_asset ⇒ Array<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
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 |
#inspect ⇒ String
The inspect method returns a string representation of the object that includes its class name and string value
127 128 129 |
# File 'lib/hackmac/github_source.rb', line 127 def inspect "#<#{self.class}: #{to_s}>" end |
#to_s ⇒ String
The to_s method returns a string representation of the object by combining its name and version attributes into a single space-separated string.
136 137 138 |
# File 'lib/hackmac/github_source.rb', line 136 def to_s "#{name} #{version}" end |