class Targit::Asset

Define asset object for a release

Attributes

asset[R]
name[R]
release[R]

Public Class Methods

new(asset, repo, tag, params = {}) click to toggle source
# File lib/targit/asset.rb, line 13
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

Public Instance Methods

already_exists?() click to toggle source
# File lib/targit/asset.rb, line 29
def already_exists?
  github_data != nil
end
delete!() click to toggle source
# File lib/targit/asset.rb, line 33
def delete!
  asset = github_data
  return unless asset
  client.delete_release_asset asset[:url]
end
github_data() click to toggle source
# File lib/targit/asset.rb, line 39
def github_data
  client.release_assets(@release.data[:url]).find { |x| x[:name] == @name }
end
upload!() click to toggle source
# File lib/targit/asset.rb, line 22
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
url() click to toggle source
# File lib/targit/asset.rb, line 43
def url
  data = github_data
  data ? data[:browser_download_url] : raise('Asset URL not found')
end

Private Instance Methods

_release(repo, tag) click to toggle source
# File lib/targit/asset.rb, line 50
def _release(repo, tag)
  Targit::Release.new(repo, tag, @options)
end
_upload_options() click to toggle source
# File lib/targit/asset.rb, line 54
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
guess_type() click to toggle source
# File lib/targit/asset.rb, line 62
def guess_type
  mime_type = MIME::Types.type_for(@asset).first
  mime_type ? mime_type.content_type : 'application/octet-stream'
end