class Dragonfly::GoogleDataStore

Constants

VERSION

Attributes

bucket_name[R]
keyfile[R]
project[R]
root_path[R]

Public Class Methods

generate_uid() click to toggle source
# File lib/dragonfly/google_data_store.rb, line 52
def self.generate_uid
  "#{Time.now.strftime('%Y/%m/%d/%H/%M')}/#{SecureRandom.uuid}"
end
new(opts) click to toggle source
# File lib/dragonfly/google_data_store.rb, line 9
def initialize(opts)
  @project = opts[:project]
  @keyfile = opts[:keyfile]
  @bucket_name = opts[:bucket]
  @root_path = opts[:root_path]
end

Public Instance Methods

destroy(uid) click to toggle source
# File lib/dragonfly/google_data_store.rb, line 45
def destroy(uid)
  file = bucket.file full_path(uid)
  file.delete
rescue StandardError
  nil
end
read(uid) click to toggle source
# File lib/dragonfly/google_data_store.rb, line 31
def read(uid)
  file = bucket.file full_path(uid)

  metadata = file.metadata.dup
  metadata['name'] ||= File.basename(file.name)

  content = file.download
  content.rewind

  [content.read, metadata]
rescue StandardError
  nil
end
write(object, opts = {}) click to toggle source
# File lib/dragonfly/google_data_store.rb, line 16
def write(object, opts = {})
  ensure_bucket_exists

  uid = opts[:path] || Dragonfly::GoogleDataStore.generate_uid

  bucket.create_file(
    object.tempfile.path,
    full_path(uid),
    metadata: object.meta,
    content_type: object.mime_type
  )

  uid
end

Private Instance Methods

bucket() click to toggle source
# File lib/dragonfly/google_data_store.rb, line 62
def bucket
  @bucket ||= storage.bucket(bucket_name)
end
ensure_bucket_exists() click to toggle source
# File lib/dragonfly/google_data_store.rb, line 66
def ensure_bucket_exists
  storage.create_bucket(bucket_name) unless bucket
end
full_path(uid) click to toggle source
# File lib/dragonfly/google_data_store.rb, line 58
def full_path(uid)
  File.join *[root_path, uid].compact
end
storage() click to toggle source
# File lib/dragonfly/google_data_store.rb, line 70
def storage
  @storage ||= Google::Cloud::Storage.new(project: project, keyfile: keyfile)
end