class Frizz::Remote

Attributes

bucket_name[R]
ignorance[R]
options[R]

Public Class Methods

new(bucket_name, ignorance, options = {}) click to toggle source
# File lib/frizz/remote.rb, line 6
def initialize(bucket_name, ignorance, options = {})
  @options = options
  @bucket_name = bucket_name
  @ignorance = ignorance
end

Public Instance Methods

delete(remote_file) click to toggle source
# File lib/frizz/remote.rb, line 32
def delete(remote_file)
  client.delete_object(
    bucket: bucket_name,
    key: remote_file.key
  )
end
files() click to toggle source
# File lib/frizz/remote.rb, line 12
def files
  @files ||= objects.reject { |o| ignore?(o) }
end
upload(file, key, options = {}) click to toggle source
# File lib/frizz/remote.rb, line 16
def upload(file, key, options = {})
  mime_type = MIME::Types.type_for(key).first

  object_options = {
    bucket: bucket_name,
    body: file,
    acl: 'public-read',
    content_type: mime_type && mime_type.content_type || 'text/plain',
    key: key
  }

  object_options[:website_redirect_location] = options[:redirect_to] if options[:redirect_to]

  client.put_object object_options
end

Private Instance Methods

client() click to toggle source
# File lib/frizz/remote.rb, line 51
def client
  @client ||= Aws::S3::Client.new(
    region: options[:region],
    credentials: Aws::Credentials.new(
      Frizz.configuration.access_key_id,
      Frizz.configuration.secret_access_key
    )
  )
end
ignore?(object) click to toggle source
# File lib/frizz/remote.rb, line 43
def ignore?(object)
  ignorance.ignore?(object.key)
end
objects() click to toggle source
# File lib/frizz/remote.rb, line 47
def objects
  paginate(client.list_objects(bucket: bucket_name), [])
end
paginate(response, contents = []) click to toggle source
# File lib/frizz/remote.rb, line 61
def paginate(response, contents = [])
  contents.push *response.contents
  response.next_page? ? paginate(response.next_page, contents) : contents
end