Class: TLAW::Namespace

Inherits:
APIPath show all
Defined in:
lib/tlaw/namespace.rb

Overview

Namespace is basically a container for Endpoints. It allows to nest Ruby calls (like api.namespace1.namespace2.real_call(params)), optionally providing some parameters while nesting, like worldbank.countries('uk').population(2016).

By default, namespaces nesting also means URL nesting (e.g. base_url/namespace1/namespace2/endpoint), but that could be altered on namespace definition, see DSL module for details.

Typically, as with Endpoint, you never create namespace instances or subclasses by yourself: you use DSL for their definition and then call .<namespace_name> method on parent namespace (or API instance):

class SampleAPI < TLAW::API
  # namespace definition:
  namespace :my_ns do
    endpoint :weather
  end
end

# usage:
api = SampleAPI.new

api.namespaces[:my_ns] # => class SampleAPI::MyNS, subclass of namespace
api.my_ns # => short-living instance of SampleAPI::MyNS
api.my_ns.weather # => real call to API

Direct Known Subclasses

API

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from APIPath

#initialize, param_set

Constructor Details

This class inherits a constructor from TLAW::APIPath

Class Method Details

.describe(definition = nil) ⇒ Util::Description

Detailed namespace documentation.

See APIPath.describe for explanations.

Returns:



93
94
95
# File 'lib/tlaw/namespace.rb', line 93

def describe(definition = nil)
  super + describe_children
end

.endpointsHash{Symbol => Endpoint}

Lists all current namespace's endpoints as a hash.

Returns:



52
53
54
# File 'lib/tlaw/namespace.rb', line 52

def endpoints
  children.select { |_k, v| v < Endpoint }
end

.inspectObject



63
64
65
66
67
# File 'lib/tlaw/namespace.rb', line 63

def inspect
  "#<#{name || '(unnamed namespace class)'}: " \
  "call-sequence: #{symbol}(#{param_set.to_code});" +
    inspect_docs
end

.namespacesHash{Symbol => Namespace}

Lists all current namespace's nested namespaces as a hash.

Returns:



45
46
47
# File 'lib/tlaw/namespace.rb', line 45

def namespaces
  children.select { |_k, v| v < Namespace }
end

Instance Method Details

#describeObject



141
142
143
144
# File 'lib/tlaw/namespace.rb', line 141

def describe
  self.class
      .describe("#{symbol}(#{param_set.to_hash_code(@parent_params)})")
end

#inspectObject



136
137
138
139
# File 'lib/tlaw/namespace.rb', line 136

def inspect
  "#<#{symbol}(#{param_set.to_hash_code(@parent_params)})" +
    self.class.inspect_docs
end