Class: Hackmac::GithubSource
- Inherits:
-
Object
- Object
- Hackmac::GithubSource
- 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
-
#auth ⇒ Object
readonly
Returns the value of attribute auth.
-
#github ⇒ Object
readonly
Returns the value of attribute github.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #download_asset ⇒ Object
-
#initialize(github, auth: nil, suffix: nil) ⇒ GithubSource
constructor
A new instance of GithubSource.
- #inspect ⇒ Object
- #to_s ⇒ Object
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) 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 ⇒ Object (readonly)
Returns the value of attribute auth.
41 42 43 |
# File 'lib/hackmac/github_source.rb', line 41 def auth @auth end |
#github ⇒ Object (readonly)
Returns the value of attribute github.
39 40 41 |
# File 'lib/hackmac/github_source.rb', line 39 def github @github end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
35 36 37 |
# File 'lib/hackmac/github_source.rb', line 35 def name @name end |
#version ⇒ Object (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_asset ⇒ Object
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 |
#inspect ⇒ Object
55 56 57 |
# File 'lib/hackmac/github_source.rb', line 55 def inspect "#<#{self.class}: #{to_s}>" end |
#to_s ⇒ Object
59 60 61 |
# File 'lib/hackmac/github_source.rb', line 59 def to_s "#{name} #{version}" end |