Class: HomebrewAutomation::SourceDist

Inherits:
Object
  • Object
show all
Defined in:
lib/homebrew_automation/source_dist.rb

Overview

A representation of a source distribution tarball file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, repo, tag, http: RestClient) ⇒ SourceDist

Assign args to attributes #user, #repo, #tag



10
11
12
13
14
15
# File 'lib/homebrew_automation/source_dist.rb', line 10

def initialize user, repo, tag, http: RestClient
  @user = user
  @repo = repo
  @tag = tag
  @http = http
end

Instance Attribute Details

#repoString (readonly)

Github repo name, as appears in Github URLs

Returns:

  • (String)


25
26
27
# File 'lib/homebrew_automation/source_dist.rb', line 25

def repo
  @repo
end

#tagString (readonly)

Git tag name, as usable in git commands

Returns:

  • (String)


30
31
32
# File 'lib/homebrew_automation/source_dist.rb', line 30

def tag
  @tag
end

#userString (readonly)

Github username, as appears in Github URLs

Returns:

  • (String)


20
21
22
# File 'lib/homebrew_automation/source_dist.rb', line 20

def user
  @user
end

Instance Method Details

#contentsString

Download and return the file contents.

Lazy and memoized.

Returns:

  • (String)

    contents of the file



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/homebrew_automation/source_dist.rb', line 46

def contents
  @contents = @contents ||
    begin
      resp = @http.get url
      case resp.code
      when 200
        resp.body.to_s
      else
        puts resp
        raise StandardError.new resp.code
      end
    end
end

#sha256String

Calculate and return the file’s checksum.

Lazy and memoized. Download the file if we haven’t already.

Returns:

  • (String)

    hex-encoded string representation of the checksum



37
38
39
# File 'lib/homebrew_automation/source_dist.rb', line 37

def sha256
  @sha256 ||= Digest::SHA256.hexdigest contents
end

#urlString

The URL to the source tarball Github generates for tagged commits

Returns:

  • (String)


63
64
65
# File 'lib/homebrew_automation/source_dist.rb', line 63

def url
  "https://github.com/#{@user}/#{@repo}/archive/#{@tag}.tar.gz"
end