Class: VagrantCloud::Box::Provider
- Inherits:
-
Data::Mutable
- Object
- Data
- Data::Immutable
- Data::Mutable
- VagrantCloud::Box::Provider
- Defined in:
- lib/vagrant_cloud/box/provider.rb
Defined Under Namespace
Classes: DirectUpload
Constant Summary
Constants inherited from Data
Instance Attribute Summary collapse
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#delete ⇒ nil
Delete this provider.
-
#dirty?(key = nil, **args) ⇒ Boolean
Check if this instance is dirty.
-
#exist? ⇒ Boolean
Provider exists remotely.
-
#initialize(version:, **opts) ⇒ Provider
constructor
A new instance of Provider.
-
#save ⇒ self
Save the provider if any changes have been made.
-
#upload(path: nil, direct: false) {|url| ... } ⇒ self, ...
Upload box file to be hosted on VagrantCloud.
Methods inherited from Data::Mutable
#[], attr_mutable, #clean, #clean!, #freeze, load
Methods inherited from Data::Immutable
attr_optional, attr_required, inherited, #inspect, sync
Methods inherited from Data
Constructor Details
#initialize(version:, **opts) ⇒ Provider
Returns a new instance of Provider.
21 22 23 24 25 26 27 |
# 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 |
Instance Attribute Details
#version ⇒ Object (readonly)
Returns the value of attribute version.
13 14 15 |
# File 'lib/vagrant_cloud/box/provider.rb', line 13 def version @version end |
Instance Method Details
#delete ⇒ nil
Delete this provider
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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, architecture: architecture ) pv = version.providers.dup pv.delete(self) version.clean(data: {providers: pv}) end nil end |
#dirty?(key = nil, **args) ⇒ Boolean
Check if this instance is dirty
137 138 139 140 141 142 143 |
# File 'lib/vagrant_cloud/box/provider.rb', line 137 def dirty?(key=nil, **args) if key super(key) else super || !exist? end end |
#exist? ⇒ Boolean
Returns provider exists remotely.
129 130 131 |
# File 'lib/vagrant_cloud/box/provider.rb', line 129 def exist? !!created_at end |
#save ⇒ self
Save the provider if any changes have been made
148 149 150 151 |
# File 'lib/vagrant_cloud/box/provider.rb', line 148 def save save_provider if dirty? self end |
#upload(path: nil, direct: false) {|url| ... } ⇒ self, ...
The callback request uses PUT request method
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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/vagrant_cloud/box/provider.rb', line 75 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, architecture: architecture, } 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 |