Class: Talis::Hierarchy::Asset

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

Overview

Represents hierarchy asset 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 assets:

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

Examples:

Create an asset with attributes.

node_options = {
  namespace: 'mynamespace',
  type: 'module',
  id: '1'
}
node = Talis::Hierarchy::Node.get(node_options)
asset_options = {
  namespace: 'mynamespace',
  type: 'list',
  id: '1',
  nodes: [ node ]
}
asset = Talis::Hierarchy::Asset.new(asset_options)
asset.save # Will raise an exception if this fails
asset.attributes = { attr_key: 'my_attr_value' }
asset.update

Create an asset and associate it with multiple nodes.

node1_options = {
  namespace: 'mynamespace',
  type: 'module',
  id: '1'
}
node2_options = {
  namespace: 'mynamespace',
  type: 'module',
  id: '2'
}
node1 = Talis::Hierarchy::Node.get(node1_options)
node2 = Talis::Hierarchy::Node.get(node2_options)
asset_options = {
  namespace: 'mynamespace',
  type: 'list',
  id: '1',
  nodes: [ node1, node2 ]
}
asset = Talis::Hierarchy::Asset.new(asset_options)
asset.save

Instance Attribute Summary collapse

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(namespace:, type:, id:, nodes: [], attributes: {}) ⇒ Asset

Create a non-persisted asset.

Parameters:

  • namespace (String)

    the namespace of the hierarchy.

  • type (String)

    the type of asset.

  • id (String)

    the ID of the asset.

  • nodes (Array<BlueprintClient::Node>) (defaults to: [])

    an array of nodes an asset can belong to.

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

    ({}) key-value pair attributes belonging to the asset.



70
71
72
73
74
75
76
77
# File 'lib/talis/hierarchy/asset.rb', line 70

def initialize(namespace:, type:, id:, nodes: [], attributes: {})
  @namespace = namespace
  @id = id
  @type = type
  @nodes = nodes
  @attributes = attributes
  @new_resource = true
end

Instance Attribute Details

#nodesArray<BlueprintClient::Node>

Returns An array of nodes an asset can belong to.

Returns:

  • (Array<BlueprintClient::Node>)

    An array of nodes an asset can belong to.



60
61
62
# File 'lib/talis/hierarchy/asset.rb', line 60

def nodes
  @nodes
end

Class Method Details

.all(request_id: new_req_id, namespace:) ⇒ Array<Talis::Hierarchy::Asset>

Return all assets in 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.

Returns:

Raises:



211
212
213
# File 'lib/talis/hierarchy/asset.rb', line 211

def all(request_id: new_req_id, namespace:)
  where(request_id: request_id, namespace: namespace)
end

.api_client(request_id = new_req_id) ⇒ Object

Exposes the underlying Blueprint assets API client.

Parameters:

  • request_id (String) (defaults to: new_req_id)

    (‘uuid’) unique ID for remote requests.

Returns:

  • BlueprintClient::AssetsApi



235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/talis/hierarchy/asset.rb', line 235

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::AssetsApi.new(api_client)
end

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

Fetch a single asset 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 asset to fetch.

  • id (String)

    the ID of the asset to fetch.

Returns:

  • Talis::Hierarchy::Asset or nil if the asset cannot be found.

Raises:



225
226
227
228
229
230
# File 'lib/talis/hierarchy/asset.rb', line 225

def find(request_id: new_req_id, namespace:, type:, id:)
  data = api_client(request_id).get_asset(namespace, type, id).data
  build(data, namespace)
rescue BlueprintClient::ApiError => error
  handle_blueprint_api_error(error, false)
end

.find_by_node(request_id: new_req_id, namespace:, type:, id:, opts: {}) ⇒ Array<Talis::Hierarchy::Asset>

Search for assets in the hierarchy for the given namespace and node.

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 the assets belong to.

  • id (String)

    the ID of the node the assets belong to.

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

Returns:

Raises:



162
163
164
165
166
167
168
# File 'lib/talis/hierarchy/asset.rb', line 162

def find_by_node(request_id: new_req_id, namespace:, type:, id:, opts: {})
  data = api_client(request_id).get_assets_in_node(namespace, type,
                                                   id, opts).data
  data.map! { |asset| build(asset, namespace) }
rescue BlueprintClient::ApiError => error
  handle_blueprint_api_error(error)
end

.where(request_id: new_req_id, namespace:, opts: {}) ⇒ Array<Talis::Hierarchy::Asset>

Search for assets in 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.

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

    ({}) optional filter and pagination criteria. see https://github.com/talis/blueprint_rb/blob/master/docs/AssetsApi.md#search_assets One exception to the options listed above are filters which take the following format, for example: Return assets that are related to any nodes specified in the array.

    filter_node: ['type/id', 'type/id']
    

    As above but explicitly requesting an any match:

    filter_node: { any: ['type/id', 'type/id'] }
    

    Return assets that are related to all nodes specified in the array.

    filter_node: { all: ['type/id', 'type/id'] }
    

Returns:

Raises:



193
194
195
196
197
198
199
200
201
# File 'lib/talis/hierarchy/asset.rb', line 193

def where(request_id: new_req_id, namespace:, opts: {})
  # search_assets method in resource module uses HTTParty not
  # blueprint_rb to call the API to get around the Swagger limitation
  # of not being able to choose between AND and IN queries.
  data = search_assets(request_id, namespace, opts)
  data.map! { |asset| build(asset, namespace) }
rescue Talis::NotFoundError
  []
end

Instance Method Details

#delete(request_id: self.class.new_req_id) ⇒ Object

Delete an existing asset.

Parameters:

  • request_id (String) (defaults to: self.class.new_req_id)

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

Raises:



116
117
118
119
120
121
122
# File 'lib/talis/hierarchy/asset.rb', line 116

def delete(request_id: self.class.new_req_id)
  self.class.api_client(request_id).delete_asset(@namespace, stored_id,
                                                 stored_type)
  mark_deleted
rescue BlueprintClient::ApiError => error
  self.class.handle_response(error)
end

#save(request_id: self.class.new_req_id) ⇒ Array<BlueprintClient::Asset>

Persist the asset to the hierarchy.

Parameters:

  • request_id (String) (defaults to: self.class.new_req_id)

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

Returns:

  • (Array<BlueprintClient::Asset>)

    the created asset.

Raises:



86
87
88
89
90
91
# File 'lib/talis/hierarchy/asset.rb', line 86

def save(request_id: self.class.new_req_id)
  add_to_nodes request_id
  mark_persisted if @nodes.count > 1
rescue BlueprintClient::ApiError => error
  self.class.handle_response(error)
end

#update(request_id: self.class.new_req_id) ⇒ Object

Update an existing asset.

Parameters:

  • request_id (String) (defaults to: self.class.new_req_id)

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

Raises:



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/talis/hierarchy/asset.rb', line 98

def update(request_id: self.class.new_req_id)
  body = BlueprintClient::AssetBody.new(data: {
                                          id: @id,
                                          type: @type,
                                          attributes: @attributes
                                        })
  self.class.api_client(request_id).replace_asset(@namespace, stored_id,
                                                  stored_type, body: body)
  mark_persisted
rescue BlueprintClient::ApiError => error
  self.class.handle_response(error)
end