Class: Roadie::UrlGenerator Private

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • url_options (Hash)

Options Hash (url_options):

  • :host (String) — default: required
  • :port (String, Integer)
  • :path (String)

    root path

  • :scheme (String)

    URL scheme (“http” is default)

  • :protocol (String)

    alias for :scheme

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
# File 'lib/roadie/url_generator.rb', line 23

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
  @root_uri = build_root_uri
end

Instance Attribute Details

#url_optionsObject (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.



11
12
13
# File 'lib/roadie/url_generator.rb', line 11

def url_options
  @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, 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.

Examples:

Normal conversions

generator = Roadie::UrlGenerator.new host: "foo.com", scheme: "https"
generator.generate_url("bar.html") # => "https://foo.com/bar.html"
generator.generate_url("/bar.html") # => "https://foo.com/bar.html"
generator.generate_url("") # => "https://foo.com"

Conversions with a base

generator = Roadie::UrlGenerator.new host: "foo.com", scheme: "https"
generator.generate_url("../images/logo.png", "/css") # => "https://foo.com/images/logo.png"
generator.generate_url("../images/logo.png", "/assets/css") # => "https://foo.com/assets/images/logo.png"

Parameters:

  • base (String) (defaults to: "/")

    The base which the relative path comes from

Returns:

  • (String)

    an absolute URL



56
57
58
59
60
61
# File 'lib/roadie/url_generator.rb', line 56

def generate_url(path, base = "/")
  return root_uri.to_s if path.nil? or path.empty?
  return path if path_is_absolute?(path)

  combine_segments(root_uri, base, path).to_s
end