Class: VagrantPlugins::BoxGCS::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-box-gcs/storage.rb

Class Method Summary collapse

Class Method Details

.download(uri, dest) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/vagrant-box-gcs/storage.rb', line 40

def self.download(uri, dest)
  init!
  file = remote_file(uri)
  raise Errors::MissingFileError, message: "No URLs matched: #{uri}" if file.nil?

  downloaded = file.download dest
  Pathname.new(downloaded.path)
end

.init!Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/vagrant-box-gcs/storage.rb', line 8

def self.init!
  return if defined?(@storage)

  begin
    @storage = Google::Cloud::Storage.new
  rescue => e
    raise Errors::CredentialsError,
      name: e.class.to_s,
      message: e.message
  end
end

.metadata_url?(uri) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/vagrant-box-gcs/storage.rb', line 34

def self.(uri)
  init!
  file = remote_file(uri)
  file.content_type == 'application/json'
end

.remote_file(uri) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/vagrant-box-gcs/storage.rb', line 20

def self.remote_file(uri)
  init!
  begin
    bucket = @storage.bucket uri.host
    bucket.file uri.path[1..-1]
  rescue => e
    body = JSON.parse(e.body)
    raise Errors::UnauthorizedError,
      name: e.message,
      code: e.status_code,
      message: body['error']['message']
  end
end