Module: Padrino::Routing::InstanceMethods
- Defined in:
- padrino-core/lib/padrino-core/application/routing.rb
Overview
Instance methods related to recognizing and processing routes and serving static files.
Instance Method Summary collapse
-
#absolute_url(*args) ⇒ Object
Returns absolute url.
-
#content_type(type = nil, params = {}) ⇒ Object
Return the request format, this is useful when we need to respond to a given Content-Type.
-
#current_path(*path_params) ⇒ Object
Returns the current path within a route from specified
path_params
. - #recognize_path(path) ⇒ Object
-
#route ⇒ Object
Returns the current route.
-
#static!(options = {}) ⇒ Object
Method for deliver static files.
-
#static_file?(path_info) ⇒ Boolean
This is mostly just a helper so request.path_info isn’t changed when serving files from the public directory.
-
#url(*args) ⇒ Object
(also: #url_for)
Instance method for URL generation.
Instance Method Details
#absolute_url(*args) ⇒ Object
Returns absolute url. Calls Sinatra::Helpers#uri to generate protocol version, hostname and port.
818 819 820 821 822 823 824 |
# File 'padrino-core/lib/padrino-core/application/routing.rb', line 818 def absolute_url(*args) url_path = args.shift if url_path.is_a?(String) && !url_path.start_with?('/') url_path = request.env['PATH_INFO'].rpartition('/').first << '/' << url_path end uri url(url_path, *args), true, false end |
#content_type(type = nil, params = {}) ⇒ Object
Return the request format, this is useful when we need to respond to a given Content-Type.
896 897 898 899 900 |
# File 'padrino-core/lib/padrino-core/application/routing.rb', line 896 def content_type(type=nil, params={}) return @_content_type unless type super(type, params) @_content_type = type end |
#current_path(*path_params) ⇒ Object
Returns the current path within a route from specified path_params
.
833 834 835 836 837 838 839 840 841 842 |
# File 'padrino-core/lib/padrino-core/application/routing.rb', line 833 def current_path(*path_params) if path_params.last.is_a?(Hash) path_params[-1] = params.merge(path_params[-1]) else path_params << params end path_params[-1] = Utils.symbolize_keys(path_params[-1]) @route.path(*path_params) end |
#recognize_path(path) ⇒ Object
826 827 828 |
# File 'padrino-core/lib/padrino-core/application/routing.rb', line 826 def recognize_path(path) settings.recognize_path(path) end |
#route ⇒ Object
Returns the current route
851 852 853 |
# File 'padrino-core/lib/padrino-core/application/routing.rb', line 851 def route @route end |
#static!(options = {}) ⇒ Object
Method for deliver static files.
871 872 873 874 875 876 877 |
# File 'padrino-core/lib/padrino-core/application/routing.rb', line 871 def static!( = {}) if path = static_file?(request.path_info) env['sinatra.static_file'] = path cache_control(*settings.static_cache_control) if settings.static_cache_control? send_file(path, ) end end |
#static_file?(path_info) ⇒ Boolean
This is mostly just a helper so request.path_info isn’t changed when serving files from the public directory.
859 860 861 862 863 864 865 866 |
# File 'padrino-core/lib/padrino-core/application/routing.rb', line 859 def static_file?(path_info) return unless public_dir = settings.public_folder public_dir = File.(public_dir) path = File.(public_dir + unescape(path_info)) return unless path.start_with?(public_dir) return unless File.file?(path) path end |
#url(*args) ⇒ Object Also known as: url_for
Instance method for URL generation.
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 |
# File 'padrino-core/lib/padrino-core/application/routing.rb', line 792 def url(*args) if args.first.is_a?(String) url_path = settings.rebase_url(args.shift) if args.empty? url_path else # Delegate sinatra-style urls to Sinatra. Ex: url("/foo", false, false) # http://www.sinatrarb.com/intro#Generating%20URLs super url_path, *args end else # Delegate to Padrino named route URL generation. settings.url(*args) end end |