Class: Talis::Hierarchy::Asset
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'
Instance Attribute Summary collapse
-
#nodes ⇒ Array<BlueprintClient::Node>
An array of nodes an asset can belong to.
Attributes included from OAuthService
#client_id, #client_secret, #oauth_host
Attributes included from Resource::Helpers
#attributes, #id, #namespace, #type
Class Method Summary collapse
-
.all(request_id: new_req_id, namespace:) ⇒ Array<Talis::Hierarchy::Asset>
Return all assets in the hierarchy for the given namespace.
-
.api_client(request_id = new_req_id) ⇒ Object
Exposes the underlying Blueprint assets API client.
-
.find(request_id: new_req_id, namespace:, type:, id:) ⇒ Object
Fetch a single asset from the hierarchy for the given namespace.
-
.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.
-
.where(request_id: new_req_id, namespace:, opts: {}) ⇒ Array<Talis::Hierarchy::Asset>
Search for assets in the hierarchy for the given namespace.
Instance Method Summary collapse
-
#delete(request_id: self.class.new_req_id) ⇒ Object
Delete an existing asset.
-
#initialize(namespace:, type:, id:, nodes: [], attributes: {}) ⇒ Asset
constructor
Create a non-persisted asset.
-
#save(request_id: self.class.new_req_id) ⇒ Array<BlueprintClient::Asset>
Persist the asset to the hierarchy.
-
#update(request_id: self.class.new_req_id) ⇒ Object
Update an existing asset.
Methods included from Resource
Methods included from Resource::Helpers
#persisted?, #stored_id, #stored_type
Methods inherited from Resource
Constructor Details
#initialize(namespace:, type:, id:, nodes: [], attributes: {}) ⇒ Asset
Create a non-persisted 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
#nodes ⇒ Array<BlueprintClient::Node>
Returns 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.
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.
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.
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.
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.
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.
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.
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.
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 |