sha_header 0.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,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm gemset use sha_header
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in sha_header.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake'
5
+ require 'rspec/core/rake_task'
6
+
7
+ desc "Run all examples"
8
+ RSpec::Core::RakeTask.new('spec') do |t|
9
+ t.pattern = 'spec/**/*_spec.rb'
10
+ end
data/lib/sha_header.rb ADDED
@@ -0,0 +1,3 @@
1
+ module SHAHeader
2
+ autoload 'Middleware', 'sha_header/middleware'
3
+ end
@@ -0,0 +1,41 @@
1
+ require 'active_support/core_ext/object/blank'
2
+ require 'active_support/memoizable'
3
+
4
+ module SHAHeader
5
+ class Middleware
6
+ extend ::ActiveSupport::Memoizable
7
+
8
+ def initialize(app, options = {})
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ @app.call(env).tap do |response|
14
+ response[1]['X-Git-SHA'] = current_git_sha
15
+ end
16
+ end
17
+
18
+
19
+ private
20
+
21
+
22
+ def on_heroku?
23
+ ENV['HEROKU_UPID'].present?
24
+ end
25
+
26
+ def revision_present?
27
+ ::Rails.root.join('REVISION').exist?
28
+ end
29
+
30
+ def current_git_sha
31
+ if revision_present?
32
+ File.read(::Rails.root.join('REVISION'))
33
+ elsif on_heroku?
34
+ ENV['COMMIT_HASH']
35
+ else
36
+ `cd "#{::Rails.root}" && git rev-parse HEAD 2>/dev/null`.strip
37
+ end
38
+ end
39
+ memoize :current_git_sha
40
+ end
41
+ end
@@ -0,0 +1,10 @@
1
+ require 'sha_header'
2
+ require 'rails'
3
+
4
+ module SHAHeader
5
+ class Railtie < Rails::Railtie
6
+ initializer "sha_header.configure_rails_initialization" do |app|
7
+ app.middleware.use SHAHeader::Middleware
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module SHAHeader
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'sha_header/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'sha_header'
7
+ s.version = SHAHeader::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Nathaniel Bibler']
10
+ s.email = ['gem@nathanielbibler.com']
11
+ s.homepage = 'https://github.com/nbibler/sha_header'
12
+ s.summary = %q{Add a header to the response of your Rails 3 application containing the current commit SHA.}
13
+ s.description = %q{This library adds a new, custom X-Git-SHA header to your Rails 3 application's response which contains the SHA hash that your application is currently running.}
14
+
15
+ s.rubyforge_project = 'sha_header'
16
+
17
+ s.add_dependency 'rails', '~>3.0.0'
18
+
19
+ s.add_development_dependency 'rspec', '~>2.5.0'
20
+ s.add_development_dependency 'fakefs', '~>0.3.0'
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
25
+ s.require_paths = ['lib']
26
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe SHAHeader::Middleware do
4
+ include FakeFS::SpecHelpers
5
+
6
+ let(:parent_app) {lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['Hello']] }}
7
+ let(:middleware) { SHAHeader::Middleware.new(parent_app) }
8
+ let(:env) { Rack::MockRequest.env_for('/hello') }
9
+
10
+ context 'the X-Git-SHA response header' do
11
+ subject { middleware.call(env)[1]['X-Git-SHA'] }
12
+
13
+ it 'is added' do
14
+ should be_present
15
+ end
16
+
17
+ context 'on Heroku' do
18
+ before(:each) { ENV['COMMIT_HASH'] = 'ABCDEFG'; ENV['HEROKU_UPID'] = '1' }
19
+ after(:each) { ENV['COMMIT_HASH'] = ENV['HEROKU_UPID'] = nil }
20
+
21
+ it { should == 'ABCDEFG' }
22
+ end
23
+
24
+ context 'with a REVISION file' do
25
+ use_fakefs
26
+
27
+ let(:path) { Rails.root.join('REVISION') }
28
+ before(:each) { File.open(path, 'w') { |file| file.write 'HIJKLMNO' }}
29
+ after(:each) { File.delete(path) }
30
+
31
+ it { should == 'HIJKLMNO' }
32
+ end
33
+
34
+ context 'when querying git' do
35
+ it 'returns the current HEAD SHA' do
36
+ middleware.should_receive(:`).
37
+ with(%r{git rev-parse HEAD}).
38
+ and_return('XYZ')
39
+ should == 'XYZ'
40
+ end
41
+ end
42
+ end
43
+
44
+
45
+ it 'is Rack::Lint compliant' do
46
+ app = Rack::Lint.new(middleware)
47
+ app.call(env)
48
+ end
49
+ end
@@ -0,0 +1,11 @@
1
+ $:.unshift(File.expand_path('../', __FILE__))
2
+ $:.unshift(File.expand_path('../../lib', __FILE__))
3
+
4
+ require 'rspec'
5
+ require 'rack'
6
+ require 'sha_header'
7
+
8
+ Dir[File.expand_path('../support/**/*.rb', __FILE__)].each do |support|
9
+ require support
10
+ end
11
+
@@ -0,0 +1,18 @@
1
+ require 'fakefs'
2
+
3
+ module FakeFS::SpecHelpers
4
+ def use_fakefs
5
+ before(:each) do
6
+ FakeFS.activate!
7
+ end
8
+
9
+ after(:each) do
10
+ FakeFS.deactivate!
11
+ FakeFS::FileSystem.clear
12
+ end
13
+ end
14
+ end
15
+
16
+ RSpec.configure do |config|
17
+ config.extend FakeFS::SpecHelpers
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'pathname'
2
+
3
+ module Rails
4
+ def self.root
5
+ Pathname.new(File.expand_path('../../../', __FILE__))
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sha_header
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Nathaniel Bibler
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-31 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ version: 3.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 27
46
+ segments:
47
+ - 2
48
+ - 5
49
+ - 0
50
+ version: 2.5.0
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: fakefs
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ hash: 19
62
+ segments:
63
+ - 0
64
+ - 3
65
+ - 0
66
+ version: 0.3.0
67
+ type: :development
68
+ version_requirements: *id003
69
+ description: This library adds a new, custom X-Git-SHA header to your Rails 3 application's response which contains the SHA hash that your application is currently running.
70
+ email:
71
+ - gem@nathanielbibler.com
72
+ executables: []
73
+
74
+ extensions: []
75
+
76
+ extra_rdoc_files: []
77
+
78
+ files:
79
+ - .gitignore
80
+ - .rvmrc
81
+ - Gemfile
82
+ - Rakefile
83
+ - lib/sha_header.rb
84
+ - lib/sha_header/middleware.rb
85
+ - lib/sha_header/railtie.rb
86
+ - lib/sha_header/version.rb
87
+ - sha_header.gemspec
88
+ - spec/models/middleware_spec.rb
89
+ - spec/spec_helper.rb
90
+ - spec/support/fakefs.rb
91
+ - spec/support/rails.rb
92
+ has_rdoc: true
93
+ homepage: https://github.com/nbibler/sha_header
94
+ licenses: []
95
+
96
+ post_install_message:
97
+ rdoc_options: []
98
+
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ hash: 3
116
+ segments:
117
+ - 0
118
+ version: "0"
119
+ requirements: []
120
+
121
+ rubyforge_project: sha_header
122
+ rubygems_version: 1.6.2
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: Add a header to the response of your Rails 3 application containing the current commit SHA.
126
+ test_files:
127
+ - spec/models/middleware_spec.rb
128
+ - spec/spec_helper.rb
129
+ - spec/support/fakefs.rb
130
+ - spec/support/rails.rb