blossom 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/blossom +18 -5
  2. data/lib/blossom.rb +38 -4
  3. metadata +3 -3
data/bin/blossom CHANGED
@@ -9,8 +9,22 @@ def make_file(name, content)
9
9
  File.open(name, "w") { |file| file.write(content) }
10
10
  end
11
11
 
12
- case ARGV.length
13
- when 1
12
+ def print_usage
13
+ warn "Usage: #$0 PROJECT-NAME"
14
+ end
15
+
16
+ if ARGV.empty?
17
+ print_usage
18
+ elsif ARGV[0].start_with? "-"
19
+ case ARGV[0]
20
+ when "--version", "-v"
21
+ puts Blossom::VERSION
22
+ when "--help", "-h", "-?"
23
+ print_usage
24
+ else
25
+ warn "#$0: unrecognized option: #{ARGV[0]}"
26
+ end
27
+ else
14
28
  name = ARGV.first
15
29
  name_symbol = name =~ /\W/ ? ":'#{name}'" : ":#{name}"
16
30
  unless File.exist? name
@@ -24,7 +38,7 @@ require "rubygems"
24
38
  require "bundler/setup"
25
39
  require "blossom"
26
40
 
27
- run Blossom(__FILE__, #{name_symbol})
41
+ run Blossom(__FILE__, #{name_symbol}, :cache => [1, :year])
28
42
  end_file
29
43
 
30
44
  make_file "Gemfile", <<-"end_file"
@@ -65,12 +79,11 @@ This will run your application at <http://localhost:9292/>.\n\
65
79
  You can start hacking in \`#{name}.haml\' and \`#{name}.sass\'.\n\
66
80
  If you create \`x.haml\', it will show up at <http://localhost:9292/x>.\n\
67
81
  Images and other static files go in the \`static/\' directory.\n\
82
+ In production, your pages are cached; configure this in \`config.ru\'.
68
83
  Good luck, have fun!\n\
69
84
  "
70
85
  else
71
86
  warn "#$0: error: file already exists: #{name}"
72
87
  exit -1
73
88
  end
74
- else
75
- warn "Usage: #$0 PROJECT-NAME"
76
89
  end
data/lib/blossom.rb CHANGED
@@ -7,18 +7,18 @@ require "rack/strip-www"
7
7
  require "hassle"
8
8
 
9
9
  module Blossom
10
- VERSION = "0.0.5"
10
+ VERSION = "0.0.6"
11
11
  end
12
12
 
13
- def Blossom(root_file, index = :index)
13
+ def Blossom(root_file, index = :index, options = {})
14
14
  Rack::Builder.app do
15
15
  use Rack::StripWWW
16
16
  use Hassle
17
- run Blossom::Base(root_file, index)
17
+ run Blossom::Base(root_file, index, options)
18
18
  end
19
19
  end
20
20
 
21
- def Blossom::Base(root_file, index = :index)
21
+ def Blossom::Base(root_file, index = :index, blossom_options = {})
22
22
  root = File.dirname(root_file)
23
23
  Class.new(Sinatra::Base).tap do |app|
24
24
  app.class_eval do
@@ -43,17 +43,51 @@ def Blossom::Base(root_file, index = :index)
43
43
  end
44
44
 
45
45
  get "/" do
46
+ headers blossom_headers
46
47
  haml settings.index
47
48
  end
48
49
 
49
50
  get "/:name.css", :file_exists? => :sass do
51
+ headers blossom_headers
50
52
  content_type "text/css", :charset => "utf-8"
51
53
  sass params[:name].to_sym
52
54
  end
53
55
 
54
56
  get "/:name", :file_exists? => :haml do
57
+ headers blossom_headers
55
58
  haml params[:name].to_sym
56
59
  end
60
+
61
+ define_method :blossom_headers do
62
+ if blossom_options.include? :cache
63
+ seconds = get_seconds(*blossom_options[:cache])
64
+ { 'Cache-Control' => "public, max-age=#{seconds}" }
65
+ else
66
+ {}
67
+ end
68
+ end
69
+
70
+ def get_seconds(value, unit)
71
+ days = 60 * 60 * 24
72
+ case unit
73
+ when :seconds, :second
74
+ value
75
+ when :minutes, :minute
76
+ value * 60
77
+ when :hours, :hour
78
+ value * 60 * 60
79
+ when :days, :day
80
+ value * days
81
+ when :weeks, :week
82
+ value * days * 7
83
+ when :months, :month
84
+ value * days * 30
85
+ when :years, :year
86
+ value * days * 365
87
+ else
88
+ raise ArgumentError, "Unrecognized unit: #{unit}"
89
+ end
90
+ end
57
91
  end
58
92
  end
59
93
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 5
9
- version: 0.0.5
8
+ - 6
9
+ version: 0.0.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Daniel Brockman
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-07 00:00:00 +02:00
17
+ date: 2010-10-28 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency