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
# 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!"
  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
  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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rack_revision_info.rb', line 19

def call(env)
  status, headers, body = @app.call(env)
  if headers['Content-Type'].to_s.include?('text/html') && !Rack::Request.new(env).xhr?
    body = body.inject("") { |acc, line| acc + line }
    begin
      if @action
        doc = Nokogiri.parse(body)
        elements = doc.css(@selector)
        if elements.size > 0
          elements.each { |e| e.send(@action, @revision_info) }
          body = doc.to_s
        end
      end
    rescue => e
      puts e
      puts e.backtrace
    end
    body << %(\n<!-- #{@revision_info} -->\n)
    headers["Content-Length"] = body.size.to_s
    body = [body]
  end
  [status, headers, body]
end