Class: Talis::Hierarchy::Node

Inherits:
Resource show all
Extended by:
OAuthService
Includes:
Resource
Defined in:
lib/talis/hierarchy/node.rb

Overview

Represents hierarchy node API operations provided by the Blueprint gem: https://github.com/talis/blueprint_rb

In order to perform remote operations, the client must be configured with a valid OAuth client that is allowed to query nodes:

Talis::Authentication.client_id = 'client_id'
Talis::Authentication.client_secret = 'client_secret'

Instance Attribute Summary

Attributes included from OAuthService

#client_id, #client_secret, #oauth_host

Attributes included from Resource::Helpers

#attributes, #id, #namespace, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource

included

Methods included from Resource::Helpers

#persisted?, #stored_id, #stored_type

Methods inherited from Resource

handle_response, new_req_id

Constructor Details

#initialize(id:, type:, namespace:, attributes: {}) ⇒ Node

Returns a new instance of Node.



20
21
22
23
24
25
# File 'lib/talis/hierarchy/node.rb', line 20

def initialize(id:, type:, namespace:, attributes: {})
  @id = id
  @type = type
  @namespace = namespace
  @attributes = attributes
end

Class Method Details

.ancestors(request_id: new_req_id, namespace:, type:, id:, opts: {}) ⇒ Object

Fetch all ancestors belonging to the specified node from the hierarchy for the given namespace.

Parameters:

  • request_id (String) (defaults to: new_req_id)

    (‘uuid’) unique ID for the remote request.

  • namespace (String)

    the namespace of the hierarchy.

  • type (String)

    the type of node whose ancestors are to be fetched.

  • id (String)

    the ID of the node whose ancestors are to be fetched.

  • opts (Hash) (defaults to: {})

Returns:

  • BlueprintClient::Node or nil if the node cannot be found.

Raises:



126
127
128
129
130
# File 'lib/talis/hierarchy/node.rb', line 126

def ancestors(request_id: new_req_id, namespace:, type:, id:, opts: {})
  api_client(request_id).get_ancestors(id, namespace, type, opts).data
rescue BlueprintClient::ApiError => error
  handle_response(error)
end

.api_client(request_id = new_req_id) ⇒ Object

Exposes the underlying Blueprint nodes API client.

Parameters:

  • request_id (String) (defaults to: new_req_id)

    (‘uuid’) unique ID for remote requests.

Returns:

  • BlueprintClient::HierarchyApi



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/talis/hierarchy/node.rb', line 178

def api_client(request_id = new_req_id)
  configure_blueprint

  api_client = BlueprintClient::ApiClient.new
  api_client.default_headers = {
    'X-Request-Id' => request_id,
    'User-Agent' => "talis-ruby-client/#{Talis::VERSION} "\
    "ruby/#{RUBY_VERSION}"
  }

  BlueprintClient::HierarchyApi.new(api_client)
end

.children(request_id: new_req_id, namespace:, type:, id:, opts: {}) ⇒ Object

Fetch all children belonging to the specified node from the hierarchy for the given namespace.

Parameters:

  • request_id (String) (defaults to: new_req_id)

    (‘uuid’) unique ID for the remote request.

  • namespace (String)

    the namespace of the hierarchy.

  • type (String)

    the type of node whose children are to be fetched.

  • id (String)

    the ID of the node whose children are to be fetched.

  • opts (Hash) (defaults to: {})

Returns:

  • BlueprintClient::Node or nil if the node cannot be found.

Raises:



105
106
107
108
109
# File 'lib/talis/hierarchy/node.rb', line 105

def children(request_id: new_req_id, namespace:, type:, id:, opts: {})
  api_client(request_id).get_children(id, namespace, type, opts).data
rescue BlueprintClient::ApiError => error
  handle_response(error)
end

.create(request_id: new_req_id, namespace:, type:, id:, attributes: {}) ⇒ Talis::Hierarchy::Node

