Class: Rubko::Asset

Inherits:
Controller show all
Defined in:
lib/rubko/asset.rb,
lib/rubko/asset/gzipStream.rb

Overview

in case we want to serve files and get notified on hits

Defined Under Namespace

Classes: FileStream, GzipStream

Instance Attribute Summary collapse

Attributes included from Base

#parent

Instance Method Summary collapse

Methods included from Base

#camelize, #finalize, #httpGet, #initialize, #jsonParse, #loadController, #loadFile, #loadModel, #loadPlugin, #loadView, #method_missing, #uncamelize

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rubko::Base

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



10
11
12
# File 'lib/rubko/asset.rb', line 10

def dir
  @dir
end

#mimeObject (readonly)

Returns the value of attribute mime.



33
34
35
# File 'lib/rubko/asset.rb', line 33

def mime
  @mime
end

#modifiedObject (readonly)

Returns the value of attribute modified.



33
34
35
# File 'lib/rubko/asset.rb', line 33

def modified
  @modified
end

Instance Method Details

#compressible?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
# File 'lib/rubko/asset.rb', line 26

def compressible?
	super && mime != 'application/octet-stream' &&
	['application', 'text'].any? { |type|
		mime.start_with? type+'/'
	}
end

#indexObject



35
36
37
# File 'lib/rubko/asset.rb', line 35

def index
	other
end

#initObject



6
7
8
# File 'lib/rubko/asset.rb', line 6

def init
	@dir = 'public'
end

#other(*path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rubko/asset.rb', line 39

def other(*path)
	# protect private files
	if path.include?('..') || path.include?('.private') && production?
		return miss(*path)
	end

	path.shift if path[0] =~ /^\d*$/
	path = "#{dir}/#{path * '/'}"
	@mime = Rack::Mime.mime_type File.extname(path)
	if File.file? path
		self.mime = mime
		headers['Cache-Control'] = 'public'
		headers['Vary'] = 'Accept-Encoding'

		@modified = File.mtime path
		since = (Time.httpdate(env['HTTP_IF_MODIFIED_SINCE']).to_i rescue 0)
		headers['Last-Modified'] = @modified.httpdate
		headers['Expires'] = (DateTime.now >> 12).httpdate

		if modified.to_i <= since
			cache( *path )
			self.status = 304
			''
		else
			hit( *path )
			require 'rubko/asset/fileStream'
			FileStream.new path
		end
	else
		miss( *path )
	end
end