Class: Hackmac::URLDownload

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

Overview

A class that provides functionality for downloading assets from URLs with version tracking

The URLDownload class encapsulates the logic for managing downloadable assets by storing their name, version, and download URL. It provides methods to retrieve the asset data and metadata, making it suitable for use in systems that need to track and download software components from web sources.

Examples:

downloader = Hackmac::URLDownload.new('Foo', '1.0.0', 'https://example.com/foo.zip')
# Configures a download source with name, version, and URL for later retrieval

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version, url) ⇒ URLDownload

The initialize method sets up a URLDownload instance by storing the provided name, URL, and version information.

This method takes the necessary parameters to configure the download source and converts the version string into a Version object for later comparison.

Parameters:

  • name (String)

    the descriptive name of the downloadable asset

  • version (String)

    the version identifier to be parsed and stored

  • url (String)

    the web address where the asset can be retrieved



30
31
32
33
34
# File 'lib/hackmac/url_download.rb', line 30

def initialize(name, version, url)
  @name    = name
  @url     = url
  @version = Version.new(version)
end

Instance Attribute Details

#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



44
45
46
# File 'lib/hackmac/url_download.rb', line 44

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



54
55
56
# File 'lib/hackmac/url_download.rb', line 54

def version
  @version
end

Instance Method Details

#download_assetArray<String, String>?

The download_asset method retrieves binary data from a configured URL

This method performs an HTTP GET request to the stored URL to download the associated asset file. It returns both the filename derived from the URL and the raw binary data content.

Returns:

  • (Array<String, String>)

    an array containing the filename and downloaded data

  • (nil)

    returns nil if no URL is configured for this instance



64
65
66
67
68
69
70
71
# File 'lib/hackmac/url_download.rb', line 64

def download_asset
  data = URI.open(
    @url,
    'Accept' => 'application/octet-stream',
    &:read
  )
  return File.basename(@url), 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



78
79
80
# File 'lib/hackmac/url_download.rb', line 78

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



88
89
90
# File 'lib/hackmac/url_download.rb', line 88

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