class SHAHeader::Middleware

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/sha_header/middleware.rb, line 7
def initialize(app, options = {})
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/sha_header/middleware.rb, line 11
def call(env)
  @app.call(env).tap do |response|
    response[1]['X-Git-SHA'] = current_git_sha
  end
end

Private Instance Methods

current_git_sha() click to toggle source
# File lib/sha_header/middleware.rb, line 29
def current_git_sha
  @current_git_sha ||= begin
    if revision_present?
      File.read(::Rails.root.join('REVISION')).strip
    elsif on_heroku?
      ENV['COMMIT_HASH'].strip
    else
      `cd "#{::Rails.root}" && git rev-parse HEAD 2>/dev/null`.strip
    end
  end
end
on_heroku?() click to toggle source
# File lib/sha_header/middleware.rb, line 21
def on_heroku?
  ENV['HEROKU_UPID'].present?
end
revision_present?() click to toggle source
# File lib/sha_header/middleware.rb, line 25
def revision_present?
  ::Rails.root.join('REVISION').exist?
end