Class: Firebaseimageupload::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/firebaseimageupload.rb

Instance Method Summary collapse

Constructor Details

#initialize(project_id, credentials) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
# File 'lib/firebaseimageupload.rb', line 11

def initialize(project_id, credentials)
  @storage = Google::Cloud::Storage.new(
    project_id: project_id,
    credentials: credentials
  )
  # @storage.authorization.fetch_access_token!
end

Instance Method Details

#upload(file, bucket_name, object_name) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/firebaseimageupload.rb', line 19

def upload(file, bucket_name, object_name)
  bucket = @storage.bucket(bucket_name)

  if bucket.nil?
    raise Error, "Bucket '#{bucket_name}' not found"
  end

  file_obj = bucket.create_file(file, object_name)

  if file_obj.nil?
    raise Error, "Failed to upload file '#{file}' to '#{bucket_name}/#{object_name}'"
  end

  file_obj.public_url
end