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



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(url_options)
  unless url_options
    raise ArgumentError, "No URL options were specified"
  end

  unless url_options[:host]
    raise ArgumentError,
      "No :host was specified; options were: #{url_options.inspect}"
  end

  validate_options url_options

  @url_options = url_options
  @scheme = normalize_scheme(url_options[:scheme] || url_options[:protocol])
  @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.



13
14
15
# File 'lib/roadie/url_generator.rb', line 13

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 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.

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



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