Class: Wonki::WikiPage
- Inherits:
-
Object
- Object
- Wonki::WikiPage
- Defined in:
- lib/wonki/wiki_page.rb
Instance Method Summary collapse
- #build_response(path) ⇒ Object
- #call(env) ⇒ Object
- #format_data(data) ⇒ Object
-
#initialize(repo_path, flannel_cache = nil, cache_control = nil) ⇒ WikiPage
constructor
A new instance of WikiPage.
- #set_cache_control(headers) ⇒ Object
Constructor Details
#initialize(repo_path, flannel_cache = nil, cache_control = nil) ⇒ WikiPage
Returns a new instance of WikiPage.
8 9 10 11 12 |
# File 'lib/wonki/wiki_page.rb', line 8 def initialize(repo_path, flannel_cache=nil, cache_control=nil) @cache_control = cache_control @repo_path = repo_path @flannel_cache = Flannel::FileCache.new(File.(flannel_cache)) if flannel_cache end |
Instance Method Details
#build_response(path) ⇒ 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/wonki/wiki_page.rb', line 19 def build_response(path) path = "/home" if path == "/" storage = Wonki::Storage.new(@repo_path) headers = {"Content-Type" => "text/html; charset=utf-8", "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.}\r\n#{e.stack_trace}"] status = 500 end [status, headers, response_body] end |
#call(env) ⇒ Object
14 15 16 17 |
# File 'lib/wonki/wiki_page.rb', line 14 def call(env) req = Rack::Request.new(env) build_response(req.path) end |
#format_data(data) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/wonki/wiki_page.rb', line 43 def format_data data if @flannel_cache output = Flannel.quilt(data[:content], :cache => @flannel_cache) else output = Flannel.quilt(data[:content]) end %Q{<h2 id="location">#{data[:route_name]}</h2><div id="content">#{output}</div>} end |
#set_cache_control(headers) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/wonki/wiki_page.rb', line 53 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 |