Class: Upcloudify::S3

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

Instance Method Summary collapse

Instance Method Details

#cloudObject

Internal: Connects to Amazon S3 using sored credentials Returns an object handle for the S3 bucket-directory



22
23
24
25
26
27
28
29
30
# File 'lib/upcloudify.rb', line 22

def cloud
  connection = Fog::Storage.new(
    :provider                 => 'AWS',
    :aws_secret_access_key    => Upcloudify.configuration.aws_secret_access_key,
    :aws_access_key_id        => Upcloudify.configuration.aws_access_key_id,
  )
  directory = connection.directories.get(Upcloudify.configuration.aws_directory)
  directory.files
end

#email(email, filename, attachment, options = {suffix: " generated on #{Time.now.to_s}", expiration: (Date.today + 30).to_time, from: 'upcloudify', subject: 'your file is attached', body: 'your report is linked '}) ⇒ Object

Public: Uploads a file to S3 and emails a link to the file. Returns nothing.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/upcloudify.rb', line 45

def email(email,
  filename,
  attachment,
  options={suffix: " generated on #{Time.now.to_s}",
    expiration: (Date.today + 30).to_time,
    from: 'upcloudify',
    subject: 'your file is attached',
    body: 'your report is linked '})
  
  suffix = options[:suffix]
  expiration = options[:expiration]
  file = upload((filename.to_s + suffix.to_s).parameterize, attachment)

  Pony.mail to: email,
    from: options[:from],
    subject: options[:subject],
    body: (options[:body] || '') + file.url(expiration)

end

#upload(filename, data) ⇒ Object

Internal: Uploads data into Amazon S3 Returns an object representing the uploaded file



34
35
36
37
38
39
40
41
# File 'lib/upcloudify.rb', line 34

def upload(filename, data)
  file = cloud.create(
    key: "#{filename}.zip",
    body: Zippy.new("#{filename}.csv" => data).data,
    :public => false
  )
  file
end