Class: HttpMagic::Uri
- Inherits:
-
Object
- Object
- HttpMagic::Uri
- 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
-
#domain ⇒ Object
readonly
Returns the value of attribute domain.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#parts ⇒ Object
Returns the value of attribute parts.
Instance Method Summary collapse
-
#build ⇒ Object
Builds a full uniform resource identifier.
-
#initialize(domain) ⇒ Uri
constructor
A new instance of Uri.
-
#url ⇒ Object
Uniform resource locator based on @domain value.
-
#urn ⇒ Object
Uniform resource name for a resource.
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
#domain ⇒ Object (readonly)
Returns the value of attribute domain.
23 24 25 |
# File 'lib/http_magic/uri.rb', line 23 def domain @domain end |
#namespace ⇒ Object
Returns the value of attribute namespace.
21 22 23 |
# File 'lib/http_magic/uri.rb', line 21 def namespace @namespace end |
#parts ⇒ Object
Returns the value of attribute parts.
21 22 23 |
# File 'lib/http_magic/uri.rb', line 21 def parts @parts end |
Instance Method Details
#build ⇒ Object
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 |
#url ⇒ Object
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 |
#urn ⇒ Object
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 |