rack-env-notifier 0.0.1

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: 896b4bc98af31b67e3fbf0cb357d67f660335301
4
+ data.tar.gz: 0a5b125d74a33fb039ac2a95f0e685673ce99209
5
+ SHA512:
6
+ metadata.gz: bf559b169d4e91640aa67eacf0f465f9d1e78dc47f2743bb12c1ca1febb44d48d81f9a35163e27be3c7377df04a76b74a2c1f5e4ac1b4b313c56f8a1503772f0
7
+ data.tar.gz: 54fa2c7e78e82d3176f37e63de8cd9b625b9f5b8631dff93beed6e9103f38ac777937237a6cc9b315d049c1ad4c7df7d851dd220c707fc5e6429fc3e995016c9
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format nested
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,2 @@
1
+ rvm:
2
+ - 2.0.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in env_status.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rake'
8
+ gem 'rack'
9
+ gem 'rspec'
10
+ gem 'ZenTest'
11
+ gem 'autotest'
12
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Catalin Ilinca
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,94 @@
1
+ # Rack::EnvNotifier [![Build Status](https://secure.travis-ci.org/ducknorris/rack-env-notifier.png)](http://travis-ci.org/ducknorris/rack-env-notifier) [![Code Climate](https://codeclimate.com/github/ducknorris/rack-env-notifier.png)](https://codeclimate.com/github/ducknorris/rack-env-notifier)
2
+
3
+ Middleware that displays the custom environment notification for every html page. Designed to work both in production and in development.
4
+
5
+ ![alt tag](https://raw.github.com/ducknorris/env_notifier/master/assets/preview1.png)
6
+
7
+ ![alt tag](https://raw.github.com/ducknorris/env_notifier/master/assets/preview2.png)
8
+
9
+ ![alt tag](https://raw.github.com/ducknorris/env_notifier/master/assets/preview3.png)
10
+
11
+ ![alt tag](https://raw.github.com/ducknorris/env_notifier/master/assets/preview4.png)
12
+
13
+ ![alt tag](https://raw.github.com/ducknorris/env_notifier/master/assets/preview5.png)
14
+
15
+ ![alt tag](https://raw.github.com/ducknorris/env_notifier/master/assets/preview6.png)
16
+
17
+ ![alt tag](https://raw.github.com/ducknorris/env_notifier/master/assets/preview7.png)
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ gem 'env_notifier'
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install env_notifier
32
+
33
+ ## Usage
34
+
35
+ This Gem will display a custom notification on every html page. By default it will only work on Development Environment. This can be overriden like so:
36
+
37
+ Rack::EnvNotifier.notify = Rails.env.production?
38
+
39
+ or
40
+
41
+ Rack::EnvNotifier.notify = true if ["12.34.56.78", "127.0.0.1"].include?(request.remote_ip)
42
+
43
+ ### There are several configuration options
44
+
45
+ #### Position on screen
46
+
47
+ Even though the "default" position is at the top of the page, the notification uses ``#env-notifer`` CSS ID. This can be customized further on, using custom CSS.
48
+
49
+ #env-notifier {
50
+ font-size: 16px;
51
+ }
52
+
53
+ /* Rack::EnvNotifier.message = 'development' */
54
+
55
+ #env-notifier.development {
56
+ background: #369;
57
+ color: #fff;
58
+ }
59
+
60
+ /* Rack::EnvNotifier.message = 'watch out! production environment!!!' */
61
+
62
+ #env-notifier.watch-out-production-environment {
63
+ background: #8a0000;
64
+ color: #fff;
65
+ font-weight: bold;
66
+ }
67
+
68
+
69
+ #### Notification message
70
+
71
+ Rack::EnvNotifier.message = "development mode"
72
+
73
+ For Ruby on Rails, by default the message it will be the name of the current environment. This can be overriden like so:
74
+
75
+ case Rails.env
76
+ when "development"
77
+ Rack::EnvNotifier.message = "safe environment"
78
+ when "staging"
79
+ Rack::EnvNotifier.message = "qa environment"
80
+ else
81
+ Rack::EnvNotifier.message = "hot environment"
82
+ end
83
+
84
+ ## Contributing
85
+
86
+ Thanks to our [contributors](https://github.com/ducknorris/env_notifier/graphs/contributors).
87
+
88
+ 1. Fork it
89
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
90
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
91
+ 4. Push to the branch (`git push origin my-new-feature`)
92
+ 5. Create new Pull Request
93
+
94
+ #### Inspired by [harleyttd / miniprofiler](https://github.com/harleyttd/miniprofiler/)
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
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,11 @@
1
+ module EnvNotifierRails
2
+ class Railtie < ::Rails::Railtie
3
+ initializer 'rack_env_notifier.initialize' do |app|
4
+ Rack::EnvNotifier.message = 'development'
5
+ Rack::EnvNotifier.notify = Rails.env.development?
6
+
7
+ # Install the Middleware
8
+ app.config.middleware.use Rack::EnvNotifier
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,38 @@
1
+ module Rack
2
+ class EnvNotifier
3
+ class BodyInjector
4
+ BODY_TAG_REGEX = /<body>|<body[^(er)][^<]*>/
5
+
6
+ attr_reader :content_length, :new_body, :notification_added
7
+
8
+ def initialize(body, text_to_be_injected)
9
+ @body = body
10
+ @text_to_be_injected = text_to_be_injected
11
+ end
12
+
13
+ def inject!(env)
14
+ @env = env
15
+ @body.close if @body.respond_to?(:close)
16
+
17
+ @body = [@body] if @body.is_a? String
18
+
19
+ @new_body = []
20
+ @body.each { |line| @new_body << line.to_s }
21
+
22
+ @content_length = 0
23
+ @notification_added = false
24
+
25
+ @new_body.each do |line|
26
+ if !@notification_added && line['<body']
27
+ line.gsub! (BODY_TAG_REGEX) {|match| %{#{match}\n#{@text_to_be_injected}} }
28
+
29
+ @notification_added = true
30
+ end
31
+
32
+ @content_length += line.bytesize
33
+ end
34
+ @new_body = @body
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,64 @@
1
+ require 'rack/env_notifier/body_injector'
2
+
3
+ module Rack
4
+ class EnvNotifier
5
+ class << self
6
+ def message
7
+ @message
8
+ end
9
+
10
+ def message=(message)
11
+ @message = message
12
+ end
13
+
14
+ def notification
15
+ <<-EOF
16
+ <!-- Notify Start -->
17
+ <div id="env-notifier" class="#{@message.gsub(/[^a-z]/i, '-').gsub(/--*/, '-').gsub(/-$/, '')}" style="position: fixed; top: 0; right: 0; left: 0; background: rgba(150, 50, 50, .7); color: #fff; text-align: center; font-size: 16px; font-weight: bold; padding: 2px; z-index: 999999">#{@message}</div>
18
+ <!-- Notify End -->
19
+ EOF
20
+ end
21
+
22
+ def notify?
23
+ @notify
24
+ end
25
+
26
+ def notify=(notify)
27
+ @notify = notify
28
+ end
29
+ end
30
+
31
+ def initialize(app)
32
+ @app = app
33
+ end
34
+
35
+ def call(env)
36
+ dup._call(env)
37
+ end
38
+
39
+ def _call(env)
40
+ status, headers, body = @app.call(env)
41
+
42
+ # inject headers, notification
43
+ if status == 200 and EnvNotifier.notify?
44
+
45
+ # inject notification
46
+ if headers['Content-Type'] =~ %r{text/html} then
47
+ injector = BodyInjector.new(body, EnvNotifier.notification)
48
+ injector.inject!(env)
49
+
50
+ # inject header
51
+ if injector.notification_added
52
+ headers['X-EnvNotifier'] = EnvNotifier.message
53
+ end
54
+
55
+ headers['Content-Length'] = injector.content_length.to_s
56
+ [status, headers, injector.new_body]
57
+ end
58
+ [status, headers, body]
59
+ else
60
+ [status, headers, body]
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,6 @@
1
+ require 'rack/env_notifier'
2
+ require 'rails/railtie' if defined? ::Rails
3
+
4
+ class Rack::EnvNotifier
5
+ VERSION = "0.0.1"
6
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack-env-notifier'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-env-notifier"
8
+ spec.version = Rack::EnvNotifier::VERSION
9
+ spec.authors = ["Catalin Ilinca"]
10
+ spec.email = ["c@talin.ro"]
11
+ spec.description = %q{Middleware that displays the current Environment notification for every html page. Designed to work both in production and in development.}
12
+ spec.summary = %q{Know your ground!}
13
+ spec.homepage = "https://github.com/ducknorris/rack-env-notifier"
14
+ spec.license = "MIT"
15
+ spec.extra_rdoc_files = [
16
+ "README.md",
17
+ "CHANGELOG.md"
18
+ ]
19
+
20
+ spec.files = `git ls-files`.split($/)
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+
29
+ spec.add_development_dependency "rack-test"
30
+
31
+ spec.add_runtime_dependency 'rack', '>= 1.1.3'
32
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::EnvNotifier::BodyInjector do
4
+ describe 'body tag regex' do
5
+ let(:regex) { described_class::BODY_TAG_REGEX }
6
+ subject { regex }
7
+
8
+ it { should be_kind_of(Regexp) }
9
+
10
+ it 'only picks a valid <body> tag' do
11
+ regex.match("<body></body>").to_s.should eq('<body>')
12
+ regex.match("<body><h1></h1></body>").to_s.should eq('<body>')
13
+ regex.match("<body attribute='something'><h1></h1></body>").to_s.should eq("<body attribute='something'>")
14
+ end
15
+
16
+ it 'responds false when no head tag' do
17
+ regex.match("<html></html>").should be_false
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::EnvNotifier do
4
+ include Rack::Test::Methods
5
+
6
+ def app
7
+ @app ||= Rack::Builder.new {
8
+ use Rack::EnvNotifier
9
+ map '/some/path' do
10
+ run lambda { |env| [200, {'Content-Type' => 'text/html'}, <<-EOF
11
+ <html>
12
+ <head>
13
+ <title>Test page</title>
14
+ </head>
15
+ <body>
16
+ <h1>Howdy</h1>
17
+ </body>
18
+ </html>
19
+ EOF
20
+ ]}
21
+ end
22
+ }.to_app
23
+ end
24
+
25
+ describe 'with a notify set on true' do
26
+
27
+ before do
28
+ Rack::EnvNotifier.message = 'warning!!! hot zone!!!'
29
+ Rack::EnvNotifier.notify = true
30
+ get '/some/path'
31
+ end
32
+
33
+ it 'returns 200' do
34
+ last_response.should be_ok
35
+ end
36
+
37
+ it 'has the X-EnvNotifier header' do
38
+ last_response.headers.has_key?('X-EnvNotifier').should be_true
39
+ end
40
+
41
+ it 'has only one X-EnvNotifier header' do
42
+ h = last_response.headers['X-EnvNotifier']
43
+ h.should eq('warning!!! hot zone!!!')
44
+ end
45
+
46
+ it 'has the Notification in the body' do
47
+ last_response.body.include?('<div id="env-notifier" class="warning-hot-zone" style="position: fixed; top: 0; right: 0; left: 0; background: rgba(150, 50, 50, .7); color: #fff; text-align: center; font-size: 16px; font-weight: bold; padding: 2px; z-index: 999999">warning!!! hot zone!!!</div>').should be_true
48
+ end
49
+
50
+ end
51
+
52
+ describe 'with a notify set on false' do
53
+
54
+ before do
55
+ Rack::EnvNotifier.message = 'warning!!!'
56
+ Rack::EnvNotifier.notify = false
57
+ get '/some/path'
58
+ end
59
+
60
+ it 'returns 200' do
61
+ last_response.should be_ok
62
+ end
63
+
64
+ it 'has the X-EnvNotifier header' do
65
+ last_response.headers.has_key?('X-EnvNotifier').should_not be_true
66
+ end
67
+
68
+ it 'has the Notification in the body' do
69
+ last_response.body.include?('<div id="env-notifier" class="warning" style="position: fixed; top: 0; right: 0; left: 0; background: rgba(150, 50, 50, .7); color: #fff; text-align: center; font-size: 16px; font-weight: bold; padding: 2px; z-index: 999999">warning</div>').should_not be_true
70
+ end
71
+
72
+ end
73
+
74
+ describe 'Notification format' do
75
+ before do
76
+ Rack::EnvNotifier.message = 'notification'
77
+ get '/some/path'
78
+ end
79
+
80
+ it "does format the Notification" do
81
+ Rack::EnvNotifier.notification.should eq(<<-EOF
82
+ <!-- Notify Start -->
83
+ <div id="env-notifier" class="notification" style="position: fixed; top: 0; right: 0; left: 0; background: rgba(150, 50, 50, .7); color: #fff; text-align: center; font-size: 16px; font-weight: bold; padding: 2px; z-index: 999999">notification</div>
84
+ <!-- Notify End -->
85
+ EOF
86
+ )
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,2 @@
1
+ require 'rack-env-notifier'
2
+ require 'rack/test'
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-env-notifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Catalin Ilinca
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-06 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '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
+ - !ruby/object:Gem::Dependency
56
+ name: rack-test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 1.1.3
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 1.1.3
83
+ description: Middleware that displays the current Environment notification for every
84
+ html page. Designed to work both in production and in development.
85
+ email:
86
+ - c@talin.ro
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files:
90
+ - README.md
91
+ - CHANGELOG.md
92
+ files:
93
+ - .gitignore
94
+ - .rspec
95
+ - .travis.yml
96
+ - CHANGELOG.md
97
+ - Gemfile
98
+ - LICENSE.txt
99
+ - README.md
100
+ - Rakefile
101
+ - assets/preview1.png
102
+ - assets/preview2.png
103
+ - assets/preview3.png
104
+ - assets/preview4.png
105
+ - assets/preview5.png
106
+ - assets/preview6.png
107
+ - assets/preview7.png
108
+ - lib/env_notifier_rails/railtie.rb
109
+ - lib/rack-env-notifier.rb
110
+ - lib/rack/env_notifier.rb
111
+ - lib/rack/env_notifier/body_injector.rb
112
+ - rack-env-notifier.gemspec
113
+ - spec/rack/body_injector_spec.rb
114
+ - spec/rack/env_notifier_spec.rb
115
+ - spec/spec_helper.rb
116
+ homepage: https://github.com/ducknorris/rack-env-notifier
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.0.7
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Know your ground!
140
+ test_files:
141
+ - spec/rack/body_injector_spec.rb
142
+ - spec/rack/env_notifier_spec.rb
143
+ - spec/spec_helper.rb