Class: FramedUploader::Uploader

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

Constant Summary collapse

CREDS_ENDPOINT =
'https://app.framed.io/uploads/1.0/credentials'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Uploader

Returns a new instance of Uploader.



13
14
15
# File 'lib/framed_uploader/uploader.rb', line 13

def initialize(api_key)
  @api_key = api_key
end

Instance Method Details

#upload(*filenames) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/framed_uploader/uploader.rb', line 17

def upload(*filenames)
  validate_files!(filenames)

  creds_response = get_credentials!
  tmpl = Addressable::Template.new(creds_response.fetch(:template))
  bucket = creds_response.fetch(:bucket)
  company_id = creds_response.fetch(:company_id)
  batch_timestamp = Time.now.to_i
  s3 = s3_client(creds_response)

  filenames.each do |filename|
    s3_key = tmpl.expand({"company_id" => company_id,
                          "timestamp" => batch_timestamp,
                          "filename" => filename}).path

    File.open(filename, 'rb') do |body|
      s3.put_object(bucket: bucket, key: s3_key, body: body)
    end
  end
end