Module: S3Log

Defined in:
lib/s3_log.rb,
lib/s3_log/version.rb,
lib/s3_log/exceptions.rb

Overview

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

Defined Under Namespace

Classes: Error, InvalidConfigError, Unconfigured

Constant Summary collapse

RequiredOptions =
[:access_key_id, :secret_access_key, :bucket]
VERSION =
'0.0.5'

Class Method Summary collapse

Class Method Details

.bucketFog::Storage::AWS::Directory, NilClass

Return the singleton's S3 bucket

Returns:

  • (Fog::Storage::AWS::Directory)

    if the logger is configured

  • (NilClass)

    if the logger is not configured



25
26
27
# File 'lib/s3_log.rb', line 25

def self.bucket
  @bucket
end

.clear_configurationNilClass

Clear the singleton logger's configuration

Returns:

  • (NilClass)


106
107
108
# File 'lib/s3_log.rb', line 106

def self.clear_configuration
  @bucket = @storage = nil
end

.configure(options = {}) ⇒ S3Log

Configure the logger so it can be used to write logs

Parameters:

  • options (Hash) (defaults to: {})

    The :access_key_id, :secret_access_key, and :bucket keys are required

Returns:

  • (S3Log)

    the singleton logger

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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?Boolean

Is the logger configured?

Returns:

  • (Boolean)

    true if the logger is configured, false otherwise



33
34
35
# File 'lib/s3_log.rb', line 33

def self.configured?
  storage && bucket
end

.read(path) ⇒ String

Read the given path

Returns:

  • (String)

    the content of the requested S3 file if present

  • (String)

    the empty string if the requested S3 file is not present

Raises:



93
94
95
96
97
98
99
100
# 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

.storageFog::Storage::AWS, BilClass

Return the singleton's S3 Storage object

Returns:

  • (Fog::Storage::AWS)

    if the logger is configured

  • (BilClass)

    if the logger is not configured



15
16
17
# File 'lib/s3_log.rb', line 15

def self.storage
  @storage
end

.write(path, content) ⇒ S3Log

Append the given content to the given S3 path

Returns:

  • (S3Log)

    the singleton logger

Raises:



71
72
73
74
75
76
77
78
79
80
81
82
83
# 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