Sha256: d7518b6c0b9029ecb834f4fc782ac8061319079219711550272b0a69feb151ad

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

require 'octokit'
require 'octoauth'
require 'mime-types'

module Targit
  ##
  # Define asset object for a release
  class Asset
    include Targit::Client

    attr_reader :release, :asset, :name, :github_data

    def initialize(asset, repo, tag, params = {})
      @options = params
      @options[:client] ||= client
      @release = _release repo, tag
      @asset = asset
      @upload_options = _upload_options
      @name = @options[:name] || File.basename(@asset)
    end

    def upload!
      delete! if @options[:force]
      raise('Release asset already exists') if already_exists?
      asset = client.upload_asset @release.data[:url], @asset, @upload_options
      client.release_asset asset[:url]
    end

    def already_exists?
      github_data != nil
    end

    def delete!
      asset = github_data
      return unless asset
      client.delete_release_asset asset[:url]
    end

    def github_data
      client.release_assets(@release.data[:url]).find { |x| x[:name] == @name }
    end

    def url
      data = github_data
      data ? data[:browser_download_url] : raise('Asset URL not found')
    end

    private

    def _release(repo, tag)
      Targit::Release.new(repo, tag, @options)
    end

    def _upload_options
      options = %i[name content_type].each_with_object({}) do |option, hash|
        hash[option] = @options[option] if @options[option]
      end
      options[:content_type] ||= guess_type
      options
    end

    def guess_type
      mime_type = MIME::Types.type_for(@asset).first
      mime_type ? mime_type.content_type : 'application/octet-stream'
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
targit-2.2.0 lib/targit/asset.rb
targit-2.1.0 lib/targit/asset.rb
targit-2.0.2 lib/targit/asset.rb