rack-revision 1.0.1 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 62a12f243a22cd037fa6506a704a1d94cfadd93327412558258ce327560a0b52
4
+ data.tar.gz: 3aae6d6d5b3e0c0ab0881c1926e5d4c33e1f81f38e54c9c58ee18da27b92f65c
5
+ SHA512:
6
+ metadata.gz: 36f785c3e545dd733c9ba45bd3b050df65c1fd90065e4f047274820969655351a5e6425d9582426d092ddafb0659fbcd90ff6b44aafb5def60a995599d306b52
7
+ data.tar.gz: 4bc2a640b44ac615434a18bafe55236fc2327f97c845600420ca4afebd03d2cf6144a69fb0b2626a5daf99459a228564957aca0288aa7e39fa752533a7a14e62
@@ -0,0 +1,20 @@
1
+ name: Build
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ test:
7
+ runs-on: ubuntu-latest
8
+ strategy:
9
+ matrix:
10
+ ruby: ['2.6', '2.7', '3.0', '3.1']
11
+ steps:
12
+ - uses: actions/checkout@v2
13
+ - name: Set up Ruby
14
+ uses: ruby/setup-ruby@v1
15
+ with:
16
+ ruby-version: ${{ matrix.ruby }}
17
+ - name: Install dependencies
18
+ run: bundle install
19
+ - name: Run tests
20
+ run: bundle exec rake
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'http://rubygems.org'
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2012-2022 Dan Sosedoff, dan.sosedoff@gmail.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,51 +1,78 @@
1
1
  # Rack::Revision
2
2
 
3
- Rack::Revision is a quick drop-in component to enable code revision tracking.
3
+ Rack::Revision is a quick drop-in component to enable code revision tracking.
4
4
  It adds `X-Revision` header with the code revision from capistrano's REVISION file.
5
5
 
