Class: ApiBlueprint::Url

Inherits:
Object
  • Object
show all
Defined in:
lib/api-blueprint/url.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base = "", custom = "") ⇒ Url

Returns a new instance of Url.



5
6
7
8
# File 'lib/api-blueprint/url.rb', line 5

def initialize(base = "", custom = "")
  self.base = base
  self.custom = custom
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



3
4
5
# File 'lib/api-blueprint/url.rb', line 3

def base
  @base
end

#customObject

Returns the value of attribute custom.



3
4
5
# File 'lib/api-blueprint/url.rb', line 3

def custom
  @custom
end

Instance Method Details

#to_sObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/api-blueprint/url.rb', line 18

def to_s
  if base.path.present? && custom.path.present? && !custom.host.present?
    # Join paths in a permissive way which handles extra slashes gracefully and returns
    # a string which Addressable can handle when joining with other paths
    paths = [base.path, custom.path].compact.map { |path| path.gsub(%r{^/*(.*?)/*$}, '\1') }.join("/")
    Addressable::URI.join(base.site, paths).to_s
  else
    base.join(custom).to_s
  end
end