Class: Bindan::Provider::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/bindan/providers/storage.rb

Defined Under Namespace

Classes: FileNotExist

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket, raise_error: nil, separator: "-", sdk: ::Google::Cloud::Storage, **kwargs) ⇒ Storage

Returns a new instance of Storage.

Parameters:

  • (defaults to: "-")
  • (defaults to: ::Google::Cloud::Storage)


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bindan/providers/storage.rb', line 40

def initialize(bucket, raise_error: nil, separator: "-", sdk: ::Google::Cloud::Storage, **kwargs)
  @_options = {}
  @bucket = bucket

  @_options[:raise_error] = raise_error
  @separator = separator
  @project_id = kwargs[:project_id]

  options = prepare_options(kwargs)

  @_storage = sdk.new(**options)
end

Instance Attribute Details

#project_id ⇒ Object (readonly)

Returns the value of attribute project_id.



52
53
54
# File 'lib/bindan/providers/storage.rb', line 52

def project_id
  @project_id
end

#separator ⇒ Object (readonly)

Returns the value of attribute separator.



52
53
54
# File 'lib/bindan/providers/storage.rb', line 52

def separator
  @separator
end

Instance Method Details

#[](file) ⇒ String

Parameters:

Returns:

Raises:

  • FileNotExist



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/bindan/providers/storage.rb', line 82

def [](file)
  remote = @_storage.bucket(bucket).file(file)

  if remote
    Tempfile.open { |t|
      remote.download(t.path)
      t.read.chomp
    }
  elsif @_options[:raise_error]
    raise FileNotExist.new file
  end
end

#bucket ⇒ String

decorated bucket name

Returns:



73
74
75
# File 'lib/bindan/providers/storage.rb', line 73

def bucket
  [@project_id, @bucket].join(@separator)
end

#prepare_options(options) ⇒ Hash

Parameters:

Returns:



58
59
60
61
62
63
64
65
66
# File 'lib/bindan/providers/storage.rb', line 58

def prepare_options(options)
  if ENV["STORAGE_EMULATOR_HOST"]
    u = URI(ENV["STORAGE_EMULATOR_HOST"].to_s)
    u.path = "/" if u.path.to_s.size == 0
    options[:endpoint] = u.to_s
  end

  options
end