Class: Hackmac::GithubSource

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

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

Returns a new instance of GithubSource.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hackmac/github_source.rb', line 11

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

#authObject (readonly)

Returns the value of attribute auth.



41
42
43
# File 'lib/hackmac/github_source.rb', line 41

def auth
  @auth
end

#githubObject (readonly)

Returns the value of attribute github.



39
40
41
# File 'lib/hackmac/github_source.rb', line 39

def github
  @github
end

#nameObject (readonly)

Returns the value of attribute name.



35
36
37
# File 'lib/hackmac/github_source.rb', line 35

def name
  @name
end

#versionObject (readonly)

Returns the value of attribute version.



37
38
39
# File 'lib/hackmac/github_source.rb', line 37

def version
  @version
end

Instance Method Details

#download_assetObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hackmac/github_source.rb', line 43

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

#inspectObject



55
56
57
# File 'lib/hackmac/github_source.rb', line 55

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

#to_sObject



59
60
61
# File 'lib/hackmac/github_source.rb', line 59

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