Class: Nucleus::LinkGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/nucleus/core/common/link_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(env, api_version) ⇒ LinkGenerator

Returns a new instance of LinkGenerator.



3
4
5
6
# File 'lib/nucleus/core/common/link_generator.rb', line 3

def initialize(env, api_version)
  @env = env
  @version = api_version
end

Instance Method Details

#api_rootString

Create a link to the API root node.

Returns:

  • (String)

    link to the API root



25
26
27
# File 'lib/nucleus/core/common/link_generator.rb', line 25

def api_root
  root_url << '/api'
end

#resource(namespaces, id) ⇒ String

Generate the link that references the resource.

Parameters:

  • namespaces (Array<String>)

    nested namespaces that must be joined to access the resource

  • id (String)

    id of the resource

Returns:

  • (String)

    URL to the resource



12
13
14
15
16
17
18
19
20
# File 'lib/nucleus/core/common/link_generator.rb', line 12

def resource(namespaces, id)
  # resource can only exist for an API version
  link = api_root
  # combine namespace and entity ID
  link << namespace(namespaces)
  link << "/#{id}" unless id.nil? || id.empty?
  # return the created link
  link
end

#root_urlObject

Get the root URL of the Nucleus API (scheme + host)



30
31
32
# File 'lib/nucleus/core/common/link_generator.rb', line 30

def root_url
  "#{@env['rack.url_scheme']}://#{@env['HTTP_HOST']}"
end