Class: Lively::Assets
- Inherits:
-
Protocol::HTTP::Middleware
- Object
- Protocol::HTTP::Middleware
- Lively::Assets
- Defined in:
- lib/lively/assets.rb
Constant Summary collapse
- DEFAULT_CACHE_CONTROL =
'public, max-age=3600'
- DEFAULT_CONTENT_TYPES =
{ ".html" => "text/html", ".css" => "text/css", ".js" => "application/javascript", ".png" => "image/png", ".jpeg" => "image/jpeg", ".gif" => "image/gif", }
Instance Method Summary collapse
- #call(request) ⇒ Object
- #expand_path(path) ⇒ Object
- #freeze ⇒ Object
-
#initialize(delegate, root: Dir.pwd, content_types: DEFAULT_CONTENT_TYPES, cache_control: DEFAULT_CACHE_CONTROL) ⇒ Assets
constructor
A new instance of Assets.
- #response_for(path, content_type) ⇒ Object
Constructor Details
#initialize(delegate, root: Dir.pwd, content_types: DEFAULT_CONTENT_TYPES, cache_control: DEFAULT_CACHE_CONTROL) ⇒ Assets
Returns a new instance of Assets.
36 37 38 39 40 41 42 43 |
# File 'lib/lively/assets.rb', line 36 def initialize(delegate, root: Dir.pwd, content_types: DEFAULT_CONTENT_TYPES, cache_control: DEFAULT_CACHE_CONTROL) super(delegate) @root = root @content_types = content_types @cache_control = cache_control end |
Instance Method Details
#call(request) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/lively/assets.rb', line 70 def call(request) if path = (request.path) extension = File.extname(path) content_type = @content_types[extension] if path.start_with?(@root) && File.exist?(path) && content_type return response_for(path, content_type) end end super end |
#expand_path(path) ⇒ Object
64 65 66 67 68 |
# File 'lib/lively/assets.rb', line 64 def (path) File.realpath(File.join(@root, path)) rescue Errno::ENOENT nil end |
#freeze ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/lively/assets.rb', line 45 def freeze return self if frozen? @root.freeze @content_types.freeze @cache_control.freeze super end |
#response_for(path, content_type) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/lively/assets.rb', line 55 def response_for(path, content_type) headers = [ ['content-type', content_type], ['cache-control', @cache_control], ] return Protocol::HTTP::Response[200, headers, Protocol::HTTP::Body::File.open(path)] end |