Module: UrlHelper

Defined in:
app/helpers/url_helper.rb

Overview

Instance Method Summary collapse

Instance Method Details

#base_domain(absolute: false) ⇒ Object


26
27
28
29
30
31
32
# File 'app/helpers/url_helper.rb', line 26

def base_domain(absolute: false)
  scheme = nil
  if absolute
    scheme = Config.force_ssl ? 'https://' : 'http://'
  end
  "#{scheme}#{Config.domain}"
end

#base_hrefObject

FIXME: This should/could use a context info or request.script_name if possible


35
36
37
# File 'app/helpers/url_helper.rb', line 35

def base_href
  '/api/v1'
end

#path_for(url_fragment, **params) ⇒ Object

Construct an absolute path to a given +url_fragment+, expects a leading slash.


8
9
10
11
12
13
14
15
16
17
18
# File 'app/helpers/url_helper.rb', line 8

def path_for(url_fragment, **params)
  uri = URI.parse(url_fragment)

  # TODO: Use addressable and refactor this ugly sh*t!
  query = params.map { |k,v|
    "#{k}=#{URI.escape(v.to_s, /[^#{URI::PATTERN::UNRESERVED}]/)}"
  }.unshift(uri.query).compact.join('&')
  uri.query = query unless query.empty?

  url_fragment.start_with?(base_href) ? uri.to_s : "#{base_href}#{uri}"
end

#url_for(url_fragment, **params) ⇒ Object

Construct a full URL to url_fragment, which should be given relative to the base of an API.


22
23
24
# File 'app/helpers/url_helper.rb', line 22

def url_for(url_fragment, **params)
  "#{base_domain(absolute: true)}#{path_for url_fragment, params}"
end