rack-xrevision 0.1

Sign up to get free protection for your applications and to get access to all the features.
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 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in xrevision.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Michal Wrobel
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,46 @@
1
+ # rack-xrevision
2
+
3
+ Rack middleware, that adds X-Rev header with revision id (git commit id)
4
+
5
+ By default it looks for REVISON file that capistrano adds during deploy,
6
+ if it doesn't find it, then it tries to run git, and get the latest commit
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'rack-xrevision'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install rack-xrevision
21
+
22
+ ## Usage
23
+
24
+ in config/application.rb
25
+
26
+ config.middleware.use Rack::Xrevision, :app_path => Rails.root
27
+
28
+
29
+ in config.ru
30
+
31
+ use Rack::Xrevision, :app_path => Dir.pwd
32
+
33
+
34
+ ### Options:
35
+
36
+ :app_path - Specify app directory, by default it's Dir.pwd
37
+
38
+ :file_name - File with revision number, default is 'REVISION'
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ require 'rack/xrevision'
@@ -0,0 +1,41 @@
1
+ module Rack
2
+ class Xrevision
3
+ REVISION_FILE_NAME = 'REVISION'
4
+ def initialize(app, options={})
5
+ @app = app
6
+ @app_path = options[:app_path] || Dir.pwd
7
+ @revision_file_name = options[:file_name] || REVISION_FILE_NAME
8
+ end
9
+
10
+ def call(env)
11
+ status, headers, response = @app.call(env)
12
+ headers['X-Rev'] = rev_id if rev_id
13
+ [status, headers, response]
14
+ end
15
+
16
+ def rev_id
17
+ @rev_id ||= begin
18
+ load_from_file || load_from_git
19
+ end
20
+ end
21
+
22
+ def revision_file_path
23
+ "#{@app_path}/#{@revision_file_name}"
24
+ end
25
+
26
+ private
27
+ def load_from_file
28
+ return nil unless ::File.exists?(revision_file_path)
29
+
30
+ ::File.read(revision_file_path)
31
+ end
32
+
33
+ def load_from_git
34
+ return '' unless system('which git 1> /dev/null')
35
+
36
+ Dir.chdir @app_path do
37
+ %x[(git rev-list master | head -n 1) 2> /dev/null]
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class Xrevision
3
+ VERSION = '0.1'
4
+ end
5
+ end
@@ -0,0 +1,81 @@
1
+ require 'fileutils'
2
+ require 'rack/builder'
3
+ require 'rack/mock'
4
+ require_relative '../lib/rack-xrevision'
5
+
6
+ describe Rack::Xrevision do
7
+ let(:app_path) { "/tmp/test_app_#{Time.now.to_i}" }
8
+ let(:app) {
9
+ lambda { |env| [200, {}, ['hello, this is dog']] }
10
+ }
11
+
12
+ before do
13
+ FileUtils.mkdir app_path
14
+ end
15
+
16
+ after do
17
+ FileUtils.remove_dir app_path
18
+ end
19
+
20
+ let(:env) { Hash.new }
21
+
22
+ it 'checks if X-Rev header is present' do
23
+ path = app_path
24
+ app = Rack::Builder.new do
25
+ use Rack::Xrevision, :app_path => path
26
+ run lambda { |env|
27
+ [
28
+ 200,
29
+ { 'Content-Type' => 'text/html' },
30
+ ["<html><head></head><body>Hello, yes this is dog</body></html>"]
31
+ ]
32
+ }
33
+ end
34
+ response = Rack::MockRequest.new(app).get('/')
35
+
36
+ response.headers.should include('X-Rev')
37
+ end
38
+
39
+ context 'when there is a REVISION file' do
40
+ let(:xrevision) { Rack::Xrevision.new(app, :app_path => app_path) }
41
+ let(:rev_id) { "ff9429886dd39e4c765881673de9ca8c5bdb6f84" }
42
+ let(:revision_file_path) { "#{app_path}/REVISION" }
43
+
44
+ before do
45
+ File.open(revision_file_path, 'w') do |file|
46
+ file.write rev_id
47
+ end
48
+ end
49
+
50
+ it 'adds x-commit-id header to response' do
51
+ status, headers, body = xrevision.call(env)
52
+ headers['X-Rev'].should_not be_nil
53
+ headers['X-Rev'].should == rev_id
54
+ end
55
+ end
56
+
57
+ context 'when there is no REVISION file' do
58
+ let(:xrevision) { Rack::Xrevision.new(app, :app_path => app_path) }
59
+
60
+ context 'and app is not in git repo' do
61
+ it 'doesn\' add X-commit-id header' do
62
+ status, headers, body = xrevision.call(env)
63
+ headers['X-Rev'].should == ''
64
+ end
65
+ end
66
+
67
+ context 'and app is in git repo' do
68
+ before do
69
+ Dir.chdir app_path do
70
+ `git init && touch foo && git add . && git commit -m 'initial commit'`
71
+ @rev_id = %x[git rev-list master | head -n 1]
72
+ end
73
+ end
74
+
75
+ it 'call git command to get latest commit id' do
76
+ status, headers, body = xrevision.call(env)
77
+ headers['X-Rev'].should == @rev_id
78
+ end
79
+ end
80
+ end
81
+ end
data/xrevision.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/xrevision/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rack-xrevision"
8
+ gem.version = Rack::Xrevision::VERSION
9
+ gem.authors = ["Michal Wrobel"]
10
+ gem.email = ["sparrovv@gmail.com"]
11
+ gem.description = %q{Rack middleware that puts revision id into response headers}
12
+ gem.summary = %q{Rack middleware that puts revision id into response headers }
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_dependency('rack')
20
+ gem.add_development_dependency('rspec')
21
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-xrevision
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michal Wrobel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Rack middleware that puts revision id into response headers
47
+ email:
48
+ - sparrovv@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/rack-xrevision.rb
60
+ - lib/rack/xrevision.rb
61
+ - lib/rack/xrevision/version.rb
62
+ - spec/xrevision_spec.rb
63
+ - xrevision.gemspec
64
+ homepage: ''
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.24
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Rack middleware that puts revision id into response headers
88
+ test_files:
89
+ - spec/xrevision_spec.rb
90
+ has_rdoc: