Class: Golf::Rack

Inherits:
Object
  • Object
show all
Defined in:
lib/golf/rack.rb

Instance Method Summary collapse

Constructor Details

#initialize(app = nil) ⇒ Rack

Returns a new instance of Rack.



4
5
6
7
8
9
10
# File 'lib/golf/rack.rb', line 4

def initialize(app = nil)
  @app = app if app
  @compiler = Golf::Compiler.new
  resource_path = File.expand_path("../../../resources", __FILE__)
  @resources = {}
  Dir["#{resource_path}/*"].each { |x| @resources = @resources.merge({ "/#{x.split('/').last}" => File.read(x) }) }
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
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
48
49
50
51
52
# File 'lib/golf/rack.rb', line 12

def call(env)
  if env["PATH_INFO"] == ""
    if env["REQUEST_URI"].split('').last != "/"
      return [301, {'Location' => env["REQUEST_URI"] << "/"}, ['See Ya!']] 
    else
      env["PATH_INFO"] = "/"
    end
  end

  code = "200"
  
  #pass through for overrides
  if File.exists?("golfapp/#{env["PATH_INFO"].sub('/','')}") and env["PATH_INFO"] != "/"
    mime = MIME_TYPES[".#{env["PATH_INFO"].split('.').last}"]
    result = File.read("golfapp/#{env["PATH_INFO"].sub('/','')}")
    return [code, { 'Content-Type' => mime, 'Content-Length' => result.length.to_s}, [result]]
  end

  #regular handling from gem resources/dynamic compilation
  case env["PATH_INFO"]
  when "/"
    mime = MIME_TYPES[".html"]
    if File.exists?('golfapp/index.html')
      result = File.read('golfapp/index.html')
    else
      result = @resources["/index.html"]
    end
  when "/components.js"
    mime = MIME_TYPES[".js"]
    result = @compiler.generate_componentsjs
  else
    mime = MIME_TYPES[".#{env["PATH_INFO"].split('.').last}"]
    result = @resources[env["PATH_INFO"]]
    unless result
      mime = 'text/plain'
      result = 'not found'
      code = "404"
    end
  end
  return [code, { 'Content-Type' => mime, 'Content-Length' => result.length.to_s}, [result]]
end