Class: Wonki::WikiPage

Inherits:
Object
  • Object
show all
Defined in:
lib/wonki/wiki_page.rb

Instance Method Summary collapse

Constructor Details

#initialize(repo_path, cache_control = nil) ⇒ WikiPage

Returns a new instance of WikiPage.



7
8
9
10
# File 'lib/wonki/wiki_page.rb', line 7

def initialize(repo_path, cache_control=nil)
  @cache_control = cache_control
  @repo_path = repo_path
end

Instance Method Details

#build_response(path) ⇒ Object



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

def build_response(path)
  path = "/home" if path == "/"
  storage = Wonki::Storage.new(@repo_path)
        
  headers = {"Content-Type" => "text/html", "Content-Language" => "en"}
  
  begin
	git_data = storage.build(path)
	response_body = format_data(git_data)
	headers["Last-Modified"] = git_data[:last_modified].httpdate
	headers["Etag"] = Digest::MD5.hexdigest(git_data[:content])
	headers = set_cache_control headers
	status = 200
  rescue Wonki::PageNotFound
	response_body = "Page Not Found"
	status = 404
  rescue RuntimeError => e
	response_body = "Server Error: #{e.message}\r\n#{e.stack_trace}"
	status = 500
  end
  
  [status, headers, response_body] 
end

#call(env) ⇒ Object



12
13
14
15
# File 'lib/wonki/wiki_page.rb', line 12

def call(env)
  req = Rack::Request.new(env)
  build_response(req.path)
end

#format_data(data) ⇒ Object



41
42
43
# File 'lib/wonki/wiki_page.rb', line 41

def format_data data
  %Q{<h2 id="location">#{data[:route_name]}</h2><div id="content">#{Flannel.quilt(data[:content])}</div>}
end

#set_cache_control(headers) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/wonki/wiki_page.rb', line 45

def set_cache_control headers
  return headers unless @cache_control
  
  if @cache_control[:max_age]
	if @cache_control[:response_directive]
	  headers["Cache-Control"] = "max-age=#{@cache_control[:max_age]}, #{@cache_control[:response_directive]}"
	else
	  headers["Cache-Control"] = "max-age=#{@cache_control[:max_age]}"
	end
  else
	if @cache_control[:response_directive]
	  headers["Cache-Control"] = @cache_control[:response_directive]
	end
  end
  
  headers
end