Class: Dragonfly::GoogleDataStore

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly/google_data_store.rb,
lib/dragonfly/google_data_store/version.rb

Constant Summary collapse

VERSION =
"1.0.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ GoogleDataStore

Returns a new instance of GoogleDataStore.



9
10
11
12
13
# File 'lib/dragonfly/google_data_store.rb', line 9

def initialize(opts)
  @project = opts[:project]
  @keyfile = opts[:keyfile]
  @bucket_name = opts[:bucket]
end

Instance Attribute Details

#bucket_nameObject (readonly)

Returns the value of attribute bucket_name.



7
8
9
# File 'lib/dragonfly/google_data_store.rb', line 7

def bucket_name
  @bucket_name
end

#keyfileObject (readonly)

Returns the value of attribute keyfile.



7
8
9
# File 'lib/dragonfly/google_data_store.rb', line 7

def keyfile
  @keyfile
end

#projectObject (readonly)

Returns the value of attribute project.



7
8
9
# File 'lib/dragonfly/google_data_store.rb', line 7

def project
  @project
end

Class Method Details

.generate_uidObject



43
44
45
# File 'lib/dragonfly/google_data_store.rb', line 43

def self.generate_uid
  "#{Time.now.strftime('%Y/%m/%d/%H/%M')}/#{SecureRandom.uuid}"
end

Instance Method Details

#destroy(uid) ⇒ Object



37
38
39
40
41
# File 'lib/dragonfly/google_data_store.rb', line 37

def destroy(uid)
  bucket.file(uid).delete
rescue
  nil
end

#read(uid) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dragonfly/google_data_store.rb', line 25

def read(uid)
  file = bucket.file uid
  content = file.download
  content.rewind
  [
    content.read,
    file.,
  ]
rescue
  nil
end

#write(object, opts = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/dragonfly/google_data_store.rb', line 15

def write(object, opts = {})
  ensure_bucket_exists

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

  bucket.create_file object.tempfile.path, uid, metadata: object.meta

  uid
end