Class: UrlPlugin

Inherits:
Rubko::Plugin show all
Defined in:
lib/rubko/plugins/url.rb

Instance Attribute Summary collapse

Attributes included from Rubko::Base

#parent

Instance Method Summary collapse

Methods inherited from Rubko::Plugin

#config

Methods included from Rubko::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

#baseObject

Returns the value of attribute base.



32
33
34
# File 'lib/rubko/plugins/url.rb', line 32

def base
  @base
end

#endingObject

Returns the value of attribute ending.



32
33
34
# File 'lib/rubko/plugins/url.rb', line 32

def ending
  @ending
end

#fileBaseObject

Returns the value of attribute fileBase.



32
33
34
# File 'lib/rubko/plugins/url.rb', line 32

def fileBase
  @fileBase
end

#fileTimeObject

Returns the value of attribute fileTime.



32
33
34
# File 'lib/rubko/plugins/url.rb', line 32

def fileTime
  @fileTime
end

#fullPathObject

Returns the value of attribute fullPath.



32
33
34
# File 'lib/rubko/plugins/url.rb', line 32

def fullPath
  @fullPath
end

#hostObject (readonly)

, :newPath



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

def host
  @host
end

#methodObject (readonly)

, :newPath



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

def method
  @method
end

#pathObject

, :newPath



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

def path
  @path
end

#protocolObject (readonly)

, :newPath



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

def protocol
  @protocol
end

Instance Method Details

#ajax?Boolean

Returns:

  • (Boolean)


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

def ajax?
	env["HTTP_X_REQUESTED_WITH"] == 'XMLHttpRequest'
end

#file(*path) ⇒ Object



64
65
66
67
68
# File 'lib/rubko/plugins/url.rb', line 64

def file(*path)
	name = 'public/' + path.join('/')
	time = File.mtime(name).to_i / 3 % 1000000 if File.exists? name
	generate fileBase, (time.to_s if fileTime), *path
end

#forceSSLObject



85
86
87
88
89
90
# File 'lib/rubko/plugins/url.rb', line 85

def forceSSL
	if protocol != 'https://'
		@protocol, @fullPath = 'https://', true
		refresh
	end
end

#generate(*path) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/rubko/plugins/url.rb', line 47

def generate(*path)
	path.compact!
	path.map! { |seg|
		seg = path[seg] if seg.kind_of? Integer
		URI.escape seg
	}
	(protocol+host if fullPath).to_s + base + path.join('/')
end

#initObject



4
5
6
7
8
9
10
# File 'lib/rubko/plugins/url.rb', line 4

def init
	@ending = '/'		# the ending for documents (use .php for amusement)
	@base = '/'			# if project is not top level, set (or use HTML <base>)
	@fileBase = 'files'	# nil to import them globally
	@fileTime = true	# timestamp files for automatic reload
	@fullPath = false	# generate absolute paths
end


56
57
58
# File 'lib/rubko/plugins/url.rb', line 56

def link(*path)
	generate(*path) + (ending unless path.empty?).to_s
end

#linkSlug(*path) ⇒ Object



60
61
62
# File 'lib/rubko/plugins/url.rb', line 60

def linkSlug(*path)
	link( *path.compact.map { |x| slug x } )
end

#newPathObject



23
24
25
26
27
28
29
30
# File 'lib/rubko/plugins/url.rb', line 23

def newPath
	unless @newPath
		@newPath = path * '/'
		config
		@newPath = @newPath.split '/', -1
	end
	@newPath
end

#redirect(*path) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/rubko/plugins/url.rb', line 70

def redirect(*path)
	self.status = 302
	self.headers['Location'] =
	if path.size == 1 && path.first =~ %r{^(https?:)?//}
		path.first
	else
		link(*path)
	end
	''
end

#refreshObject



81
82
83
# File 'lib/rubko/plugins/url.rb', line 81

def refresh
	redirect(*path)
end

#rewrite(from, to) ⇒ Object



39
40
41
# File 'lib/rubko/plugins/url.rb', line 39

def rewrite(from, to)
	@newPath.sub! from, to if from === @newPath
end

#slug(str) ⇒ Object



43
44
45
# File 'lib/rubko/plugins/url.rb', line 43

def slug(str)
	str.strip.downcase.gsub(/[^0-9a-z]+/i, '-').gsub(/^-+|-+$/, '')
end