safety_cone 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cab14d4acefb71708d80cb908f282070cad9e383
4
+ data.tar.gz: 4a74b048ef23b6a107b3b6b51569f8e43d1d57b3
5
+ SHA512:
6
+ metadata.gz: 88740dc9991f8a65131ff701fe7a9e756942cd1fb883e1c7a1085bc63e47cb5d2990627d8259e904a42aae8eb85cbeb0e097e1234d9745df9e573c28d1324ad0
7
+ data.tar.gz: 7484bad29aa9c4e47f0bdd701ac44988cabff87e5ed2f8cd72d6c59842e0757cc868ee8e8f2909176437178a5b5a930c59ed85bc2e344f431f7568dac3d1265d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at edwin@boost.co.nz. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in safety_cone.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Edwin Rozario
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # SafetyCone
2
+
3
+ Safety Cone is a Rails gem that lets an application to temporarily warn/block requests to pages in case of maintenance. Safety Cone allows the application raise warnings or custom messages which can be configured from the application initializer.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'safety_cone'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install safety_cone
20
+
21
+ ## Usage
22
+
23
+ Create a safety_con.rb file in config/initializer/
24
+
25
+ ```
26
+ SafetyCone.configure do |config|
27
+ # disables safety_cone. By default this value is true
28
+ config.enable = false
29
+
30
+ # This config will block all POST requests and display this message.
31
+ config.add(
32
+ method: :POST,
33
+ message: 'We are unable to write any data to database now.',
34
+ measure: :block
35
+ )
36
+
37
+ # This is a controller action specific warning. But with no measures to prevent this action
38
+ config.add(
39
+ controller: :users,
40
+ action: :new,
41
+ message: 'We are unable to register any user now. Please try after sometime.'
42
+ measure: :notice
43
+ )
44
+
45
+ # This is a controller action specific block. This config will let the application
46
+ # to raise an alert message and block the request from hitting the controller action.
47
+ config.add(
48
+ controller: 'users',
49
+ action: 'create',
50
+ message: 'We are unable to register any user now. Please try after sometime.',
51
+ measure: :block
52
+ )
53
+
54
+ # This is a controller action specific block with a redirect configured.
55
+ config.add(
56
+ controller: 'users',
57
+ action: 'create',
58
+ message: 'We are unable to register any user now. Please try after sometime.',
59
+ measure: :block,
60
+ redirect: '/page/more_info'
61
+ )
62
+
63
+ # For :block flash message is an alert
64
+ # For :notice flash message is a notice
65
+ end
66
+ ```
67
+
68
+ SafetyCone uses flash messages. So its expected that flash messages are rendered in view for all pages.
69
+
70
+ <Development>
71
+
72
+ <After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.>
73
+
74
+ <To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).>
75
+
76
+ <Contributing>
77
+
78
+ <Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/safety_cone. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.>
79
+
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT)
84
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "safety_cone"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,62 @@
1
+ module SafetyCone
2
+ # Module for configuring safety measures
3
+ module Configuration
4
+ VALID_MEASURES = [
5
+ :block, :warn,
6
+ :notice, :alert
7
+ ].freeze
8
+
9
+ VALID_OPTION_KEYS = [
10
+ :method, :controller,
11
+ :action, :message,
12
+ :measure, :redirect
13
+ ].freeze
14
+
15
+ attr_accessor :enabled, :cones, :options
16
+
17
+ # Method add a safety measure
18
+ def add(options = {})
19
+ self.options = options
20
+ valid?
21
+ cones[make_key] = options
22
+ end
23
+
24
+ # Method to generate a key from the options
25
+ def make_key
26
+ if options.key? :method
27
+ options[:method].to_sym
28
+ elsif options.include?(:controller) && options.include?(:action)
29
+ "#{options[:controller]}_#{options[:action]}".to_sym
30
+ else
31
+ raise(ArgumentError,
32
+ 'Options should contain :controller and :action or :method.')
33
+ end
34
+ end
35
+
36
+ # Checks the validity of configuration params
37
+ def valid?
38
+ invalid_options = (options.keys - VALID_OPTION_KEYS)
39
+ unless invalid_options.empty?
40
+ raise ArgumentError, "Options #{invalid_options} are not valid."
41
+ end
42
+
43
+ unless options.key? :message
44
+ raise ArgumentError, 'Option :message is mandatory.'
45
+ end
46
+
47
+ if options[:measure]
48
+ unless VALID_MEASURES.include? options[:measure]
49
+ raise(ArgumentError,
50
+ "Option #{options[:measure]} is not a valid :measure")
51
+ end
52
+ end
53
+ end
54
+
55
+ # Configuration method for Rails initializer
56
+ def configure
57
+ self.enabled = true
58
+ self.cones = {}
59
+ yield self
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,49 @@
1
+ module SafetyCone
2
+ # Module for Filtering requests and raise notices
3
+ # and take measures
4
+ module Filter
5
+ # Method to include to base class.
6
+ # This lets declare a before filter here
7
+ def self.included(base)
8
+ base.before_filter :safety_cone_filter
9
+ end
10
+
11
+ # Filter method that does the SafetyCone action
12
+ # based on the configuration.
13
+ def safety_cone_filter
14
+ return unless SafetyCone.enabled
15
+
16
+ if cone = fetch_cone
17
+ flash.clear
18
+ flash[notice_type(cone[:measure])] = cone[:message]
19
+ redirect_to safety_redirect(cone[:redirect]) if cone[:measure] == :block
20
+ end
21
+ end
22
+
23
+ # Fetches a configuration based on current request
24
+ def fetch_cone
25
+ cones = SafetyCone.cones
26
+ cones[request_method] || cones[request_action]
27
+ end
28
+
29
+ # Method to redirect a request
30
+ def safety_redirect(path)
31
+ path ? path : request.env['HTTP_REFERER']
32
+ end
33
+
34
+ # Returns type of notice
35
+ def notice_type(measure)
36
+ measure == :notice ? :notice : :alert
37
+ end
38
+
39
+ # Returns the current request action as a symbol
40
+ def request_method
41
+ request.method.to_sym
42
+ end
43
+
44
+ # Returns the current controller_action as a symbol
45
+ def request_action
46
+ "#{controller_name}_#{action_name}".to_sym
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module SafetyCone
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,8 @@
1
+ require 'safety_cone/version'
2
+ require 'safety_cone/configuration'
3
+ require 'safety_cone/filter'
4
+
5
+ # SafetyCone
6
+ module SafetyCone
7
+ extend Configuration
8
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'safety_cone/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'safety_cone'
8
+ spec.version = SafetyCone::VERSION
9
+ spec.authors = ['Edwin Rozario']
10
+ spec.email = ['edwin@boost.co.nz']
11
+
12
+ spec.summary = 'Blocks or warns requests as per configuration'
13
+ spec.description = 'At times we would want to block certain requests.
14
+ SafetyCone allows this to be configued.'
15
+ spec.homepage = 'https://github.com/boost/safety_cone'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.12'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec', '~> 3.0'
26
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: safety_cone
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Edwin Rozario
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-23 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: |-
56
+ At times we would want to block certain requests.
57
+ SafetyCone allows this to be configued.
58
+ email:
59
+ - edwin@boost.co.nz
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/safety_cone.rb
75
+ - lib/safety_cone/configuration.rb
76
+ - lib/safety_cone/filter.rb
77
+ - lib/safety_cone/version.rb
78
+ - safety_cone.gemspec
79
+ homepage: https://github.com/boost/safety_cone
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.5.1
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Blocks or warns requests as per configuration
103
+ test_files: []