Sha256: eb256a228c8233d3b02bf162636297c13dcbbbb50b1ef170be63a84b6f9f8ef3

Contents?: true

Size: 1.52 KB

Versions: 23

Compression:

Stored size: 1.52 KB

Contents

require 'open-uri'
require 'json'
require 'tins/string_version'

module Hackmac
  class GithubSource
    GITHUB_API_URL = 'https://api.github.com/repos/%s/releases'

    include Tins::StringVersion

    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

    attr_reader :name

    attr_reader :version

    attr_reader :github

    attr_reader :auth

    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

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

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

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

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
hackmac-1.8.0 lib/hackmac/github_source.rb
hackmac-1.7.1 lib/hackmac/github_source.rb
hackmac-1.7.0 lib/hackmac/github_source.rb
hackmac-1.6.1 lib/hackmac/github_source.rb
hackmac-1.6.0 lib/hackmac/github_source.rb
hackmac-1.5.1 lib/hackmac/github_source.rb
hackmac-1.5.0 lib/hackmac/github_source.rb
hackmac-1.4.2 lib/hackmac/github_source.rb
hackmac-1.4.1 lib/hackmac/github_source.rb
hackmac-1.4.0 lib/hackmac/github_source.rb
hackmac-1.3.0 lib/hackmac/github_source.rb
hackmac-1.2.1 lib/hackmac/github_source.rb
hackmac-1.2.0 lib/hackmac/github_source.rb
hackmac-1.1.3 lib/hackmac/github_source.rb
hackmac-1.1.2 lib/hackmac/github_source.rb
hackmac-1.1.1 lib/hackmac/github_source.rb
hackmac-1.1.0 lib/hackmac/github_source.rb
hackmac-1.0.5 lib/hackmac/github_source.rb
hackmac-1.0.4 lib/hackmac/github_source.rb
hackmac-1.0.3 lib/hackmac/github_source.rb