Class: VagrantPlugins::Skytap::API::Resource

Inherits:
Object
  • Object
show all
Includes:
SpecifiedAttributes
Defined in:
lib/vagrant-skytap/api/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SpecifiedAttributes

#get_api_attribute, included

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

#attrsObject (readonly)

Returns the value of attribute attrs.



33
34
35
# File 'lib/vagrant-skytap/api/resource.rb', line 33

def attrs
  @attrs
end

#envObject (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_nameString

Resource name suitable for use in URLs. This should be overridden for classes with camel-cased names (e.g., VpnAttachment).

Returns:



47
48
49
# File 'lib/vagrant-skytap/api/resource.rb', line 47

def rest_name
  short_name.downcase
end

.short_nameString

Last segment of the class name (without the namespace).

Returns:



39
40
41
# File 'lib/vagrant-skytap/api/resource.rb', line 39

def short_name
  name.split("::").last
end

Instance Method Details

#deleteNilClass

Remove this resource from Skytap.

Returns:



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.

Returns:



78
79
80
81
# File 'lib/vagrant-skytap/api/resource.rb', line 78

def refresh(attrs)
  @attrs = attrs
  self
end

#reloadAPI::Resource

Re-fetch the object from the API and update attributes.

Returns:



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.

Parameters:

  • attrs (Hash)

    The attributes to update on the resource.

  • path (String) (defaults to: nil)

    The path to this resource, if different from the default.

Returns:



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

#urlString

The URL for this specific instance. This method should be overridden for more complex routes such as Rails nested resources.

Returns:



61
62
63
# File 'lib/vagrant-skytap/api/resource.rb', line 61

def url
  "/#{self.class.rest_name}s/#{id}"
end