class VagrantCloud::Box::Provider

Constants

DirectUpload

Result for upload requests to upload directly to the storage backend.

@param [String] upload_url URL for uploading file asset @param [String] callback_url URL callback to PUT after successful upload @param [Proc] callback Callable proc to perform callback via configured client

Attributes

version[R]

Public Class Methods

new(version:, **opts) click to toggle source
Calls superclass method
# File lib/vagrant_cloud/box/provider.rb, line 21
def initialize(version:, **opts)
  if !version.is_a?(Version)
    raise TypeError, "Expecting type `#{Version.name}` but received `#{version.class.name}`"
  end
  @version = version
  super(**opts)
end

Public Instance Methods

delete() click to toggle source

Delete this provider

@return [nil]

# File lib/vagrant_cloud/box/provider.rb, line 32
def delete
  if exist?
    version.box.organization.account.client.box_version_provider_delete(
      username: version.box.username,
      name: version.box.name,
      version: version.version,
      provider: name
    )
    pv = version.providers.dup
    pv.delete(self)
    version.clean(data: {providers: pv})
  end
  nil
end
dirty?(key=nil, **args) click to toggle source

Check if this instance is dirty

@param [Boolean] deep Check nested instances @return [Boolean] instance is dirty

Calls superclass method
# File lib/vagrant_cloud/box/provider.rb, line 135
def dirty?(key=nil, **args)
  if key
    super(key)
  else
    super || !exist?
  end
end
exist?() click to toggle source

@return [Boolean] provider exists remotely

# File lib/vagrant_cloud/box/provider.rb, line 127
def exist?
  !!created_at
end
save() click to toggle source

Save the provider if any changes have been made

@return [self]

# File lib/vagrant_cloud/box/provider.rb, line 146
def save
  save_provider if dirty?
  self
end
upload(path: nil, direct: false) { |upload_url| ... } click to toggle source

Upload box file to be hosted on VagrantCloud. This method provides different behaviors based on the parameters passed. When the `direct` option is enabled the upload target will be directly to the backend storage. However, when the `direct` option is used the upload process becomes a two steps where a callback must be called after the upload is complete.

If the path is provided, the file will be uploaded and the callback will be requested if the `direct` option is enabled.

If a block is provided, the upload URL will be yielded to the block. If the `direct` option is set, the callback will be automatically requested after the block execution has completed.

If no path or block is provided, the upload URL will be returned. If the `direct` option is set, the `DirectUpload` instance will be yielded and it is the caller's responsibility to issue the callback

@param [String] path Path to asset @param [Boolean] direct Upload directly to backend storage @yieldparam [String] url URL to upload asset @return [self, Object, String, DirectUpload] self when path provided, result of yield when block provided, URL otherwise @note The callback request uses PUT request method

# File lib/vagrant_cloud/box/provider.rb, line 74
def upload(path: nil, direct: false)
  if !exist?
    raise Error::BoxError::ProviderNotFoundError,
      "Provider #{name} not found for box #{version.box.tag} version #{version.version}"
  end
  if path && block_given?
    raise ArgumentError,
      "Only path or block may be provided, not both"
  end
  if path && !File.exist?(path)
    raise Errno::ENOENT, path
  end
  req_args = {
    username: version.box.username,
    name: version.box.name,
    version: version.version,
    provider: name
  }
  if direct
    r = version.box.organization.account.client.box_version_provider_upload_direct(**req_args)
  else
    r = version.box.organization.account.client.box_version_provider_upload(**req_args)
  end
  result = DirectUpload.new(
    upload_url: r[:upload_path],
    callback_url: r[:callback],
    callback: proc {
      if r[:callback]
        version.box.organization.account.client.
          request(method: :put, path: URI.parse(r[:callback]).path)
      end
    }
  )
  if block_given?
    block_r = yield result.upload_url
    result[:callback].call
    block_r
  elsif path
    File.open(path, "rb") do |file|
      chunks = lambda { file.read(Excon.defaults[:chunk_size]).to_s }
      Excon.put(result.upload_url, request_block: chunks)
    end
    result[:callback].call
    self
  else
    # When returning upload information for requester to complete,
    # return upload URL when `direct` option is false, otherwise
    # return the `DirectUpload` instance
    direct ? result : result.upload_url
  end
end

Protected Instance Methods

save_provider() click to toggle source

Save the provider

@return [self]

# File lib/vagrant_cloud/box/provider.rb, line 156
def save_provider
  req_args = {
    username: version.box.username,
    name: version.box.name,
    version: version.version,
    provider: name,
    checksum: checksum,
    checksum_type: checksum_type
  }
  if exist?
    result = version.box.organization.account.client.box_version_provider_update(**req_args)
  else
    result = version.box.organization.account.client.box_version_provider_create(**req_args)
  end
  clean(data: result)
  self
end