maintenance_mode 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2896353d3d28297a736ece0c8db187ea37fd1902
4
+ data.tar.gz: 6186d898dc13994dfdf53e2253a53327847d6b57
5
+ SHA512:
6
+ metadata.gz: 2fe2567bda7e6d787e7143841c9cefd451ae04eebae92d48bbc3333f84bfc8c83f459bb695cfaad5c767d697bdc52452288e6c9df23552c2fed120f79cb19c81
7
+ data.tar.gz: de1b522889974b205e9fb66048101a45052d7a7b6a37257d431a9713524894ccd5f60aefb86b86e0c0d0de1cfc3b015af31f766ac3b41005ec8c8528e9ff27b3
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in maintenance-mode.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 GoodGuide
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,103 @@
1
+ # MaintenanceMode
2
+
3
+ This provides a simple interface to a "maintenance mode" toggle with optional
4
+ maintenance message. It's not specific to maintenance mode, really, and could be
5
+ used whenever a similar switch is needed.
6
+
7
+ ## Usage
8
+
9
+ ```ruby
10
+ MaintenanceMode.enable
11
+ MaintenanceMode.as_json # => { "enabled": true, "message": null }
12
+ MaintenanceMode.enable "we'll be back soon"
13
+ MaintenanceMode.as_json # => { "enabled": true, "message": "we'll be back soon" }
14
+ MaintenanceMode.enabled? # => true
15
+ MaintenanceMode.message # => "we'll be back soon"
16
+ MaintenanceMode.disable
17
+ MaintenanceMode.enabled? # => false
18
+ ```
19
+
20
+ There are also rake tasks to toggle MaintenanceMode:
21
+
22
+ ```shell
23
+ rake maintenance:enable
24
+ rake maintenance:enable MESSAGE="Oh Noes!"
25
+ rake 'maintenance:enable[Oh Noes!]' # also works
26
+ rake maintenance:disable
27
+ ```
28
+
29
+ To get these tasks, you'll have to add this line to your `Rakefile`:
30
+
31
+ ```ruby
32
+ # Rakefile
33
+ require 'maintenance_mode/rake_tasks'
34
+ ```
35
+
36
+ ## Configuration
37
+
38
+ The persistence mechanism is configurable. Currently, the only one which ships with this gem just uses a file on disk.
39
+
40
+ Without configuration, the library will act on memory only (which of course means rake tasks have no effect on a server process). To configure the file-based persistence, use the following:
41
+
42
+ ```ruby
43
+ MaintenanceMode.persist_with :file, Rails.root.join('config/maintenance-mode')
44
+ # equivalent to
45
+ MaintenanceMode.persistence = MaintenanceMode::FilePersistence.new(
46
+ Rails.root.join('config/maintenance-mode'))
47
+ ```
48
+
49
+ n.b. The argument to `FilePersistence` must be a `Pathname` instead of merely a string.
50
+
51
+ ### Custom logic
52
+
53
+ You can set `MaintenanceMode.persistence` to any object which responds to the
54
+ correct interface.
55
+
56
+ - `enable(message=nil)`
57
+ - `disable`
58
+ - `enabled?`
59
+ - `message`
60
+
61
+ Here's an example using Redis for persistence (this is untested; just for example):
62
+
63
+ ```ruby
64
+ class MyRedisMaintenancePersistence
65
+ NULL = '__null__'
66
+
67
+ def initialize(redis, key)
68
+ @redis = redis
69
+ @key = key
70
+ end
71
+
72
+ def enable(message=NULL)
73
+ @redis.set @key, message
74
+ end
75
+
76
+ def disable
77
+ @redis.del @key
78
+ end
79
+
80
+ def enabled?
81
+ !!@redis.get(@key)
82
+ end
83
+
84
+ def message
85
+ v = @redis.get(@key)
86
+ v == NULL ? nil : v
87
+ end
88
+ end
89
+
90
+ r = MyRedisMaintenancePersistence.new(Redis.new, 'maintenance-mode')
91
+ MaintenanceMode.persistence = r
92
+
93
+ # If writing as a separately distributed library, you could have your code
94
+ # register itself:
95
+ MaintenanceMode.register_persistence_method :redis, MyRedisMaintenancePersistence
96
+
97
+ # then people can use it via:
98
+ MaintenanceMode.persist_with :redis, Redis.new, 'maintenance-mode'
99
+ ```
100
+
101
+ ## Contributing
102
+
103
+ Pull requests welcome.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.pattern = 'test/**/*_test.rb'
7
+ end
8
+
9
+ task default: :test
@@ -0,0 +1,48 @@
1
+ require 'forwardable'
2
+ require 'maintenance_mode/version'
3
+ require 'maintenance_mode/file_persistence'
4
+ require 'maintenance_mode/null_persistence'
5
+
6
+ class MaintenanceMode
7
+ class << self
8
+ extend Forwardable
9
+ def_delegator :persistence, :enable
10
+ def_delegator :persistence, :disable
11
+ def_delegator :persistence, :message
12
+ def_delegator :persistence, :enabled?
13
+
14
+ def as_json
15
+ {
16
+ enabled: enabled?,
17
+ message: message,
18
+ }
19
+ end
20
+
21
+ attr_writer :persistence
22
+
23
+ def persistence
24
+ @persistence || NullPersistence.new
25
+ end
26
+
27
+ def persist_with(persistence_method, *args, &block)
28
+ self.persistence = persistence_methods.fetch(persistence_method).new(*args, &block)
29
+ end
30
+
31
+ def register_persistence_method(name, persistence_method_class)
32
+ persistence_methods[name] = persistence_method_class
33
+ end
34
+
35
+ def reset
36
+ @persistence_methods = nil
37
+ @persistence = nil
38
+ end
39
+
40
+ private
41
+
42
+ def persistence_methods
43
+ @persistence_methods ||= {
44
+ file: FilePersistence
45
+ }
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,28 @@
1
+ class MaintenanceMode
2
+ class FilePersistence
3
+ def initialize(pathname)
4
+ @pathname = pathname
5
+ end
6
+
7
+ attr_reader :pathname
8
+
9
+ def enabled?
10
+ @pathname.file?
11
+ end
12
+
13
+ def enable(message='')
14
+ @pathname.write(message)
15
+ end
16
+
17
+ def disable
18
+ return unless enabled?
19
+ @pathname.unlink
20
+ end
21
+
22
+ def message
23
+ return unless enabled?
24
+ contents = @pathname.read.to_s
25
+ contents == '' ? nil : contents
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ class MaintenanceMode
2
+ class NullPersistence
3
+ def enabled?
4
+ false
5
+ end
6
+
7
+ def enable(message=nil); end
8
+
9
+ def disable; end
10
+
11
+ def message; end
12
+ end
13
+ end
@@ -0,0 +1,26 @@
1
+ require 'maintenance_mode'
2
+
3
+ class MaintenanceMode
4
+ class RakeTasks
5
+ extend Rake::DSL
6
+
7
+ def self.install
8
+ namespace :maintenance do
9
+ desc 'Enable MaintenanceMode - optional message can be passed as only argument or use $MESSAGE'
10
+ task :enable, [:message] => [:configure] do |_, args|
11
+ MaintenanceMode.enable(ENV.fetch('MESSAGE', args[:message]))
12
+ end
13
+
14
+ desc 'Disable MaintenanceMode'
15
+ task :disable => [:configure] do
16
+ MaintenanceMode.disable
17
+ end
18
+
19
+ desc 'Empty hook to configure MaintenanceMode before enable/disable'
20
+ task :configure
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ MaintenanceMode::RakeTasks.install
@@ -0,0 +1,3 @@
1
+ class MaintenanceMode
2
+ VERSION = '0.0.2'
3
+ end
File without changes
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'maintenance_mode/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "maintenance_mode"
8
+ spec.version = MaintenanceMode::VERSION
9
+ spec.authors = ["Ryan Taylor Long"]
10
+ spec.email = ["ryan.long@goodguide.com"]
11
+ spec.summary = "This provides a simple interface to a 'maintenance mode' toggle with optional maintenance message."
12
+ spec.homepage = "http://github.com/goodguide/maintenance_mode"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "minitest"
23
+ end
@@ -0,0 +1,79 @@
1
+ require 'test_helper'
2
+ require 'pathname'
3
+ require 'tempfile'
4
+
5
+ class MaintenanceMode::FilePersistenceTest < Minitest::Test
6
+ def _file
7
+ @tempfile ||= Tempfile.new('MaintenanceMode').tap { |f| f.close }
8
+ Pathname.new(@tempfile.path)
9
+ end
10
+
11
+ def setup
12
+ _file.unlink
13
+ end
14
+
15
+ def subject
16
+ MaintenanceMode::FilePersistence.new(_file)
17
+ end
18
+
19
+ def test_enable
20
+ assert !_file.file?, 'file should not exist'
21
+ subject.enable
22
+
23
+ assert _file.file?, 'file should exist'
24
+ end
25
+
26
+ def test_enable_with_message
27
+ assert !_file.file?, 'file should not exist'
28
+ subject.enable 'foo'
29
+
30
+ assert _file.file?, 'file should exist'
31
+ assert_equal 'foo', _file.read.to_s
32
+ end
33
+
34
+ def test_enable_when_enabled
35
+ assert !_file.file?, 'file should not exist'
36
+ subject.enable 'foo'
37
+ subject.enable
38
+
39
+ assert _file.file?, 'file should exist'
40
+ assert_equal '', _file.read.to_s
41
+ end
42
+
43
+ def test_disable
44
+ _file.write('foo')
45
+ assert _file.file?, 'file should exist'
46
+ subject.disable
47
+
48
+ assert !_file.file?, 'file should not exist'
49
+ end
50
+
51
+ def test_disable_when_not_enabled
52
+ subject.disable
53
+
54
+ assert !_file.file?, 'file should not exist'
55
+ end
56
+
57
+ def test_enabled?
58
+ _file.write('foo')
59
+ assert subject.enabled?
60
+ _file.unlink
61
+ assert !subject.enabled?
62
+ end
63
+
64
+ def test_message
65
+ _file.write('foobar')
66
+
67
+ assert_equal 'foobar', subject.message
68
+ end
69
+
70
+ def test_message_when_empty
71
+ _file.write('')
72
+
73
+ assert_equal nil, subject.message
74
+ end
75
+
76
+ def test_message_when_not_enabled
77
+ assert_equal nil, subject.message
78
+ end
79
+ end
@@ -0,0 +1,37 @@
1
+ require 'test_helper'
2
+
3
+ class MaintenanceMode::NullPersistenceTest < Minitest::Test
4
+ def subject
5
+ MaintenanceMode::NullPersistence.new
6
+ end
7
+
8
+ def test_enable
9
+ subject.enable
10
+ assert !subject.enabled?
11
+ end
12
+
13
+ def test_enable_with_message
14
+ subject.enable 'foo'
15
+ assert_nil subject.message
16
+ assert !subject.enabled?
17
+ end
18
+
19
+ def test_enable_when_enabled
20
+ subject.enable 'foo'
21
+ subject.enable
22
+
23
+ assert !subject.enabled?
24
+ end
25
+
26
+ def test_disable
27
+ subject.disable
28
+ end
29
+
30
+ def test_enabled?
31
+ assert !subject.enabled?
32
+ end
33
+
34
+ def test_message
35
+ assert_nil subject.message
36
+ end
37
+ end
@@ -0,0 +1,85 @@
1
+ class MaintenanceModeTest
2
+ class Delegation < Minitest::Test
3
+ def persistence_method_stub
4
+ @persistence_method_stub ||= Minitest::Mock.new
5
+ end
6
+
7
+ def setup
8
+ MaintenanceMode.persistence = persistence_method_stub
9
+ end
10
+
11
+ def teardown
12
+ @persistence_method_stub = nil
13
+ end
14
+
15
+ def test_enable
16
+ persistence_method_stub.expect :enable, nil
17
+ MaintenanceMode.enable
18
+
19
+ persistence_method_stub.verify
20
+ end
21
+
22
+ def test_enable_with_message
23
+ persistence_method_stub.expect :enable, nil, ['foo']
24
+ MaintenanceMode.enable 'foo'
25
+
26
+ persistence_method_stub.verify
27
+ end
28
+
29
+ def test_disable
30
+ persistence_method_stub.expect :disable, nil
31
+ MaintenanceMode.disable
32
+
33
+ persistence_method_stub.verify
34
+ end
35
+
36
+ def test_enabled?
37
+ persistence_method_stub.expect :enabled?, :foobar
38
+
39
+ assert_equal :foobar, MaintenanceMode.enabled?
40
+ persistence_method_stub.verify
41
+ end
42
+
43
+ def test_message
44
+ persistence_method_stub.expect :message, :foobarbaz
45
+
46
+ assert_equal :foobarbaz, MaintenanceMode.message
47
+ persistence_method_stub.verify
48
+ end
49
+
50
+ def test_as_json
51
+ persistence_method_stub.expect :message, 'foo'
52
+ persistence_method_stub.expect :enabled?, true
53
+
54
+ assert_equal({ enabled: true, message: 'foo' }, MaintenanceMode.as_json)
55
+ persistence_method_stub.verify
56
+ end
57
+ end
58
+
59
+ class PersistenceRegistration < Minitest::Test
60
+ def setup
61
+ MaintenanceMode.reset
62
+ end
63
+
64
+ def test_persistence_default
65
+ assert_kind_of MaintenanceMode::NullPersistence, MaintenanceMode.persistence
66
+ end
67
+
68
+ def test_persist_with
69
+ MaintenanceMode.persist_with :file, 'tmp/foo'
70
+ assert_kind_of MaintenanceMode::FilePersistence, MaintenanceMode.persistence
71
+ assert_equal 'tmp/foo', MaintenanceMode.persistence.pathname
72
+ end
73
+
74
+ def test_persist_with_not_registered
75
+ assert_raises(KeyError) { MaintenanceMode.persist_with :foo }
76
+ end
77
+
78
+ def test_register_persistence_method
79
+ fake_persistence_method_class = Class.new(Object)
80
+ MaintenanceMode.register_persistence_method :foo, fake_persistence_method_class
81
+ MaintenanceMode.persist_with :foo
82
+ assert_kind_of fake_persistence_method_class, MaintenanceMode.persistence
83
+ end
84
+ end
85
+ end
@@ -0,0 +1 @@
1
+ maintenance.enabled
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'maintenance_mode', path: '../../'
4
+ gem 'rake'
@@ -0,0 +1,16 @@
1
+ PATH
2
+ remote: ../../
3
+ specs:
4
+ maintenance_mode (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.4.2)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ maintenance_mode!
16
+ rake
@@ -0,0 +1,5 @@
1
+ require 'maintenance_mode/rake_tasks'
2
+
3
+ task 'maintenance:configure' do
4
+ require_relative './maintenance_mode_config'
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'json'
2
+ require 'maintenance_mode'
3
+ require_relative './maintenance_mode_config'
4
+
5
+ puts MaintenanceMode.as_json.to_json
@@ -0,0 +1,5 @@
1
+ require 'maintenance_mode'
2
+ require 'pathname'
3
+
4
+ path = Pathname.new(File.expand_path('../maintenance.enabled', __FILE__))
5
+ MaintenanceMode.persist_with :file, path
@@ -0,0 +1,4 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+
4
+ require 'maintenance_mode'
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maintenance_mode
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Taylor Long
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - ryan.long@goodguide.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/maintenance_mode.rb
68
+ - lib/maintenance_mode/file_persistence.rb
69
+ - lib/maintenance_mode/null_persistence.rb
70
+ - lib/maintenance_mode/rake_tasks.rb
71
+ - lib/maintenance_mode/version.rb
72
+ - lib/tasks/maintenance_mode.rake
73
+ - maintenance_mode.gemspec
74
+ - test/maintenance_mode/file_persistence_test.rb
75
+ - test/maintenance_mode/null_persistence_test.rb
76
+ - test/maintenance_mode_test.rb
77
+ - test/rake_task_test_env/.gitignore
78
+ - test/rake_task_test_env/Gemfile
79
+ - test/rake_task_test_env/Gemfile.lock
80
+ - test/rake_task_test_env/Rakefile
81
+ - test/rake_task_test_env/current_state.rb
82
+ - test/rake_task_test_env/maintenance_mode_config.rb
83
+ - test/test_helper.rb
84
+ homepage: http://github.com/goodguide/maintenance_mode
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.2.2
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: This provides a simple interface to a 'maintenance mode' toggle with optional
108
+ maintenance message.
109
+ test_files:
110
+ - test/maintenance_mode/file_persistence_test.rb
111
+ - test/maintenance_mode/null_persistence_test.rb
112
+ - test/maintenance_mode_test.rb
113
+ - test/rake_task_test_env/.gitignore
114
+ - test/rake_task_test_env/Gemfile
115
+ - test/rake_task_test_env/Gemfile.lock
116
+ - test/rake_task_test_env/Rakefile
117
+ - test/rake_task_test_env/current_state.rb
118
+ - test/rake_task_test_env/maintenance_mode_config.rb
119
+ - test/test_helper.rb
120
+ has_rdoc: