rack-environmental 1.0.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.
Files changed (6) hide show
  1. data/CHANGELOG +10 -0
  2. data/LICENSE +21 -0
  3. data/README +44 -0
  4. data/Rakefile +25 -0
  5. data/lib/environmental.rb +113 -0
  6. metadata +68 -0
data/CHANGELOG ADDED
@@ -0,0 +1,10 @@
1
+ 1.0.0 (November 17, 2009)
2
+ * BUGFIX: The body was being returned as a string, which is not in accordance with the Rack spec.
3
+ * BUGFIX: The update content length algorithm was incorrect.
4
+ * BUGFIX: The first HTML response body kept rewriting subsequent requests.
5
+ * FEATURE: If the response is a Rails ActionController::Response object, we make sure we pass
6
+ it along.
7
+ * API CHANGE: Renamed the gem from environmental to rack-environmental.
8
+
9
+ 0.0.1 (November 14, 2009)
10
+ * 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
+
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ require 'rake'
2
+ require 'rake/gempackagetask'
3
+ require 'rubygems'
4
+
5
+ gem_spec = Gem::Specification.new do |s|
6
+ s.name = "rack-environmental"
7
+ s.version = "1.0.0"
8
+ s.add_dependency 'nokogiri', '>= 1.4.0'
9
+ s.author = "Wyatt Greene"
10
+ s.email = "techiferous@gmail.com"
11
+ s.summary = "Rack middleware that adds an indicator of your application environment"
12
+ s.description = %Q{
13
+ Rack::Environmental indicates which environment your web application is running
14
+ in (staging, test, etc.).
15
+ }
16
+ s.require_path = "lib"
17
+ s.files = ["lib/environmental.rb", "LICENSE", "Rakefile", "README",
18
+ "CHANGELOG"]
19
+ s.homepage = "http://github.com/techiferous/rack-environmental"
20
+ s.requirements << "none"
21
+ s.has_rdoc = false
22
+ end
23
+
24
+ Rake::GemPackageTask.new(gem_spec) do |pkg|
25
+ end
@@ -0,0 +1,113 @@
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
+ @doc = nil # clear out the doc object so we don't accidentally keep the last
13
+ # request's HTML around
14
+ @request = Rack::Request.new(env)
15
+ status, @headers, @body = @app.call(env)
16
+ if html?
17
+ add_to_top_of_web_page(create_sticker)
18
+ update_content_length
19
+ end
20
+ [status, @headers, @body]
21
+ end
22
+
23
+ private
24
+
25
+ def html?
26
+ @headers["Content-Type"] && @headers["Content-Type"].include?("text/html")
27
+ end
28
+
29
+ def add_to_top_of_web_page(node)
30
+ if node
31
+ doc.at_css("body").children.first.add_previous_sibling(node)
32
+ end
33
+ new_body_string = doc.to_html
34
+ # If we're dealing with a Rails response, we don't want to throw the
35
+ # response object away, we just want to update the response string.
36
+ if @body.class.name == "ActionController::Response"
37
+ @body.body = new_body_string
38
+ else
39
+ @body = [new_body_string]
40
+ end
41
+ end
42
+
43
+ def create_sticker
44
+ environment_name = environment(@request.url)
45
+ return nil if environment_name.nil?
46
+ div = Nokogiri::XML::Node.new("div", doc)
47
+ div['style'] = style(@options[environment_name])
48
+ div.content = environment_name.to_s
49
+ div
50
+ end
51
+
52
+ def environment(url)
53
+ url = url.split('//').last # remove http://
54
+ @options.each do |environment_name, options|
55
+ if options[:url] && options[:url] =~ url
56
+ return environment_name
57
+ end
58
+ end
59
+ return nil
60
+ end
61
+
62
+ def doc
63
+ @doc ||= Nokogiri::HTML(body_to_string)
64
+ end
65
+
66
+ def body_to_string
67
+ s = ""
68
+ @body.each { |x| s << x }
69
+ s
70
+ end
71
+
72
+ def style(options)
73
+ style = ""
74
+ style << "font-family: Verdana, Arial, sans-serif;"
75
+ style << "font-weight: bold;"
76
+ style << "text-transform: uppercase;"
77
+ style << "text-align: center;"
78
+ style << "color: black;"
79
+ style << "padding: 3px;"
80
+ style << "background-color: #{options[:color] || "blue"};"
81
+ case options[:size]
82
+ when :large
83
+ style << "font-size: 20px;"
84
+ when :medium
85
+ style << "font-size: 14px;"
86
+ when :small
87
+ style << "font-size: 10px;"
88
+ else
89
+ style << "font-size: 14px;"
90
+ end
91
+ if options[:style] == :badge
92
+ style << "margin: 5px;"
93
+ style << "position: fixed;"
94
+ style << "top: #{options[:top] || 5}px;"
95
+ style << "left: #{options[:left] || 5}px;"
96
+ style << "opacity: #{options[:opacity] || 0.7};"
97
+ style << "-moz-border-radius: 5px; -khtml-border-radius: 5px; -webkit-border-radius: 5px; border-radius: 5px;"
98
+ else
99
+ style << "margin: 0px;"
100
+ end
101
+ end
102
+
103
+ def update_content_length
104
+ length = 0
105
+ @body.each do |s| # we can't use inject because @body may not respond to it
106
+ length += Rack::Utils.bytesize(s) # we use Rack::Utils.bytesize to avoid
107
+ # incompatibilities between Ruby 1.8 and 1.9
108
+ end
109
+ @headers['Content-Length'] = length.to_s
110
+ end
111
+
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-environmental
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Wyatt Greene
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-17 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: nokogiri
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.4.0
24
+ version:
25
+ description: "\n Rack::Environmental indicates which environment your web application is running\n in (staging, test, etc.).\n "
26
+ email: techiferous@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - lib/environmental.rb
35
+ - LICENSE
36
+ - Rakefile
37
+ - README
38
+ - CHANGELOG
39
+ has_rdoc: true
40
+ homepage: http://github.com/techiferous/rack-environmental
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements:
61
+ - none
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.5
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Rack middleware that adds an indicator of your application environment
67
+ test_files: []
68
+