wonki 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.7
@@ -1,9 +1,8 @@
1
1
  require 'mime/types'
2
2
  require 'grit'
3
- require 'flannel'
4
3
 
5
4
  module Wonki
6
- class PageBuilder
5
+ class Storage
7
6
  def initialize(repo_path)
8
7
  @repo_path = repo_path
9
8
  @repository = Grit::Repo.new(@repo_path)
@@ -13,8 +12,7 @@ module Wonki
13
12
  route_name = route[1..-1] # strip leading slash
14
13
  data = find(route_name)
15
14
  mod_date = get_mod_date(route_name)
16
- content = %Q{<h2 id="location">#{route_name}</h2><div id="content">#{Flannel.quilt(data)}</div>}
17
- {:content => content, :last_modified => mod_date}
15
+ {:content => data, :last_modified => mod_date, :route_name => route_name}
18
16
  end
19
17
 
20
18
  def find(name)
@@ -24,7 +22,7 @@ module Wonki
24
22
  end
25
23
 
26
24
  def get_mod_date(name)
27
- @repository.commits.first.committed_date
25
+ @repository.commits.first.committed_date # date of the last commit to the repo.
28
26
  end
29
27
  end
30
28
  end
@@ -1,9 +1,11 @@
1
- require 'wonki/page_builder'
1
+ require 'wonki/storage'
2
2
  require 'wonki/page_not_found'
3
+ require 'flannel'
3
4
 
4
5
  module Wonki
5
6
  class WikiPage
6
- def initialize(repo_path)
7
+ def initialize(repo_path, cache_control=nil)
8
+ @cache_control = cache_control
7
9
  @repo_path = repo_path
8
10
  end
9
11
 
@@ -14,16 +16,16 @@ module Wonki
14
16
 
15
17
  def build_response(path)
16
18
  path = "/home" if path == "/"
17
- builder = Wonki::PageBuilder.new(@repo_path)
19
+ storage = Wonki::Storage.new(@repo_path)
18
20
 
19
21
  headers = {"Content-Type" => "text/html", "Content-Language" => "en"}
20
22
 
21
23
  begin
22
- git_data = builder.build(path)
23
- response_body = git_data[:content]
24
+ git_data = storage.build(path)
25
+ response_body = format_data(git_data)
24
26
  headers["Last-Modified"] = git_data[:last_modified].httpdate
25
27
  headers["Etag"] = Digest::MD5.hexdigest(git_data[:content])
26
- headers["Cache-Control"] = "max-age=300, public"
28
+ headers = set_cache_control headers
27
29
  status = 200
28
30
  rescue Wonki::PageNotFound
29
31
  response_body = "Page Not Found"
@@ -35,5 +37,27 @@ module Wonki
35
37
 
36
38
  [status, headers, response_body]
37
39
  end
40
+
41
+ def format_data data
42
+ %Q{<h2 id="location">#{data[:route_name]}</h2><div id="content">#{Flannel.quilt(data[:content])}</div>}
43
+ end
44
+
45
+ def set_cache_control headers
46
+ return headers unless @cache_control
47
+
48
+ if @cache_control[:max_age]
49
+ if @cache_control[:response_directive]
50
+ headers["Cache-Control"] = "max-age=#{@cache_control[:max_age]}, #{@cache_control[:response_directive]}"
51
+ else
52
+ headers["Cache-Control"] = "max-age=#{@cache_control[:max_age]}"
53
+ end
54
+ else
55
+ if @cache_control[:response_directive]
56
+ headers["Cache-Control"] = @cache_control[:response_directive]
57
+ end
58
+ end
59
+
60
+ headers
61
+ end
38
62
  end
39
63
  end
data/test/helper.rb CHANGED
@@ -7,4 +7,7 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
7
  require 'wonki'
8
8
 
9
9
  class Test::Unit::TestCase
10
+ def assert_false assertion
11
+ assert !assertion
12
+ end
10
13
  end
@@ -1,43 +1,33 @@
1
1
  require 'helper'
2
- require 'lib/wonki/page_builder'
2
+ require 'lib/wonki/storage'
3
3
  require 'lib/wonki/page_not_found'
4
4
 
5
- class PageBuilderTest < Test::Unit::TestCase
5
+ class StorageTest < Test::Unit::TestCase
6
6
  context "Building a Page" do
7
7
  setup do
8
- @builder = Wonki::PageBuilder.new("~/working/rubyyot-wiki-test")
9
- end
10
-
11
- should "include the location" do
12
- out = @builder.build("/cheese")
13
- assert_match("cheese", out[:content])
8
+ @storage = Wonki::Storage.new("~/working/rubyyot-wiki-test")
14
9
  end
15
10
 
16
11
  should "include the content" do
17
- out = @builder.build("/cheese")
12
+ out = @storage.build("/cheese")
18
13
  assert_match("swiss", out[:content])
19
14
  end
20
15
 
21
- should "flannel content" do
22
- out = @builder.build("/header")
23
- assert_match("<h2>foo</h2>", out[:content])
24
- end
25
-
26
16
  context "finding blob data" do
27
17
  should "find page contents in the git repo" do
28
- blob_data = @builder.find("foo")
18
+ blob_data = @storage.find("foo")
29
19
  assert_match("bar bar", blob_data)
30
20
  end
31
21
  end
32
22
 
33
23
  context "last_modified date" do
34
24
  should "not be nil" do
35
- out = @builder.build("/foo")
25
+ out = @storage.build("/foo")
36
26
  assert_not_nil out[:last_modified]
37
27
  end
38
-
28
+
39
29
  should "return the commited date" do
40
- out = @builder.build("/foo")
30
+ out = @storage.build("/foo")
41
31
  assert_equal Time, out[:last_modified].class
42
32
  end
43
33
  end
@@ -45,7 +35,7 @@ class PageBuilderTest < Test::Unit::TestCase
45
35
  context "page doesn't exist" do
46
36
  should "throw page doesn't exist" do
47
37
  begin
48
- @builder.find("walla-walla-bing-bang")
38
+ @storage.find("walla-walla-bing-bang")
49
39
  assert false, "Should have thrown a page not found exception"
50
40
  rescue Wonki::PageNotFound
51
41
  assert true
@@ -28,9 +28,51 @@ class WikiPageTest < Test::Unit::TestCase
28
28
  should "set etag" do
29
29
  assert_not_nil @headers['Etag']
30
30
  end
31
+ end
32
+
33
+ context "caching" do
34
+ should "not set if no info passed" do
35
+ page = Wonki::WikiPage.new("~/working/rubyyot-wiki-test")
36
+ status, headers, body = page.build_response("/foo")
37
+
38
+ assert_false(headers.has_key?("Cache-Control"))
39
+ end
40
+
41
+ should "set max age" do
42
+ page = Wonki::WikiPage.new("~/working/rubyyot-wiki-test", :max_age => 300)
43
+ status, headers, body = page.build_response("/foo")
44
+
45
+ assert_equal("max-age=300", headers["Cache-Control"])
46
+ end
47
+
48
+ should "set response directive" do
49
+ page = Wonki::WikiPage.new("~/working/rubyyot-wiki-test", :response_directive => 'public')
50
+ status, headers, body = page.build_response("/foo")
51
+
52
+ assert_equal("public", headers["Cache-Control"])
53
+ end
54
+
55
+ should "set both response directive and max_age when passed" do
56
+ page = Wonki::WikiPage.new("~/working/rubyyot-wiki-test", :response_directive => 'public', :max_age => 20)
57
+ status, headers, body = page.build_response("/foo")
58
+
59
+ assert_equal("max-age=20, public", headers["Cache-Control"])
60
+ end
61
+ end
62
+
63
+ context "Formatting" do
64
+ setup do
65
+ @page = Wonki::WikiPage.new("~/working/rubyyot-wiki-test")
66
+ end
67
+
68
+ should "include the location" do
69
+ out = @page.format_data :route_name => 'wonki', :content => 'this is cool'
70
+ assert_match("wonki", out)
71
+ end
31
72
 
32
- should "set cache control" do
33
- assert_equal(@headers["Cache-Control"], "max-age=300, public")
73
+ should "flannel content" do
74
+ out = @page.format_data :route_name => 'test', :content => '==foo'
75
+ assert_match("<h2>foo</h2>", out)
34
76
  end
35
77
  end
36
78
 
data/wonki.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{wonki}
8
- s.version = "0.0.6"
8
+ s.version = "0.0.7"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["rubyyot"]
12
- s.date = %q{2010-01-12}
12
+ s.date = %q{2010-01-13}
13
13
  s.description = %q{a Rack and Git based wiki like thing}
14
14
  s.email = %q{jamal.hansen@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -24,11 +24,11 @@ Gem::Specification.new do |s|
24
24
  "Rakefile",
25
25
  "VERSION",
26
26
  "lib/wonki.rb",
27
- "lib/wonki/page_builder.rb",
28
27
  "lib/wonki/page_not_found.rb",
28
+ "lib/wonki/storage.rb",
29
29
  "lib/wonki/wiki_page.rb",
30
30
  "test/helper.rb",
31
- "test/page_builder_test.rb",
31
+ "test/storage_test.rb",
32
32
  "test/wiki_page_test.rb",
33
33
  "wonki.gemspec"
34
34
  ]
@@ -39,8 +39,8 @@ Gem::Specification.new do |s|
39
39
  s.summary = %q{a simple Rack and Git based wiki like thing}
40
40
  s.test_files = [
41
41
  "test/wiki_page_test.rb",
42
- "test/helper.rb",
43
- "test/page_builder_test.rb"
42
+ "test/storage_test.rb",
43
+ "test/helper.rb"
44
44
  ]
45
45
 
46
46
  if s.respond_to? :specification_version then
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wonki
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - rubyyot
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-12 00:00:00 -06:00
12
+ date: 2010-01-13 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -59,11 +59,11 @@ files:
59
59
  - Rakefile
60
60
  - VERSION
61
61
  - lib/wonki.rb
62
- - lib/wonki/page_builder.rb
63
62
  - lib/wonki/page_not_found.rb
63
+ - lib/wonki/storage.rb
64
64
  - lib/wonki/wiki_page.rb
65
65
  - test/helper.rb
66
- - test/page_builder_test.rb
66
+ - test/storage_test.rb
67
67
  - test/wiki_page_test.rb
68
68
  - wonki.gemspec
69
69
  has_rdoc: true
@@ -96,5 +96,5 @@ specification_version: 3
96
96
  summary: a simple Rack and Git based wiki like thing
97
97
  test_files:
98
98
  - test/wiki_page_test.rb
99
+ - test/storage_test.rb
99
100
  - test/helper.rb
100
- - test/page_builder_test.rb