Class: RedfishClient::Resource
- Inherits:
-
Object
- Object
- RedfishClient::Resource
- Defined in:
- lib/redfish_client/resource.rb
Overview
Resource is basic building block of Redfish client and serves as a container for the data that is retrieved from the Redfish service.
When we interact with the Redfish service, resource will wrap the data retrieved from the service API and offer us dot-notation accessors for values stored.
Resource will also load any sub-resource on demand when we access it.
For example, if we have a root Redfish resource stored in root,
accessing root.SessionService will automatically fetch the appropriate
resource from the API.
In order to reduce the amount of requests being sent to the service, resource can also utilise caching connector. If we would like to get fresh values from the service, #reset call will flush the cache, causing next access to retrieve fresh data.
Direct Known Subclasses
Defined Under Namespace
Classes: NoODataId, NoResource
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Headers, returned from the service when resource has been constructed.
Instance Method Summary collapse
-
#[](attr) ⇒ Object
Access resource content.
-
#delete ⇒ Excon::Response
Issue a DELETE requests to the endpoint of the resource.
-
#dig(*keys) ⇒ Object
Safely access nested resource content.
-
#initialize(connector, oid: nil, content: nil) ⇒ Resource
constructor
Create new resource.
-
#key?(name) ⇒ Boolean
Test if resource contains required key.
-
#method_missing(symbol, *_args, &_block) ⇒ Object
Convenience access for resource data.
-
#patch(field: "@odata.id", path: nil, payload: nil) ⇒ Excon::Response
Issue a PATCH requests to the selected endpoint.
-
#post(field: "@odata.id", path: nil, payload: nil) ⇒ Excon::Response
Issue a POST requests to the selected endpoint.
-
#raw ⇒ Hash
Access raw JSON data that resource wraps.
-
#reset ⇒ Object
Clear the cached sub-resources.
- #respond_to_missing?(symbol, include_private = false) ⇒ Boolean
-
#to_s ⇒ String
Pretty-print the wrapped content.
Constructor Details
#initialize(connector, oid: nil, content: nil) ⇒ Resource
Create new resource.
Resource can be created either by passing in OpenData identifier or supplying the content (hash). In the first case, connector will be used to fetch the resource data. In the second case, resource only wraps the passed-in hash and does no fetching.
46 47 48 49 50 51 52 53 54 |
# File 'lib/redfish_client/resource.rb', line 46 def initialize(connector, oid: nil, content: nil) @connector = connector if oid initialize_from_service(oid) else @content = content end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *_args, &_block) ⇒ Object
Convenience access for resource data.
Calling resource.Value is exactly the same as resource["Value"].
91 92 93 |
# File 'lib/redfish_client/resource.rb', line 91 def method_missing(symbol, *_args, &_block) self[symbol.to_s] end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Headers, returned from the service when resource has been constructed.
32 33 34 |
# File 'lib/redfish_client/resource.rb', line 32 def headers @headers end |
Instance Method Details
#[](attr) ⇒ Object
Access resource content.
This function offers a way of accessing resource data in the same way that hash exposes its content.
63 64 65 |
# File 'lib/redfish_client/resource.rb', line 63 def [](attr) build_resource(@content[attr]) end |
#delete ⇒ Excon::Response
Issue a DELETE requests to the endpoint of the resource.
If the resource has no @odata.id field, NoODataId error will be
raised, since deleting non-networked resources makes no sense and
probably indicates bug in library consumer.
166 167 168 |
# File 'lib/redfish_client/resource.rb', line 166 def delete @connector.delete(get_path("@odata.id", nil)) end |
#dig(*keys) ⇒ Object
Safely access nested resource content.
This function is an equivalent of safe navigation operator that can be used with arbitrary keys.
Calling res.dig("a", "b", "c") is equivalent to res.a&.b&.c and
res["a"] && res["a"]["b"] && res["a"]["b"]["c"].
76 77 78 |
# File 'lib/redfish_client/resource.rb', line 76 def dig(*keys) keys.reduce(self) { |a, k| a.nil? ? nil : a[k] } end |
#key?(name) ⇒ Boolean
Test if resource contains required key.
84 85 86 |
# File 'lib/redfish_client/resource.rb', line 84 def key?(name) @content.key?(name.to_s) end |
#patch(field: "@odata.id", path: nil, payload: nil) ⇒ Excon::Response
Issue a PATCH requests to the selected endpoint.
Works exactly the same as the #post method, but issued a PATCH request to the server.
154 155 156 |
# File 'lib/redfish_client/resource.rb', line 154 def patch(field: "@odata.id", path: nil, payload: nil) @connector.patch(get_path(field, path), payload) end |
#post(field: "@odata.id", path: nil, payload: nil) ⇒ Excon::Response
Issue a POST requests to the selected endpoint.
By default, POST request will be sent to the path, stored in @odata.id
field. Source field can be changed by specifying the field parameter
when calling this function. Specifying the path argument will bypass
the field lookup altogether and POST directly to the requested path.
In order to avoid having to manually serialize data to JSON, this function call takes Hash as a payload and encodes it before sending it to the endpoint.
If the resource has no lookup field, NoODataId error will be raised, since posting to non-networked resources makes no sense and probably indicates bug in library consumer.
140 141 142 |
# File 'lib/redfish_client/resource.rb', line 140 def post(field: "@odata.id", path: nil, payload: nil) @connector.post(get_path(field, path), payload) end |
#raw ⇒ Hash
Access raw JSON data that resource wraps.
109 110 111 |
# File 'lib/redfish_client/resource.rb', line 109 def raw @content end |
#reset ⇒ Object
Clear the cached sub-resources.
This method is a no-op if connector in use does not support caching.
102 103 104 |
# File 'lib/redfish_client/resource.rb', line 102 def reset @connector.reset if @connector.respond_to?(:reset) end |
#respond_to_missing?(symbol, include_private = false) ⇒ Boolean
95 96 97 |
# File 'lib/redfish_client/resource.rb', line 95 def respond_to_missing?(symbol, include_private = false) key?(symbol.to_s) || super end |
#to_s ⇒ String
Pretty-print the wrapped content.
116 117 118 |
# File 'lib/redfish_client/resource.rb', line 116 def to_s JSON.pretty_generate(@content) end |