Sha256: 12144e6ab436c922776b4853bd28ce653230d9f6aa86b147585f62935985897e

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

module Nucleus
  class LinkGenerator
    def initialize(env, api_version)
      @env = env
      @version = api_version
    end

    # Generate the link that references the resource.
    # @param [Array<String>] namespaces nested namespaces that must be joined to access the resource
    # @param [String] id id of the resource
    # @return [String] URL to the resource
    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

    # Create a link to the API root node.
    #
    # @return [String] link to the API root
    def api_root
      root_url << '/api'
    end

    # Get the root URL of the Nucleus API (scheme + host)
    def root_url
      "#{@env['rack.url_scheme']}://#{@env['HTTP_HOST']}"
    end

    private

    def namespace(namespaces)
      if namespaces.is_a?(String) && !namespaces.empty?
        "/#{namespaces}"
      elsif !namespaces.nil? && !namespaces.empty?
        "/#{namespaces.join('/')}"
      else
        ''
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nucleus-0.3.1 lib/nucleus/core/common/link_generator.rb
nucleus-0.2.0 lib/nucleus/core/common/link_generator.rb
nucleus-0.1.0 lib/nucleus/core/common/link_generator.rb