s3_loggable 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/s3_loggable.rb +11 -6
- data/lib/s3_loggable/configuration.rb +21 -0
- data/lib/s3_loggable/logger.rb +1 -1
- data/lib/s3_loggable/version.rb +1 -1
- data/s3_loggable.gemspec +0 -1
- data/test/test_s3_loggable.rb +27 -4
- data/test/test_s3_loggable_configuration.rb +29 -0
- metadata +4 -2
data/lib/s3_loggable.rb
CHANGED
@@ -1,26 +1,31 @@
|
|
1
1
|
require "fog"
|
2
2
|
require "s3_loggable/exceptions"
|
3
3
|
require "s3_loggable/logger"
|
4
|
+
require "s3_loggable/configuration"
|
4
5
|
|
5
6
|
module S3Loggable
|
6
7
|
|
7
8
|
RequiredCredentials = [:aws_access_key_id, :aws_secret_access_key]
|
8
9
|
|
9
10
|
def self.credentials?
|
10
|
-
|
11
|
-
|
11
|
+
set_credentials unless (Fog.credentials.keys & RequiredCredentials).count == RequiredCredentials.count
|
12
|
+
true
|
12
13
|
end
|
13
14
|
|
14
15
|
def self.set_credentials
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
key = (S3Loggable.configuration.aws_access_key_id || ENV["AWS_ACCESS_KEY_ID"])
|
17
|
+
secret = (S3Loggable.configuration.aws_secret_access_key || ENV["AWS_SECRET_ACCESS_KEY"])
|
18
|
+
|
19
|
+
if key and secret
|
20
|
+
Fog.credentials[:aws_access_key_id] = key
|
21
|
+
Fog.credentials[:aws_secret_access_key] = secret
|
18
22
|
else
|
19
23
|
raise ConfigurationError, "Set AWS access key id and secret access key"
|
20
24
|
end
|
21
25
|
end
|
22
26
|
|
23
|
-
def log_to_s3(
|
27
|
+
def log_to_s3(message, bucket = S3Loggable.configuration.default_bucket, id = self.id.to_s,
|
28
|
+
folder = self.class.to_s, date_time = DateTime.now)
|
24
29
|
S3Loggable::Logger.new(bucket).log_to_s3(message, id, folder, date_time)
|
25
30
|
end
|
26
31
|
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module S3Loggable
|
2
|
+
class << self
|
3
|
+
attr_writer :configuration
|
4
|
+
|
5
|
+
def configuration
|
6
|
+
@configuration ||= Configuration.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def configure
|
10
|
+
yield(self.configuration)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Configuration
|
15
|
+
attr_accessor \
|
16
|
+
:aws_access_key_id,
|
17
|
+
:aws_secret_access_key,
|
18
|
+
:default_bucket
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
data/lib/s3_loggable/logger.rb
CHANGED
@@ -5,7 +5,7 @@ module S3Loggable
|
|
5
5
|
class Logger
|
6
6
|
attr_reader :bucket, :s3
|
7
7
|
|
8
|
-
def initialize(bucket_name)
|
8
|
+
def initialize(bucket_name = S3Loggable.configuration.default_bucket)
|
9
9
|
@s3 = Fog::Storage.new({:provider => 'AWS'}) if S3Loggable.credentials?
|
10
10
|
set_s3_bucket(bucket_name)
|
11
11
|
end
|
data/lib/s3_loggable/version.rb
CHANGED
data/s3_loggable.gemspec
CHANGED
data/test/test_s3_loggable.rb
CHANGED
@@ -5,20 +5,43 @@ require "s3_loggable"
|
|
5
5
|
class TestS3Loggable < Test::Unit::TestCase
|
6
6
|
class TestClass; include S3Loggable; def self.initialize(*args); end; end
|
7
7
|
|
8
|
-
def
|
8
|
+
def test_credentials_are_unset_raises_an_exception
|
9
9
|
Fog.expects(:credentials).returns({})
|
10
|
-
ENV.expects(:[]).
|
10
|
+
ENV.expects(:[]).twice.returns(nil)
|
11
11
|
assert_raise(S3Loggable::ConfigurationError) do
|
12
12
|
S3Loggable.credentials?
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
16
|
+
def test_credentials_is_true_when_fog_is_already_used
|
17
17
|
Fog.expects(:credentials).returns({:aws_access_key_id => "ABC",
|
18
18
|
:aws_secret_access_key => "123"})
|
19
19
|
assert_equal(S3Loggable.credentials?, true)
|
20
20
|
end
|
21
21
|
|
22
|
+
def test_credentials_is_true_when_configuration_is_set
|
23
|
+
Fog.expects(:credentials).times(3).returns({})
|
24
|
+
config = mock('config')
|
25
|
+
|
26
|
+
S3Loggable.expects(:configuration).twice.returns(config)
|
27
|
+
config.expects(:aws_access_key_id).returns('ABC')
|
28
|
+
config.expects(:aws_secret_access_key).returns('123')
|
29
|
+
|
30
|
+
assert_equal(S3Loggable.credentials?, true)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_credentials_is_true_when_env_variables_are_set
|
34
|
+
Fog.expects(:credentials).times(3).returns({})
|
35
|
+
config = mock('config')
|
36
|
+
|
37
|
+
S3Loggable.expects(:configuration).twice.returns(config)
|
38
|
+
config.expects(:aws_access_key_id).returns(nil)
|
39
|
+
config.expects(:aws_secret_access_key).returns(nil)
|
40
|
+
ENV.expects(:[]).with("AWS_ACCESS_KEY_ID").returns("ABC")
|
41
|
+
ENV.expects(:[]).with("AWS_SECRET_ACCESS_KEY").returns("123")
|
42
|
+
|
43
|
+
assert_equal(S3Loggable.credentials?, true)
|
44
|
+
end
|
22
45
|
|
23
46
|
def test_log_to_s3_instance_method
|
24
47
|
logger = mock('logger')
|
@@ -31,6 +54,6 @@ class TestS3Loggable < Test::Unit::TestCase
|
|
31
54
|
S3Loggable::Logger.expects(:new).with(bucket).returns(logger)
|
32
55
|
logger.expects(:log_to_s3).returns(s3_file)
|
33
56
|
|
34
|
-
assert_equal(test_object.log_to_s3(
|
57
|
+
assert_equal(test_object.log_to_s3(message, bucket, id), s3_file)
|
35
58
|
end
|
36
59
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "mocha/setup"
|
3
|
+
require "s3_loggable"
|
4
|
+
|
5
|
+
class TestS3LoggableConfiguration < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_default_configurations_are_nil
|
8
|
+
assert_nil(S3Loggable.configuration.aws_access_key_id)
|
9
|
+
assert_nil(S3Loggable.configuration.aws_secret_access_key)
|
10
|
+
assert_nil(S3Loggable.configuration.default_bucket)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_paramaters_can_be_set_and_accessed
|
14
|
+
key = "ABC"
|
15
|
+
secret = "123"
|
16
|
+
bucket = "foo-bar"
|
17
|
+
|
18
|
+
S3Loggable.configure do |config|
|
19
|
+
config.aws_access_key_id = key
|
20
|
+
config.aws_secret_access_key = secret
|
21
|
+
config.default_bucket = bucket
|
22
|
+
end
|
23
|
+
|
24
|
+
assert_equal(S3Loggable.configuration.aws_access_key_id, key)
|
25
|
+
assert_equal(S3Loggable.configuration.aws_secret_access_key, secret)
|
26
|
+
assert_equal(S3Loggable.configuration.default_bucket, bucket)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: s3_loggable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -72,11 +72,13 @@ files:
|
|
72
72
|
- README.md
|
73
73
|
- Rakefile
|
74
74
|
- lib/s3_loggable.rb
|
75
|
+
- lib/s3_loggable/configuration.rb
|
75
76
|
- lib/s3_loggable/exceptions.rb
|
76
77
|
- lib/s3_loggable/logger.rb
|
77
78
|
- lib/s3_loggable/version.rb
|
78
79
|
- s3_loggable.gemspec
|
79
80
|
- test/test_s3_loggable.rb
|
81
|
+
- test/test_s3_loggable_configuration.rb
|
80
82
|
- test/test_s3_loggable_logger.rb
|
81
83
|
homepage: http://github.com/adaptly/s3_loggable
|
82
84
|
licenses: []
|