module S3Log

This module is a singleton that implements an append-style S3 file writer.

Constants

RequiredOptions
VERSION

Public Class Methods

bucket() click to toggle source

Return the singleton’s S3 bucket

@return [Fog::Storage::AWS::Directory] if the logger is configured

@return [NilClass] if the logger is not configured

# File lib/s3_log.rb, line 25
def self.bucket
  @bucket
end
clear_configuration() click to toggle source

Clear the singleton logger’s configuration

@return [NilClass]

# File lib/s3_log.rb, line 106
def self.clear_configuration
  @bucket = @storage = nil
end
configure(options = {}) click to toggle source

Configure the logger so it can be used to write logs

@param options [Hash] The :access_key_id, :secret_access_key, and :bucket

keys are required

@return [S3Log] the singleton logger

@raise [S3Log::InvalidConfigError] if not provided all required

configuration options
# File lib/s3_log.rb, line 47
def self.configure(options = {})
  missing = RequiredOptions - options.keys
  raise InvalidConfigError.new(missing) unless missing.empty?

  clear_configuration

  @storage = Fog::Storage::AWS.new(
    aws_access_key_id: options[:access_key_id],
    aws_secret_access_key: options[:secret_access_key]
  )

  @bucket = @storage.directories.create(
    key: options[:bucket],
    public: false
  )
  self
end
configured?() click to toggle source

Is the logger configured?

@return [Boolean] true if the logger is configured, false otherwise

# File lib/s3_log.rb, line 33
def self.configured?
  storage && bucket
end
read(path) click to toggle source

Read the given path

@return [String] the content of the requested S3 file if present

@return [String] the empty string if the requested S3 file is not present

@raise [S3Log::Unconfigured] if the logger has not been configured

# File lib/s3_log.rb, line 93
def self.read(path)
  unless configured?
    raise Unconfigured
  end

  file = bucket.files.get(path)
  file.nil? ? '' : file.body.to_s
end
storage() click to toggle source

Return the singleton’s S3 Storage object

@return [Fog::Storage::AWS] if the logger is configured

@return [BilClass] if the logger is not configured

# File lib/s3_log.rb, line 15
def self.storage
  @storage
end
write(path, content) click to toggle source

Append the given content to the given S3 path

@return [S3Log] the singleton logger

@raise [S3Log::Unconfigured] if the logger has not been configured

# File lib/s3_log.rb, line 71
def self.write(path, content)
  unless configured?
    raise Unconfigured
  end

  bucket.files.create(
    key: path,
    body: read(path).split("\n").push(content).join("\n"),
    public: false
  )

  self
end