class Roadie::UrlGenerator
get passed right through, so all URLs could be passed through here.
according to the given options. It is written such as absolute URLs will
URL generation is all about converting relative URLs into absolute URLS
Class that handles URL generation
@api private
def add_scheme(path)
def add_scheme(path) [scheme, path].join(":") end
def apply_base(base, path)
def apply_base(base, path) if path[0] == "/" path else File.join(base, path) end end
def build_root_uri
def build_root_uri path = make_absolute url_options[:path] port = parse_port url_options[:port] URI::Generic.build(scheme: scheme, host: url_options[:host], port: port, path: path) end
def combine_segments(root, base, path)
def combine_segments(root, base, path) new_path = apply_base(base, path) if root.path new_path = File.join(root.path, new_path) end root.merge(new_path) end
def generate_url(path, base = "/")
-
(String)
- an absolute URL
Parameters:
-
base
(String
) -- The base which the relative path comes from
Other tags:
- Example: Conversions with a base -
Example: Normal conversions -
def generate_url(path, base = "/") return root_uri.to_s if path.nil? or 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
def initialize(url_options)
(**url_options)
-
:protocol
(String
) -- alias for :scheme -
:scheme
(String
) -- URL scheme ("http" is default) -
:path
(String
) -- root path -
:port
(String, Integer
) -- -
:host
(String
) --
Parameters:
-
url_options
(Hash
) --
def initialize(url_options) raise ArgumentError, "No URL options were specified" unless url_options raise ArgumentError, "No :host was specified; options are: #{url_options.inspect}" unless url_options[:host] validate_options url_options @url_options = url_options @scheme = normalize_scheme(url_options[:scheme] || url_options[:protocol]) @root_uri = build_root_uri end
def make_absolute(path)
def make_absolute(path) if path.nil? || path[0] == "/" path else "/#{path}" end end
def normalize_scheme(scheme)
def normalize_scheme(scheme) return 'http' unless scheme scheme.to_s[/^\w+/] end
def parse_port(port)
def parse_port(port) (port ? port.to_i : port) end
def path_is_anchor?(path)
def path_is_anchor?(path) path.start_with? '#' end
def path_is_schemeless?(path)
def path_is_schemeless?(path) path =~ %r{^//\w} end
def validate_options(options)
def validate_options(options) keys = Set.new(options.keys) unless keys.subset? VALID_OPTIONS raise ArgumentError, "Passed invalid options: #{(keys - VALID_OPTIONS).to_a}, valid options are: #{VALID_OPTIONS.to_a}" end end