6
+ [![Build](https://github.com/sosedoff/rack-revision/actions/workflows/test.yml/badge.svg)](https://github.com/sosedoff/rack-revision/actions/workflows/test.yml)
7
+ [![Gem Version](https://img.shields.io/gem/v/rack-revision.svg)](http://badge.fury.io/rb/rack-revision)
8
+
6
9
  ## Installation
7
10
 
8
- From rubygems:
11
+ Add dependency to your Gemfile:
9
12
 
10
13
  ```
11
- gem install rack-revision
14
+ gem "rack-revision"
12
15
  ```
13
16
 
14
- Or install with bundler:
17
+ Install gem:
15
18
 
16
19
  ```
17
- gem 'rack-revision'
20
+ bundle install
18
21
  ```
19
22
 
20
- And run `bundle install`
21
-
22
23
  ## Usage
23
24
 
24
25
  Rack::Revision is implemented as a piece of Rack middleware and can be used with
25
- any Rack-based application. If you have a `config.ru` rackup file you can
26
+ any Rack-based application. If you have a `config.ru` rackup file you can
26
27
  drop the following snippet (for sinatra app):
27
28
 
28
29
  ```ruby
29
- require 'rack/revision'
30
+ require "rack/revision"
30
31
 
31
32
  # Basic example
32
33
  use Rack::Revision
33
34
 
34
35
  # With custom revision header
35
- use Rack::Revision, :header => 'X-CODE-VERSION'
36
+ use Rack::Revision, :header => "X-CODE-VERSION"
36
37
 
37
38
  # With custom filename and default value
38
- use Rack::Revision, :filename => '.version', :default => 'n/a'
39
+ use Rack::Revision, :filename => ".version", :default => "n/a"
39
40
 
41
+ # Execute application
40
42
  run Sinatra::Application
41
43
  ```
42
44
 
43
45
  Available options:
44
46
 
45
- - `:header` - Sets a custom revision header. Default: `X-Revision`
46
- - `:filename` - Sets a filename with revision data. Default: `REVISION`
47
- - `:default` - Sets a revision value if file does not exist. Default: UNDEFINED
48
- - `:rack_env` - Sets a revision value to Rack's `env` hash. Default: `env['rack.app_revision']`
47
+ - `:header` - Changes revision header name. Default: `X-Revision`
48
+ - `:filename` - Changes the revision filename. Default: `REVISION`
49
+ - `:rack_env` - Changes Rack environment key for revision. Default: `env['rack.app_revision']`
50
+ - `:env_var` - Sets revision value from an environment variable. Default: `RACK_REVISION`
51
+ - `:default` - Sets revision value if env var or a file with revision does not exist. Default: `UNDEFINED`
52
+
53
+ ## Example
54
+
55
+ To see what the header might look like, run a curl command:
56
+
57
+ ```
58
+ curl -i -X HEAD "http://yourdomain.com"
59
+ ```
60
+
61
+ Example response:
62
+
63
+ ```
64
+ HTTP/1.1 200 OK
65
+ Server: nginx/1.4.4
66
+ Content-Type: text/html; charset=utf-8
67
+ Connection: keep-alive
68
+ X-UA-Compatible: IE=Edge,chrome=1
69
+ ETag: "5e622b6dab40d3cb8ad8a4bd51627a59"
70
+ Cache-Control: max-age=0, private, must-revalidate
71
+ X-Request-Id: ae07ad30bd857788bebfa1421576f96e
72
+ X-Runtime: 0.022007
73
+ X-Rack-Cache: miss
74
+ X-Revision: a3de2043d4cea6182e511c9c73f57f4f1e0dbc2b
75
+ ```
49
76
 
50
77
  ## Test
51
78
 
@@ -57,10 +84,6 @@ rake test
57
84
 
58
85
  ## License
59
86
 
60
- Copyright (c) 2012 Dan Sosedoff.
61
-
62
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
63
-
64
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
87
+ The MIT License
65
88
 
66
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
89
+ Copyright (c) 2012-2022 Dan Sosedoff <dan.sosedoff@gmail.com>
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class Revision
3
- VERSION = '1.0.1'
3
+ VERSION = "1.1.1"
4
4
  end
5
- end
5
+ end
data/lib/rack/revision.rb CHANGED
@@ -6,39 +6,62 @@ module Rack
6
6
  @@revision = nil
7
7
 
8
8
  def initialize(app, options={})
9
- @options = {
10
- :header => options[:header].nil? ? 'X-Revision' : options[:header],
11
- :filename => options[:filename] || 'REVISION',
12
- :default => options[:default] || 'UNDEFINED',
13
- :rack_env => options[:rack_env].nil? ? 'rack.app_revision' : options[:rack_env]
14
- }
9
+ initialize_options(options)
15
10
 
16
11
  @app = app
12
+ @dir = Dir.pwd
17
13
  end
18
-
14
+
19
15
  def call(env)
20
- env[@options[:rack_env]] = revision if @options[:rack_env]
16
+ if @options[:rack_env]
17
+ env[@options[:rack_env]] = revision
18
+ end
19
+
21
20
  status, headers, body = @app.call(env)
22
- headers[@options[:header]] = revision if @options[:header]
21
+
22
+ if @options[:header]
23
+ headers[@options[:header]] = revision
24
+ end
25
+
23
26
  [status, headers, body]
24
27
  end
25
28
 
26
29
  def reset_revision
27
30
  @@revision = nil
28
31
  end
29
-
32
+
30
33
  protected
31
-
34
+
32
35
  def revision
33
- @@revision ||= read_revision
36
+ @@revision ||= (fetch_from_env || fetch_from_file || fetch_default)
34
37
  end
35
-
36
- def read_revision
37
- File.exists?(detected_filename) ? File.read(detected_filename).strip : @options[:default]
38
+
39
+ def fetch_from_env
40
+ ENV[@options[:env_var]]
41
+ end
42
+
43
+ def fetch_from_file
44
+ if ::File.exist?(detected_filename)
45
+ ::File.read(detected_filename).strip
46
+ end
47
+ end
48
+
49
+ def fetch_default
50
+ @options[:default]
38
51
  end
39
52
 
40
53
  def detected_filename
41
- @file ||= (@options[:filename] =~ /\A\// ? @options[:filename] : File.join(Dir.pwd, @options[:filename]))
54
+ @file ||= (@options[:filename] =~ /\A\// ? @options[:filename] : File.join(@dir, @options[:filename]))
55
+ end
56
+
57
+ def initialize_options(options)
58
+ @options = {
59
+ :header => options[:header].nil? ? "X-Revision" : options[:header],
60
+ :rack_env => options[:rack_env].nil? ? "rack.app_revision" : options[:rack_env],
61
+ :env_var => options[:env_var] || "RACK_REVISION",
62
+ :filename => options[:filename] || "REVISION",
63
+ :default => options[:default] || "UNDEFINED"
64
+ }
42
65
  end
43
66
  end
44
67
  end
@@ -10,7 +10,11 @@ Gem::Specification.new do |s|
10
10
  s.email = ["dan.sosedoff@gmail.com"]
11
11
 
12
12
  s.add_runtime_dependency 'rack', '>= 1.0'
13
+
14
+ s.add_development_dependency 'rake', '~> 10.0'
13
15
  s.add_development_dependency 'rack-test', '>= 0'
16
+ s.add_development_dependency 'simplecov', '~> 0.8'
17
+ s.add_development_dependency 'test-unit', '>= 0'
14
18
 
15
19
  s.files = `git ls-files`.split("\n")
16
20
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -1,14 +1,17 @@
1
- require 'test/unit'
2
- require 'rack/test'
3
- require 'rack/revision'
4
- require 'fileutils'
1
+ require "simplecov"
2
+ SimpleCov.start
3
+
4
+ require "test/unit"
5
+ require "rack/test"
6
+ require "rack/revision"
7
+ require "fileutils"
5
8
 
6
9
  class TestRevision < Test::Unit::TestCase
7
10
  include Rack::Test::Methods
8
11
 
9
12
  def default_app
10
13
  lambda do |env|
11
- headers = {'Content-Type' => "text/html"}
14
+ headers = {"Content-Type" => "text/html"}
12
15
  [200, headers, ["OK"]]
13
16
  end
14
17
  end
@@ -24,11 +27,11 @@ class TestRevision < Test::Unit::TestCase
24
27
  attr_writer :app
25
28
 
26
29
  def setup
27
- FileUtils.mkdir('./test/tmp')
30
+ FileUtils.mkdir("./test/tmp")
28
31
  end
29
32
 
30
33
  def teardown
31
- FileUtils.rm_rf('./test/tmp')
34
+ FileUtils.rm_rf("./test/tmp")
32
35
  end
33
36
 
34
37
  def test_revision_request
@@ -42,8 +45,8 @@ class TestRevision < Test::Unit::TestCase
42
45
  self.app.reset_revision
43
46
  get app_url
44
47
 
45
- assert_not_nil last_response.headers['X-REVISION']
46
- assert_not_nil last_response.headers['X-Revision']
48
+ assert_not_nil last_response.headers["X-REVISION"]
49
+ assert_not_nil last_response.headers["X-Revision"]
47
50
  end
48
51
 
49
52
  def test_blank
@@ -51,15 +54,15 @@ class TestRevision < Test::Unit::TestCase
51
54
  self.app.reset_revision
52
55
  get app_url
53
56
 
54
- assert_nil last_response.headers['X-REVISION']
55
- assert_nil last_response.headers['X-Revision']
57
+ assert_nil last_response.headers["X-REVISION"]
58
+ assert_nil last_response.headers["X-Revision"]
56
59
  end
57
60
 
58
61
  def test_default_value
59
62
  self.app.reset_revision
60
63
  get app_url
61
64
 
62
- assert_equal "UNDEFINED", last_response.headers['X-Revision']
65
+ assert_equal "UNDEFINED", last_response.headers["X-Revision"]
63
66
  end
64
67
 
65
68
  def test_custom_value
@@ -67,7 +70,7 @@ class TestRevision < Test::Unit::TestCase
67
70
  self.app.reset_revision
68
71
 
69
72
  get app_url
70
- assert_equal "FOOBAR", last_response.headers['X-Revision']
73
+ assert_equal "FOOBAR", last_response.headers["X-Revision"]
71
74
  end
72
75
 
73
76
  def test_custom_header
@@ -75,44 +78,61 @@ class TestRevision < Test::Unit::TestCase
75
78
  self.app.reset_revision
76
79
 
77
80
  get app_url
78
- assert_not_nil last_response.headers['FOOBAR']
81
+ assert_not_nil last_response.headers["FOOBAR"]
79
82
  end
80
83
 
81
84
  def test_custom_filename
82
- File.open('./test/tmp/REVISION', 'w') { |f| f.write('qwe123') }
85
+ File.open("./test/tmp/REVISION", "w") { |f| f.write("qwe123") }
83
86
 
84
- self.app = Rack::Revision.new(default_app, :filename => './test/tmp/REVISION')
87
+ self.app = Rack::Revision.new(default_app, :filename => "./test/tmp/REVISION")
85
88
  self.app.reset_revision
86
89
 
87
90
  get app_url
88
- assert_equal 'qwe123', last_response.headers['X-Revision']
91
+ assert_equal "qwe123", last_response.headers["X-Revision"]
89
92
  end
90
93
 
91
94
  def test_custom_filename_starting_from_root
92
- File.open('./test/tmp/REVISION', 'w') { |f| f.write('qwe123') }
95
+ File.open("./test/tmp/REVISION", "w") { |f| f.write("qwe123") }
93
96
  filename = File.expand_path("./test/tmp/REVISION")
94
97
 
95
98
  self.app = Rack::Revision.new(default_app, :filename => filename)
96
99
  self.app.reset_revision
97
100
 
98
101
  get app_url
99
- assert_equal 'qwe123', last_response.headers['X-Revision']
102
+ assert_equal "qwe123", last_response.headers["X-Revision"]
103
+ end
104
+
105
+ def test_dir_does_not_exist
106
+ File.write("./test/tmp/REVISION", "example")
107
+
108
+ Dir.chdir("./test/tmp") do
109
+ self.app.reset_revision
110
+
111
+ get app_url
112
+ assert_equal "example", last_response.headers["X-Revision"]
113
+
114
+ FileUtils.rm_rf("../tmp")
115
+ self.app.reset_revision
116
+
117
+ get app_url
118
+ assert_equal "UNDEFINED", last_response.headers["X-Revision"]
119
+ end
100
120
  end
101
121
 
102
122
  def test_env_is_present
103
123
  self.app.reset_revision
104
124
  get app_url
105
125
 
106
- assert_not_nil last_request.env['rack.app_revision']
126
+ assert_not_nil last_request.env["rack.app_revision"]
107
127
  end
108
128
 
109
129
  def test_custom_env
110
- self.app = Rack::Revision.new(default_app, :rack_env => 'rack.custom_env')
130
+ self.app = Rack::Revision.new(default_app, :rack_env => "rack.custom_env")
111
131
  self.app.reset_revision
112
132
  get app_url
113
133
 
114
- assert_nil last_request.env['rack.app_revision']
115
- assert_not_nil last_request.env['rack.custom_env']
134
+ assert_nil last_request.env["rack.app_revision"]
135
+ assert_not_nil last_request.env["rack.custom_env"]
116
136
  end
117
137
 
118
138
  def test_disable_env
@@ -120,6 +140,25 @@ class TestRevision < Test::Unit::TestCase
120
140
  self.app.reset_revision
121
141
  get app_url
122
142
 
123
- assert_nil last_request.env['rack.app_revision']
143
+ assert_nil last_request.env["rack.app_revision"]
144
+ end
145
+
146
+ def test_from_env_var
147
+ self.app = Rack::Revision.new(default_app)
148
+ self.app.reset_revision
149
+
150
+ ENV["RACK_REVISION"] = "revision"
151
+ get app_url
152
+ assert_equal "revision", last_response.headers["X-Revision"]
153
+ end
154
+
155
+ def test_from_custom_env_var
156
+ self.app = Rack::Revision.new(default_app, env_var: "SERVICE_REVISION")
157
+ self.app.reset_revision
158
+
159
+ ENV["RACK_REVISION"] = "bad"
160
+ ENV["SERVICE_REVISION"] = "good"
161
+ get app_url
162
+ assert_equal "good", last_response.headers["X-Revision"]
124
163
  end
125
164
  end
metadata CHANGED
@@ -1,38 +1,85 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-revision
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
5
- prerelease:
4
+ version: 1.1.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Dan Sosedoff
9
- autorequire:
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-12-17 00:00:00.000000000Z
11
+ date: 2022-06-06 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rack
16
- requirement: &2157761320 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '1.0'
22
20
  type: :runtime
23
21
  prerelease: false
24
- version_requirements: *2157761320
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
25
41
  - !ruby/object:Gem::Dependency
26
42
  name: rack-test
27
- requirement: &2157759940 !ruby/object:Gem::Requirement
28
- none: false
43
+ requirement: !ruby/object:Gem::Requirement
29
44
  requirements:
30
- - - ! '>='
45
+ - - ">="
31
46
  - !ruby/object:Gem::Version
32
47
  version: '0'
33
48
  type: :development
34
49
  prerelease: false
35
- version_requirements: *2157759940
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: test-unit
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
36
83
  description: Adds an extra X-REVISION header with source code revision string (git,
37
84
  svn, etc)
38
85
  email:
@@ -41,9 +88,10 @@ executables: []
41
88
  extensions: []
42
89
  extra_rdoc_files: []
43
90
  files:
44
- - .gitignore
45
- - .travis.yml
91
+ - ".github/workflows/test.yml"
92
+ - ".gitignore"
46
93
  - Gemfile
94
+ - LICENSE
47
95
  - README.md
48
96
  - Rakefile
49
97
  - lib/rack/revision.rb
@@ -52,28 +100,25 @@ files:
52
100
  - test/test_revision.rb
53
101
  homepage: http://github.com/sosedoff/rack-revision
54
102
  licenses: []
55
- post_install_message:
103
+ metadata: {}
104
+ post_install_message:
56
105
  rdoc_options: []
57
106
  require_paths:
58
107
  - lib
59
108
  required_ruby_version: !ruby/object:Gem::Requirement
60
- none: false
61
109
  requirements:
62
- - - ! '>='
110
+ - - ">="
63
111
  - !ruby/object:Gem::Version
64
112
  version: '0'
65
113
  required_rubygems_version: !ruby/object:Gem::Requirement
66
- none: false
67
114
  requirements:
68
- - - ! '>='
115
+ - - ">="
69
116
  - !ruby/object:Gem::Version
70
117
  version: '0'
71
118
  requirements: []
72
- rubyforge_project:
73
- rubygems_version: 1.8.15
74
- signing_key:
75
- specification_version: 3
119
+ rubygems_version: 3.3.9
120
+ signing_key:
121
+ specification_version: 4
76
122
  summary: Code ravision rack middleware
77
123
  test_files:
78
124
  - test/test_revision.rb
79
- has_rdoc:
data/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 1.8.7
4
- - 1.9.2
5
- - 1.9.3