Create a new node

Parameters:

Returns:



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/talis/hierarchy/node.rb', line 161

def create(request_id: new_req_id, namespace:, type:, id:, attributes: {})
  new_node = {
    data: {
      id: id,
      type: type,
      attributes: attributes
    }
  }

  build(api_client(request_id).add_node(namespace, new_node, {}).data, namespace)
rescue BlueprintClient::ApiError => error
  handle_response(error)
end

.descendants(request_id: new_req_id, namespace:, type:, id:, opts: {}) ⇒ Object

Fetch all descendants belonging to the specified node from the hierarchy for the given namespace.

Parameters:

  • request_id (String) (defaults to: new_req_id)

    (‘uuid’) unique ID for the remote request.

  • namespace (String)

    the namespace of the hierarchy.

  • type (String)

    the type of node whose descendants are to be fetched.

  • id (String)

    the ID of the node whose descendants are to be fetched.

  • opts (Hash) (defaults to: {})

Returns:

  • BlueprintClient::Node or nil if the node cannot be found.

Raises:



147
148
149
150
151
# File 'lib/talis/hierarchy/node.rb', line 147

def descendants(request_id: new_req_id, namespace:, type:, id:, opts: {})
  api_client(request_id).get_descendants(id, namespace, type, opts).data
rescue BlueprintClient::ApiError => error
  handle_response(error)
end

.find(request_id: new_req_id, namespace:, opts: {}) ⇒ Hash

Search for nodes in the hierarchy for the given namespace.

where nodes are of type BlueprintClient::Node

Parameters:

Returns:

  • (Hash)

    containing data and meta attributes. The structure as follows:

    {
      data: [node1, node2, node3, node4, node5],
      meta: { offset: 0, count: 20, limit: 5 }
    }
    

Raises:



45
46
47
48
49
# File 'lib/talis/hierarchy/node.rb', line 45

def find(request_id: new_req_id, namespace:, opts: {})
  api_client(request_id).search_nodes(namespace, opts)
rescue BlueprintClient::ApiError => error
  handle_response(error)
end

.get(request_id: new_req_id, namespace:, type:, id:) ⇒ Object

Fetch a single node from the hierarchy for the given namespace.

Parameters:

  • request_id (String) (defaults to: new_req_id)

    (‘uuid’) unique ID for the remote request.

  • namespace (String)

    the namespace of the hierarchy.

  • type (String)

    the type of node to fetch.

  • id (String)

    the ID of the node to fetch.

Returns:

  • BlueprintClient::Node or nil if the node cannot be found.

Raises:



61
62
63
64
65
66
67
68
69
# File 'lib/talis/hierarchy/node.rb', line 61

def get(request_id: new_req_id, namespace:, type:, id:)
  api_client(request_id).get_node(namespace, id, type).data
rescue BlueprintClient::ApiError => error
  begin
    handle_response(error)
  rescue Talis::NotFoundError
    nil
  end
end

.parents(request_id: new_req_id, namespace:, type:, id:, opts: {}) ⇒ Object

Fetch all parents belonging to the specified node from the hierarchy for the given namespace.

Parameters:

  • request_id (String) (defaults to: new_req_id)

    (‘uuid’) unique ID for the remote request.

  • namespace (String)

    the namespace of the hierarchy.

  • type (String)

    the type of node whose parents are to be fetched.

  • id (String)

    the ID of the node whose parents are to be fetched.

  • opts (Hash) (defaults to: {})

Returns:

  • BlueprintClient::Node or nil if the node cannot be found.

Raises:



84
85
86
87
88
# File 'lib/talis/hierarchy/node.rb', line 84

def parents(request_id: new_req_id, namespace:, type:, id:, opts: {})
  api_client(request_id).get_parents(id, namespace, type, opts).data
rescue BlueprintClient::ApiError => error
  handle_response(error)
end