Class: Web::UrlBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/services/web/url_builder.rb

Overview

Service object: url builder.

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ UrlBuilder

Returns a new instance of UrlBuilder.

Parameters:

  • options (Hash, nil) (defaults to: nil)

    URL defaults

Options Hash (options):

  • :default_host (String)

    host applied when the URL has none

  • :default_scheme (String)

    scheme applied when the URL has none

  • :default_locale (String, Symbol)

    locale injected into the path
    when no +force_locale+ is given

  • :default_port (Integer, String)

    port applied when none is given



11
12
13
# File 'app/services/web/url_builder.rb', line 11

def initialize(options = nil)
  @options = options || {}
end

Instance Method Details

#default_hostObject



15
16
17
# File 'app/services/web/url_builder.rb', line 15

def default_host
  @options[:default_host]
end

#default_localeObject



23
24
25
# File 'app/services/web/url_builder.rb', line 23

def default_locale
  @options[:default_locale]
end

#default_portObject



27
28
29
# File 'app/services/web/url_builder.rb', line 27

def default_port
  @options[:default_port]
end

#default_schemeObject



19
20
21
# File 'app/services/web/url_builder.rb', line 19

def default_scheme
  @options[:default_scheme]
end

#process(url, force_locale = nil, parameters: {}, anchor: nil, host: nil, scheme: nil, port: nil, strip_params: false, relative: false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/services/web/url_builder.rb', line 31

def process(url, force_locale = nil, parameters: {}, anchor: nil, host: nil, scheme: nil, port: nil, strip_params: false, relative: false)
  return nil if url.blank?

  port = port.to_s.scan(/\d/).join.to_i if port.present?
  port ||= default_port
  # Establish the possible locales
  valid_locales = I18n.available_locales.map(&:to_s) - %i[en es]
  # Validate the force_locale specified
  force_locale ||= default_locale
  raise "cms_link force_locale specifies an invalid locale #{force_locale}, must be one of #{valid_locales.join(',')} or :delocalized" if force_locale.present? && valid_locales.exclude?(force_locale.to_s) && (force_locale != :delocalized)

  # Store path
  uri = Addressable::URI.parse(url.to_s.squish)

  if relative
    uri.host = nil
    uri.scheme = nil
    uri.port = nil
  else
    uri.host = host if host.present?
    uri.host ||= default_host
    uri.scheme = scheme if scheme.present?
    uri.scheme ||= default_scheme
    uri.scheme ||= 'https' if uri.host.present?
    uri.port = port.to_i if port.present? && port.to_i.positive? && !port.to_i.in?([443, 80])
  end
  # Rails.logger.debug "[cms_link] #{url} parsed as #{uri.inspect}"
  # Analyze path, first cleanup
  path_parts = uri.path.split('/').filter_map(&:presence)
  # Remove the locale
  path_parts.reject! { |pp| valid_locales.include? pp }
  # (Re)inject the locale unless we don't want it. Coerce to a locale the
  # site actually routes (en-US / en-CA) so a non-request context — where
  # I18n.locale is the framework default :en — can never fabricate an
  # unroutable /en/ prefix.
  path_parts.insert(0, service_locale(force_locale || I18n.locale)) unless force_locale == :delocalized
  # Rebuild our path
  uri.path = "/#{path_parts.compact.join('/')}".chomp('/').presence

  # Merge inbound params with passed params
  merged_params = Rack::Utils.parse_nested_query uri.query
  merged_params = (merged_params || {}).with_indifferent_access
  merged_params.merge!(parameters) if parameters

  # Sometime we don't want the param, in case of canonical for instance
  if strip_params && parameters.blank? # If we specified parameters we ignore this directive
    # The only one we preserve is page, if we want more later we can add another parameter to this function
    merged_params.slice!(:page)
  end
  uri.query = (merged_params.presence&.to_query)
  uri.fragment = anchor if anchor.present?

  uri.to_s
end