Class: Roadie::UrlGenerator Private
- Inherits:
-
Object
- Object
- Roadie::UrlGenerator
- Defined in:
- lib/roadie/url_generator.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Class that handles URL generation
URL generation is all about converting relative URLs into absolute URLS according to the given options. It is written such as absolute URLs will get passed right through, so all URLs could be passed through here.
Instance Attribute Summary collapse
- #url_options ⇒ Object readonly private
Instance Method Summary collapse
-
#generate_url(path, base = "/") ⇒ String
private
Generate an absolute URL from a relative URL.
-
#initialize(url_options) ⇒ UrlGenerator
constructor
private
Create a new instance with the given URL options.
Constructor Details
#initialize(url_options) ⇒ UrlGenerator
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a new instance with the given URL options.
Initializing without a host setting raises an error, as do unknown keys.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/roadie/url_generator.rb', line 25 def initialize() unless raise ArgumentError, "No URL options were specified" end unless [:host] raise ArgumentError, "No :host was specified; options were: #{.inspect}" end @url_options = @scheme = normalize_scheme([:scheme] || [:protocol]) @root_uri = build_root_uri end |
Instance Attribute Details
#url_options ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
13 14 15 |
# File 'lib/roadie/url_generator.rb', line 13 def @url_options end |
Instance Method Details
#generate_url(path, base = "/") ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generate an absolute URL from a relative URL.
If the passed path is already an absolute URL or just an anchor reference, it will be returned as-is. If passed a blank path, the “root URL” will be returned. The root URL is the URL that the #url_options would generate by themselves.
An optional base can be specified. The base is another relative path from the root that specifies an “offset” from which the path was found in. A common use-case is to convert a relative path found in a stylesheet which resides in a subdirectory.
67 68 69 70 71 72 73 74 |
# File 'lib/roadie/url_generator.rb', line 67 def generate_url(path, base = "/") return root_uri.to_s if path.nil? || path.empty? return path if path_is_anchor?(path) return add_scheme(path) if path_is_schemeless?(path) return path if Utils.path_is_absolute?(path) combine_segments(root_uri, base, path).to_s end |