Skip to content

Commit

Permalink
Renamed rack-staging
Browse files Browse the repository at this point in the history
Added default template
Simplified options
  • Loading branch information
thibaudgg committed Aug 23, 2010
1 parent 8f00588 commit 59020a5
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 179 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -17,5 +17,6 @@ tmtags
coverage
rdoc
pkg
.bundle

## PROJECT::SPECIFIC
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source :rubygems

# Specify your gem's dependencies in rack-staging.gemspec
gemspec
15 changes: 15 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,15 @@
PATH
remote: .
specs:
rack-staging (0.1.0)

GEM
remote: http://rubygems.org/
specs:

PLATFORMS
ruby

DEPENDENCIES
bundler (>= 1.0.0.rc.5)
rack-staging!
33 changes: 7 additions & 26 deletions README.textile
Expand Up @@ -6,32 +6,13 @@ h1. Usage

h2. Examples

h3. In you Rails initilizer

<pre><code>config.middleware.use 'Rack::Staging',
:template_path => "#{RAILS_ROOT}/app/views/staging/index.html",
:auth_codes => [ 'Hfgo_JHg+47', 'dh_JHg_ra988', 'fd-m*nKJ_DE7'] # Basic setup</code></pre>

<pre><code>config.middleware.use 'Staging',
:session_key => :staging_auth, # Custom session key
:code_param => :staging_code", # Custom input name (<input name="staging_code" />), defult is :code
:template_path => "#{RAILS_ROOT}/app/views/staging/index.html",
:auth_codes => [ 'Hfgo_JHg+47', 'dh_JHg_ra988', 'fd-m*nKJ_DE7']</code></pre>

h3. In you staging template (“#{RAILS_ROOT}/app/views/staging/index.html”)

<pre><code><html>
<head>
<title>Staging application</title>
</head>
<body>
<h1>Please input staging code</h1>
<form action="/" method="post">
<input name="code" />
<input type="submit" />
</form>
</body>
</html></code></pre>
h3. In you Rails initializer

<pre><code>config.middleware.use 'Rack::Staging', :code => 'Hfgo_JHg+47'</code></pre>

<pre><code>config.middleware.use 'Rack::Staging',
:code => 'Hfgo_JHg+47',
:template_path => "#{Rails.root}/app/views/staging/index.html" # custom template</code></pre>

h3. … and now you are ready to protected staging!

58 changes: 2 additions & 56 deletions Rakefile
@@ -1,56 +1,2 @@
require 'rubygems'
require 'rake'

begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "staging"
gem.summary = %Q{Protect you Rails application from anonymous and prying.}
gem.description = %Q{Staging rack application purpose to protect you Rails application from anonymous and prying.}
gem.email = "kossnocorp@gmail.com"
gem.homepage = "http://github.com/kossnocorp/staging"
gem.authors = ["Aleksandr Koss"]
gem.version = '0.1'

#gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
end

require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.pattern = 'test/**/test_*.rb'
test.verbose = true
end

begin
require 'rcov/rcovtask'
Rcov::RcovTask.new do |test|
test.libs << 'test'
test.pattern = 'test/**/test_*.rb'
test.verbose = true
end
rescue LoadError
task :rcov do
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
end
end

task :test => :check_dependencies

task :default => :test

require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
version = File.exist?('VERSION') ? File.read('VERSION') : ""

rdoc.rdoc_dir = 'rdoc'
rdoc.title = "staging #{version}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end

require 'bundler'
Bundler::GemHelper.install_tasks
52 changes: 52 additions & 0 deletions lib/rack-staging.rb
@@ -0,0 +1,52 @@
# Hi! I'am rack middleware!
# I was born for protect you staging application from anonymous and prying

# My author's name was Aleksandr Koss. Mail him at kossnocorp@gmail.com
# Nice to MIT you!

module Rack
class Staging

def initialize(app, options = {})
@app = app
@options = options
end

def call(env)
request = Rack::Request.new(env)

# Check code in session and return Rails call if is valid
return @app.call(env) if already_auth?(request)

# If post method check :code_param value
if request.post? and code_valid?(request.params["staging_code"])
request.session[:staging_code] = request.params["staging_code"]
[301, {'Location' => '/'}, ''] # Redirect if code is valid
else
render_staging
end
end

private
# Render staging html file
def render_staging
[200, {'Content-Type' => 'text/html'}, [
::File.open(html_template, 'rb').read
]]
end

def html_template
@options[:template_path] || ::File.expand_path('../rack-staging/index.html', __FILE__)
end

# Validate code
def code_valid?(code)
@options[:code] == code
end

def already_auth?(request)
code_valid?(request.session[:staging_code])
end
end
end

15 changes: 15 additions & 0 deletions lib/rack-staging/index.html
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>Staging protection</title>
</head>
<body onLoad='document.forms[0].elements[0].focus();' style='text-align: center; margin-top:100px'>
<form action="/" method="post">
<label>
<h1>Staging code</h1>
<input name="staging_code" />
</label>
<input type="submit" />
</form>
</body>
</html>
5 changes: 5 additions & 0 deletions lib/rack-staging/version.rb
@@ -0,0 +1,5 @@
module Rack
class Staging
VERSION = "0.1.0"
end
end
56 changes: 0 additions & 56 deletions lib/staging.rb

This file was deleted.

22 changes: 22 additions & 0 deletions rack-staging.gemspec
@@ -0,0 +1,22 @@
# -*- encoding: utf-8 -*-
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
require 'rack-staging/version'

Gem::Specification.new do |s|
s.name = "rack-staging"
s.version = Rack::Staging::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Aleksandr Koss"]
s.email = ["kossnocorp@gmail.com"]
s.homepage = "http://github.com/kossnocorp/staging"
s.summary = "Protect you Rails application from anonymous and prying."
s.description = "Staging rack application purpose to protect you Rails application from anonymous and prying."

s.required_rubygems_version = ">= 1.3.6"
s.rubyforge_project = "rack-staging"

s.add_development_dependency "bundler", ">= 1.0.0.rc.5"

s.files = Dir.glob("{lib}/**/*") + %w[LICENSE]
s.require_path = 'lib'
end
41 changes: 0 additions & 41 deletions staging.gemspec

This file was deleted.

0 comments on commit 59020a5

Please sign in to comment.