Class: RightScaleAPI::Base
- Inherits:
-
Object
- Object
- RightScaleAPI::Base
- Defined in:
- lib/right-scale-api/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#href ⇒ Object
Returns the value of attribute href.
-
#id ⇒ Object
Returns the value of attribute id.
Class Method Summary collapse
-
.api_name ⇒ Object
The RightScale API name for the class.
-
.attributes(attrs = nil) ⇒ Object
attributes that directly correspond to the api’s.
-
.create(opts) ⇒ Object
creates a new object on RightScale.
-
.get(id) ⇒ Object
gets an object by id.
Instance Method Summary collapse
- #delete(*args) ⇒ Object
-
#destroy ⇒ Object
tells the API to delete the object.
- #get(*args) ⇒ Object
- #head(*args) ⇒ Object
-
#initialize(attributes) ⇒ Base
constructor
A new instance of Base.
- #post(*args) ⇒ Object
- #put(*args) ⇒ Object
-
#reload! ⇒ Object
Reload the attributes of the object.
- #send_request(method, *args) ⇒ Object
-
#update(attrs) ⇒ Object
Updates the object with the passed attributes.
-
#uri ⇒ Object
the objects uri on rightscale.
Constructor Details
#initialize(attributes) ⇒ Base
Returns a new instance of Base.
60 61 62 63 64 65 66 67 68 |
# File 'lib/right-scale-api/base.rb', line 60 def initialize attributes attributes.each do |attr,value| send "#{attr}=", value end unless id self.id = id_from_href href if href end end |
Instance Attribute Details
#href ⇒ Object
Returns the value of attribute href.
4 5 6 |
# File 'lib/right-scale-api/base.rb', line 4 def href @href end |
#id ⇒ Object
Returns the value of attribute id.
4 5 6 |
# File 'lib/right-scale-api/base.rb', line 4 def id @id end |
Class Method Details
.api_name ⇒ Object
The RightScale API name for the class
56 57 58 |
# File 'lib/right-scale-api/base.rb', line 56 def self.api_name name.demodulize.underscore end |
.attributes(attrs = nil) ⇒ Object
attributes that directly correspond to the api’s
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/right-scale-api/base.rb', line 8 def self.attributes attrs=nil if attrs @attributes ||= Base.attributes @attributes ||= [] @attributes += attrs.map {|attr|attr.to_sym} attr_accessor *attrs attrs.each do |attr| if attr =~ /(.+)_href$/ relation = $1 attr_accessor relation end end end @attributes end |
.create(opts) ⇒ Object
creates a new object on RightScale
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/right-scale-api/base.rb', line 36 def self.create opts object = new opts query_opts = opts_to_query_opts opts result = RightScaleAPI::Client.post(object.collection_uri, :body => {api_name => query_opts}) if result.code.to_i != 201 p object.collection_uri p query_opts p result.code p result.headers puts result.inspect raise "create failed" end new opts.merge(result.merge(:href => result.headers['location'])) end |
.get(id) ⇒ Object
gets an object by id
30 31 32 |
# File 'lib/right-scale-api/base.rb', line 30 def self.get id new :id => id end |
Instance Method Details
#delete(*args) ⇒ Object
102 103 104 |
# File 'lib/right-scale-api/base.rb', line 102 def delete *args send_request :delete, *args end |
#destroy ⇒ Object
tells the API to delete the object
77 78 79 |
# File 'lib/right-scale-api/base.rb', line 77 def destroy delete '' end |
#get(*args) ⇒ Object
86 87 88 |
# File 'lib/right-scale-api/base.rb', line 86 def get *args send_request :get, *args end |
#head(*args) ⇒ Object
90 91 92 |
# File 'lib/right-scale-api/base.rb', line 90 def head *args send_request :head, *args end |
#post(*args) ⇒ Object
94 95 96 |
# File 'lib/right-scale-api/base.rb', line 94 def post *args send_request :post, *args end |
#put(*args) ⇒ Object
98 99 100 |
# File 'lib/right-scale-api/base.rb', line 98 def put *args send_request :put, *args end |
#reload! ⇒ Object
Reload the attributes of the object
112 113 114 115 116 |
# File 'lib/right-scale-api/base.rb', line 112 def reload! get('')[self.class.api_name].each do |attr, value| self.send :"#{attr}=",value end end |
#send_request(method, *args) ⇒ Object
81 82 83 84 |
# File 'lib/right-scale-api/base.rb', line 81 def send_request method, *args args[0] = "#{path}#{args[0]}" RightScaleAPI::Client.send method, *args end |
#update(attrs) ⇒ Object
Updates the object with the passed attributes
72 73 74 |
# File 'lib/right-scale-api/base.rb', line 72 def update attrs put '', :body => {self.class.api_name => self.class.opts_to_query_opts(attrs)} end |
#uri ⇒ Object
the objects uri on rightscale
107 108 109 |
# File 'lib/right-scale-api/base.rb', line 107 def uri RightScaleAPI::Client.base_uri + path end |