environmental 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/CHANGELOG +2 -0
  2. data/LICENSE +21 -0
  3. data/README +44 -0
  4. data/Rakefile +24 -0
  5. data/lib/environmental.rb +99 -0
  6. metadata +59 -0
@@ -0,0 +1,2 @@
1
+ 0.0.1 (November 14, 2009)
2
+ * Initial release.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2009 Wyatt M. Greene
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 ADDED
@@ -0,0 +1,44 @@
1
+ = Rack::Environmental
2
+
3
+ == Description
4
+
5
+ Never again will you accidentally delete production data! Rack::Environmental adds
6
+ an indicator badge to your web application so that you can tell whether you're
7
+ working with the staging, test, development or production version of your web app.
8
+
9
+ == Usage
10
+
11
+ This Rack middleware can be used with any Rack application, but here's how you would
12
+ use it in Rails. Put the following in config/environment.rb:
13
+
14
+ config.middleware.use Rack::Environmental,
15
+ :staging => { :url => /^staging.+$/ },
16
+ :test => { :url => /^test.+$/ },
17
+ :development => { :url => /^localhost.+$/ }
18
+
19
+ When a request comes to your web app, Rack::Environmental compares the URL to the supplied
20
+ regular expressions. If the URL matches, the name of the environment is displayed at the
21
+ top of the web page.
22
+
23
+ Each environment can be further configured:
24
+
25
+ config.middleware.use Rack::Environmental,
26
+ :staging => { :url => /^staging.+$/,
27
+ :color => "yellow",
28
+ :size => :large },
29
+ :test => { :url => /^test.+$/,
30
+ :color => "purple",
31
+ :style => :badge },
32
+ :development => { :url => /^localhost.+$/,
33
+ :color => "orange" }
34
+
35
+ Here's the full list of configuration options:
36
+
37
+ :url => a regular expression
38
+ :style => either :badge (a transparent, floating badge) or :banner (default)
39
+ :color => a string that represents a CSS color, such as "red", "rgb(6,70,14)", or "#8e6630"
40
+ :size => :small, :medium, or :large; defaults to :medium
41
+ :opacity => a number from 0 (completely transparent) to 1; only works with the badge style
42
+ :top => distance in pixels from the top; only works with the badge style
43
+ :left => distance in pixels from the left; only works with the badge style
44
+
@@ -0,0 +1,24 @@
1
+ require 'rake'
2
+ require 'rake/gempackagetask'
3
+ require 'rubygems'
4
+
5
+ gem_spec = Gem::Specification.new do |s|
6
+ s.name = "environmental"
7
+ s.version = "0.0.1"
8
+ s.author = "Wyatt Greene"
9
+ s.email = "techiferous@gmail.com"
10
+ s.summary = "Rack middleware that adds an indicator of your application environment"
11
+ s.description = %Q{
12
+ Rack::Environmental indicates which environment your web application is running
13
+ in (staging, test, etc.).
14
+ }
15
+ s.require_path = "lib"
16
+ s.files = ["lib/environmental.rb", "LICENSE", "Rakefile", "README",
17
+ "CHANGELOG"]
18
+ s.homepage = "http://github.com/techiferous/environmental"
19
+ s.requirements << "none"
20
+ s.has_rdoc = false
21
+ end
22
+
23
+ Rake::GemPackageTask.new(gem_spec) do |pkg|
24
+ end
@@ -0,0 +1,99 @@
1
+ require 'nokogiri'
2
+
3
+ module Rack
4
+ class Environmental
5
+
6
+ def initialize(app, options = {})
7
+ @app = app
8
+ @options = options
9
+ end
10
+
11
+ def call(env)
12
+ @request = Rack::Request.new(env)
13
+ status, @headers, @body = @app.call(env)
14
+ if html?
15
+ @body = add_to_top_of_web_page(create_sticker)
16
+ update_content_length
17
+ end
18
+ [status, @headers, @body]
19
+ end
20
+
21
+ private
22
+
23
+ def html?
24
+ @headers["Content-Type"] && @headers["Content-Type"].include?("text/html")
25
+ end
26
+
27
+ def add_to_top_of_web_page(node)
28
+ if node
29
+ doc.at_css("body").children.first.add_previous_sibling(node)
30
+ end
31
+ doc.to_html
32
+ end
33
+
34
+ def create_sticker
35
+ environment_name = environment(@request.url)
36
+ return nil if environment_name.nil?
37
+ div = Nokogiri::XML::Node.new("div", doc)
38
+ div['style'] = style(@options[environment_name])
39
+ div.content = environment_name.to_s
40
+ div
41
+ end
42
+
43
+ def environment(url)
44
+ url = url.split('//').last # remove http://
45
+ @options.each do |environment_name, options|
46
+ if options[:url] && options[:url] =~ url
47
+ return environment_name
48
+ end
49
+ end
50
+ return nil
51
+ end
52
+
53
+ def doc
54
+ @doc ||= Nokogiri::HTML(body_to_string)
55
+ end
56
+
57
+ def body_to_string
58
+ s = ""
59
+ @body.each { |x| s << x }
60
+ s
61
+ end
62
+
63
+ def style(options)
64
+ style = ""
65
+ style << "font-family: Verdana, Arial, sans-serif;"
66
+ style << "font-weight: bold;"
67
+ style << "text-transform: uppercase;"
68
+ style << "text-align: center;"
69
+ style << "color: black;"
70
+ style << "padding: 3px;"
71
+ style << "background-color: #{options[:color] || "blue"};"
72
+ case options[:size]
73
+ when :large
74
+ style << "font-size: 20px;"
75
+ when :medium
76
+ style << "font-size: 14px;"
77
+ when :small
78
+ style << "font-size: 10px;"
79
+ else
80
+ style << "font-size: 14px;"
81
+ end
82
+ if options[:style] == :badge
83
+ style << "margin: 5px;"
84
+ style << "position: fixed;"
85
+ style << "top: #{options[:top] || 5}px;"
86
+ style << "left: #{options[:left] || 5}px;"
87
+ style << "opacity: #{options[:opacity] || 0.7};"
88
+ style << "-moz-border-radius: 5px; -khtml-border-radius: 5px; -webkit-border-radius: 5px; border-radius: 5px;"
89
+ else
90
+ style << "margin: 0px;"
91
+ end
92
+ end
93
+
94
+ def update_content_length
95
+ @headers['Content-Length'] = Rack::Utils.bytesize(@body).to_s
96
+ end
97
+
98
+ end
99
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: environmental
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Wyatt Greene
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-14 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: "\n Rack::Environmental indicates which environment your web application is running\n in (staging, test, etc.).\n "
17
+ email: techiferous@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/environmental.rb
26
+ - LICENSE
27
+ - Rakefile
28
+ - README
29
+ - CHANGELOG
30
+ has_rdoc: true
31
+ homepage: http://github.com/techiferous/environmental
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements:
52
+ - none
53
+ rubyforge_project:
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Rack middleware that adds an indicator of your application environment
58
+ test_files: []
59
+