Class: Rack::RevisionInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-revision-info.rb

Constant Summary collapse

INJECT_ACTIONS =
[:after, :before, :append, :prepend, :swap, :inner_html]

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ RevisionInfo

Returns a new instance of RevisionInfo.



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

def initialize(app, opts={})
  @app = app
  path = opts[:path] or raise ArgumentError, "You must specify directory of your local repository with :path option"
  revision, date = get_revision_info(path, opts)
  @revision_info = "#{get_revision_label(opts)} #{revision || 'unknown'}"
  @revision_info << " (#{date.strftime(get_date_format(opts))})" if date
  @action = (opts.keys & INJECT_ACTIONS).first
  @into_header = opts[:header]
  if @action
    require ::File.join(::File.dirname(__FILE__), 'rack-revision-info', 'nokogiri_backend')
    @selector = opts[@action]
    @action = :inner_html= if @action == :inner_html
  end
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rack-revision-info.rb', line 20

def call(env)
  status, headers, body = @app.call(env)
  if @into_header
    h = @into_header == true ? 'X-Revision-Info' : @into_header
    headers[h] = @revision_info
  elsif headers['Content-Type'].to_s.include?('text/html') && !Rack::Request.new(env).xhr?
    new_body = ""
    body.each { |line| new_body << line }
    body = new_body
    begin
      if @action
        doc = Nokogiri.parse(body)
        elements = doc.css(@selector)
        if elements.size > 0
          elements.each { |e| e.send(@action, "<span class=\"rack-revision-info\">#{@revision_info}</span>") }
          body = doc.to_s
        end
      end
    rescue => e
      puts e
      puts e.backtrace
    end
    body << %(\n<!-- #{@revision_info} -->\n)
    headers["Content-Length"] = body.bytesize.to_s
    body = [body]
  end
  [status, headers, body]
end