Class: HttpMagic::Uri

Inherits:
Object
  • Object
show all
Defined in:
lib/http_magic/uri.rb

Overview

Helper class that holds the parts of the URI and provides methods to put them together.

Example

uri = HttpMagic::Uri.new('example.com')
uri.build
=> "https://example.com/"

uri.namespace = 'api/v2'
uri.build
=> "https://example.com/api/v2"

uri.parts = ['path']
uri.build
=> "https://example.com/api/v2/path"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain) ⇒ Uri

Returns a new instance of Uri.



25
26
27
28
# File 'lib/http_magic/uri.rb', line 25

def initialize(domain)
  @domain = domain
  @parts = []
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



23
24
25
# File 'lib/http_magic/uri.rb', line 23

def domain
  @domain
end

#namespaceObject

Returns the value of attribute namespace.



21
22
23
# File 'lib/http_magic/uri.rb', line 21

def namespace
  @namespace
end

#partsObject

Returns the value of attribute parts.



21
22
23
# File 'lib/http_magic/uri.rb', line 21

def parts
  @parts
end

Instance Method Details

#buildObject

Builds a full uniform resource identifier.

Example

uri = HttpMagic::Uri.new('example.com')
uri.namespace = 'api/v1'
uri.parts = %w(foo bar)

uri.urn
=> "https://example.com/api/v1/foo/bar"


40
41
42
# File 'lib/http_magic/uri.rb', line 40

def build
  "#{url}#{urn}"
end

#urlObject

Uniform resource locator based on @domain value.

Example

uri = HttpMagic::Uri.new('example.com')

uri.url
=> "https://example.com/"

uri.url(false)
=> "https://example.com"


55
56
57
# File 'lib/http_magic/uri.rb', line 55

def url
  "https://#{@domain}"
end

#urnObject

Uniform resource name for a resource.

Example

uri = HttpMagic::Uri.new('example.com')
uri.namespace = 'api/v1'
uri.parts = %w(foo bar)

uri.urn
=> "/api/v1/foo/bar"


69
70
71
72
# File 'lib/http_magic/uri.rb', line 69

def urn
  resource_name = [@namespace, @parts].flatten.compact.join('/')
  "/#{resource_name}"
end