Class: VagrantPlugins::Skytap::API::Resource
- Includes:
- SpecifiedAttributes
- Defined in:
- lib/vagrant-skytap/api/resource.rb
Direct Known Subclasses
Credentials, Environment, Interface, Network, PublicIp, PublishSet, PublishedService, Tunnel, Vm, Vpn, VpnAttachment
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
Class Method Summary collapse
-
.rest_name ⇒ String
Resource name suitable for use in URLs.
-
.short_name ⇒ String
Last segment of the class name (without the namespace).
Instance Method Summary collapse
-
#delete ⇒ NilClass
Remove this resource from Skytap.
-
#initialize(*args) ⇒ Resource
constructor
A new instance of Resource.
-
#refresh(attrs) ⇒ API::Resource
Replace the object’s attributes hash.
-
#reload ⇒ API::Resource
Re-fetch the object from the API and update attributes.
-
#update(attrs, path = nil) ⇒ API::Resource
Sets attributes on the Skytap model, then refreshes this resource from the response.
-
#url ⇒ String
The URL for this specific instance.
Methods included from SpecifiedAttributes
Constructor Details
#initialize(*args) ⇒ Resource
Returns a new instance of Resource.
52 53 54 55 |
# File 'lib/vagrant-skytap/api/resource.rb', line 52 def initialize(*args) @attrs = args.first @env = args.last end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
33 34 35 |
# File 'lib/vagrant-skytap/api/resource.rb', line 33 def attrs @attrs end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
33 34 35 |
# File 'lib/vagrant-skytap/api/resource.rb', line 33 def env @env end |
Class Method Details
.rest_name ⇒ String
Resource name suitable for use in URLs. This should be overridden for classes with camel-cased names (e.g., VpnAttachment).
47 48 49 |
# File 'lib/vagrant-skytap/api/resource.rb', line 47 def rest_name short_name.downcase end |
.short_name ⇒ String
Last segment of the class name (without the namespace).
39 40 41 |
# File 'lib/vagrant-skytap/api/resource.rb', line 39 def short_name name.split("::").last end |
Instance Method Details
#delete ⇒ NilClass
Remove this resource from Skytap.
98 99 100 |
# File 'lib/vagrant-skytap/api/resource.rb', line 98 def delete api_client.delete(url) end |
#refresh(attrs) ⇒ API::Resource
Replace the object’s attributes hash. Subclasses may override this method to perform additional operations such as discarding cached child collections.
78 79 80 81 |
# File 'lib/vagrant-skytap/api/resource.rb', line 78 def refresh(attrs) @attrs = attrs self end |
#reload ⇒ API::Resource
Re-fetch the object from the API and update attributes.
68 69 70 71 |
# File 'lib/vagrant-skytap/api/resource.rb', line 68 def reload resp = api_client.get(url) refresh(JSON.load(resp.body)) end |
#update(attrs, path = nil) ⇒ API::Resource
Sets attributes on the Skytap model, then refreshes this resource from the response.
90 91 92 93 |
# File 'lib/vagrant-skytap/api/resource.rb', line 90 def update(attrs, path=nil) resp = api_client.put(path || url, JSON.dump(attrs)) refresh(JSON.load(resp.body)) end |
#url ⇒ String
The URL for this specific instance. This method should be overridden for more complex routes such as Rails nested resources.
61 62 63 |
# File 'lib/vagrant-skytap/api/resource.rb', line 61 def url "/#{self.class.rest_name}s/#{id}" end |