Class: Rack::GitUp

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-git-up.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of GitUp.



7
8
9
10
11
12
13
# File 'lib/rack-git-up.rb', line 7

def initialize(app, options={})
  @app = app
  @repo = Grit::Repo.new(options[:repo])
  @urls = options[:urls] || ["/favicon.ico"]
  @root = options[:root] || Dir.pwd
  @file_server = Rack::File.new(@root)
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rack-git-up.rb', line 15

def call(env)
  path = env["PATH_INFO"]
  can_serve = @urls.any? { |url| path.index(url) == 0 }

  if can_serve
    pull_from_git path
    result = @file_server.call(env)
    return result if result[0] == 200
  end

  @app.call(env)
end

#pull_from_git(path) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/rack-git-up.rb', line 28

def pull_from_git path
  blob = (@repo.tree/path[1..-1])   #remove leading slash

  return unless blob

  f = ::File.new(::File.join(@root, path), "w")
  f.syswrite(blob.data)
  f.close
end