Class: TinySite

Inherits:
Object
  • Object
show all
Defined in:
lib/tiny_site.rb,
lib/tiny_site.rb,
lib/tiny_site/version.rb

Defined Under Namespace

Classes: View

Constant Summary collapse

VERSION =
"0.2.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ TinySite

Returns a new instance of TinySite.



88
89
90
91
92
93
94
# File 'lib/tiny_site.rb', line 88

def initialize(opts)
  @file_path         = opts[:file_path]
  @file_path_postfix = opts[:file_path_postfix] || ''
  @file_extension    = opts[:file_extension]    || 'textile'
  @image_path        = opts[:image_path]        || File.join(@file_path, 'images')
  @cache_buster      = opts[:cache_buster]      || 'bust'
end

Instance Attribute Details

#file_extensionObject (readonly)

Returns the value of attribute file_extension.



86
87
88
# File 'lib/tiny_site.rb', line 86

def file_extension
  @file_extension
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



86
87
88
# File 'lib/tiny_site.rb', line 86

def file_path
  @file_path
end

#file_path_postfixObject (readonly)

Returns the value of attribute file_path_postfix.



86
87
88
# File 'lib/tiny_site.rb', line 86

def file_path_postfix
  @file_path_postfix
end

#image_pathObject (readonly)

Returns the value of attribute image_path.



86
87
88
# File 'lib/tiny_site.rb', line 86

def image_path
  @image_path
end

#query_stringObject (readonly)

Returns the value of attribute query_string.



86
87
88
# File 'lib/tiny_site.rb', line 86

def query_string
  @query_string
end

#request_pathObject (readonly)

Returns the value of attribute request_path.



86
87
88
# File 'lib/tiny_site.rb', line 86

def request_path
  @request_path
end

Instance Method Details

#bodyObject



124
125
126
# File 'lib/tiny_site.rb', line 124

def body
  Haml::Engine.new(File.open("#{layout}.haml").read, :format => :html5).render view
end

#caching_headerObject



128
129
130
131
132
# File 'lib/tiny_site.rb', line 128

def caching_header
  return { 'Cache-Control' => 'public, max-age=3600' } unless @query_string == @cache_buster
  
  CachedHttpFile.bust and return { 'Cache-Control' => 'no-cache' }
end

#call(env) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/tiny_site.rb', line 142

def call(env)
  @request_path, @query_string = env['PATH_INFO'], env['QUERY_STRING']
  
  return [301, {'Location' => @request_path}, ['']] if @request_path.gsub!(/(.)\/$/,'\\1')
  
  [status, headers, [body]]
rescue => e
  puts "#{e.class}: #{e.message} #{e.backtrace}"
  [500, {}, ['Sorry, but something went wrong']]
end

#file_url_for(filename, path = file_path) ⇒ Object



114
115
116
# File 'lib/tiny_site.rb', line 114

def file_url_for(filename, path=file_path)
  File.join path, "#{filename}#{file_path_postfix}"
end

#globalObject



96
97
98
99
# File 'lib/tiny_site.rb', line 96

def global
        _, global_file = CachedHttpFile.get page_content_url_for('__global')
  TextileParts.parse global_file, self
end

#headersObject



134
135
136
# File 'lib/tiny_site.rb', line 134

def headers
  caching_header.merge({'Content-Type' => 'text/html'})
end

#image_url_for(img_name) ⇒ Object



111
112
113
# File 'lib/tiny_site.rb', line 111

def image_url_for(img_name)
  file_url_for img_name, @image_path
end

#layoutObject



121
122
123
# File 'lib/tiny_site.rb', line 121

def layout
  page[:layout] || global[:layout] || 'layout'
end

#pageObject



101
102
103
104
105
# File 'lib/tiny_site.rb', line 101

def page
  @status, page_file   = CachedHttpFile.get page_content_url_for(@request_path)
        _, page_file   = CachedHttpFile.get page_content_url_for(@status.to_s)        if @status != 200
  TextileParts.parse page_file, self
end

#page_content_url_for(filename) ⇒ Object



117
118
119
# File 'lib/tiny_site.rb', line 117

def page_content_url_for(filename)
  file_url_for "#{filename.gsub(%r{^/$},'/index')}.#{file_extension}"
end

#statusObject



107
108
109
# File 'lib/tiny_site.rb', line 107

def status
  @status or page && @status
end

#viewObject



138
139
140
# File 'lib/tiny_site.rb', line 138

def view
  @view ||= View.new self
end