class RubyBuildInfo::Middleware

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/ruby_build_info/middleware.rb, line 5
def initialize(app, options = {})
  @app = app
  @file_paths = options[:file_paths]
  @token = options[:token]
end

Public Instance Methods

call(env) click to toggle source
# File lib/ruby_build_info/middleware.rb, line 11
def call(env)
  if env['PATH_INFO'] == '/build_info'
    @env = env
    return @app.call(env) unless token_valid?

    @build_info = {}
    @build_info[:git] = git_revision unless git_revision.empty?
    read_file_paths
    rails_info
    @build_info[:gems] = bundle_show
    output = JSON.pretty_generate(@build_info)
    [200, {'Content-Type' => 'application/json'}, [output]]
  else
    @app.call(env)
  end
end

Private Instance Methods

bundle_show() click to toggle source
# File lib/ruby_build_info/middleware.rb, line 30
def bundle_show
  `bundle show`.gsub(/\n /, '').gsub(/\n/, '').split(' * ')
end
git_revision() click to toggle source
# File lib/ruby_build_info/middleware.rb, line 34
def git_revision
  `git log --pretty=format:'%ad %h %d' --abbrev-commit --date=short -1`
end
params() click to toggle source
# File lib/ruby_build_info/middleware.rb, line 47
def params
  query_string = @env['QUERY_STRING']
  return nil if query_string.nil?
  Hash[query_string.split('&').map{ |q| q.split('=') }]
end
rails_defined?() click to toggle source
# File lib/ruby_build_info/middleware.rb, line 53
def rails_defined?
  defined? Rails
end
rails_info() click to toggle source
# File lib/ruby_build_info/middleware.rb, line 57
def rails_info
  if rails_defined?
    result = Rails::Info.properties
    @build_info['Rails::Info'] = Hash[result]
  end
end
read_file_paths() click to toggle source
# File lib/ruby_build_info/middleware.rb, line 38
def read_file_paths
  if @file_paths
    @file_paths.each do |file|
      raise "File does not exist: #{file}" unless File.exist? file
      @build_info[file] = `less #{file}`
    end
  end
end
token_param() click to toggle source
# File lib/ruby_build_info/middleware.rb, line 64
def token_param
  params['token'] if params
end
token_valid?() click to toggle source
# File lib/ruby_build_info/middleware.rb, line 68
def token_valid?
  return true if @token.nil?
  return false if token_param.nil?
  token_param == @token
end