Sha256: d6d2dc7c28663e253a4ce6161de2d8c3635c5500ac8026362ce82b18b2b9e461

Contents?: true

Size: 1.57 KB

Versions: 10

Compression:

Stored size: 1.57 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

    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

10 entries across 10 versions & 1 rubygems

Version Path
targit-2.4.8 lib/targit/asset.rb
targit-2.4.7 lib/targit/asset.rb
targit-2.4.6 lib/targit/asset.rb
targit-2.4.5 lib/targit/asset.rb
targit-2.4.4 lib/targit/asset.rb
targit-2.4.3 lib/targit/asset.rb
targit-2.4.2 lib/targit/asset.rb
targit-2.4.1 lib/targit/asset.rb
targit-2.4.0 lib/targit/asset.rb
targit-2.3.0 lib/targit/asset.rb