Class: Rack::GitRevision

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-git-revision.rb,
lib/rack-git-revision/version.rb

Constant Summary collapse

VERSION =
'1.0.2'

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ GitRevision

Returns a new instance of GitRevision.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rack-git-revision.rb', line 5

def initialize(app, options = {})
  @app = app
  @options = {
    path: '/dev/revision',
    git_path: './.git',
    revision: lambda { |git_path|
      if git_path
        `git --git-dir #{git_path} log -1 --pretty="format:%H"`
      else
        `git log -1 --pretty="format:%H"`
      end
    },
    body: lambda { |revision| revision },
    status: lambda { |revision| revision ? 200 : 404 },
    headers: lambda { |revision| { 'Content-Type' => 'text/plain' } },
  }.merge(options)
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rack-git-revision.rb', line 23

def call(env)
  if env['PATH_INFO'] == @options[:path]
    git_path = @options[:git_path]
    revision = @options[:revision].call(git_path) rescue nil
    body = [@options[:body].call(revision) || 'not found']
    status = @options[:status].call(revision)
    headers = @options[:headers].call(revision)
    [status, headers, body]
  else
    @app.call(env)
  end
end