Class: RightScaleAPI::Base
- Inherits:
-
Object
- Object
- RightScaleAPI::Base
- Defined in:
- lib/right-scale-api/base.rb
Direct Known Subclasses
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
- #collection_uri ⇒ Object
- #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.
- #path ⇒ Object
- #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.
58 59 60 61 62 63 64 65 66 |
# File 'lib/right-scale-api/base.rb', line 58 def initialize attributes attributes.each do |attr,value| send "#{attr}=", value end unless id self.id = id_from_href href if href end end |
Class Method Details
.api_name ⇒ Object
The RightScale API name for the class
54 55 56 |
# File 'lib/right-scale-api/base.rb', line 54 def self.api_name name.demodulize.underscore end |
.attributes(attrs = nil) ⇒ Object
attributes that directly correspond to the api’s
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/right-scale-api/base.rb', line 6 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
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/right-scale-api/base.rb', line 34 def self.create opts object_opts = opts.reject{|k,v| ! instance_methods.include? "#{k}="} object = new object_opts query_opts = opts_to_query_opts opts result = RightScaleAPI::Client.post(object.collection_uri, :body => 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 object_opts.merge(result.merge(:href => result.headers['location'])) end |
.get(id) ⇒ Object
gets an object by id
28 29 30 |
# File 'lib/right-scale-api/base.rb', line 28 def self.get id new :id => id end |
Instance Method Details
#collection_uri ⇒ Object
116 117 118 |
# File 'lib/right-scale-api/base.rb', line 116 def collection_uri self.class.collection_uri end |
#delete(*args) ⇒ Object
100 101 102 |
# File 'lib/right-scale-api/base.rb', line 100 def delete *args send_request :delete, *args end |
#destroy ⇒ Object
tells the API to delete the object
75 76 77 |
# File 'lib/right-scale-api/base.rb', line 75 def destroy delete '' end |
#get(*args) ⇒ Object
84 85 86 |
# File 'lib/right-scale-api/base.rb', line 84 def get *args send_request :get, *args end |
#head(*args) ⇒ Object
88 89 90 |
# File 'lib/right-scale-api/base.rb', line 88 def head *args send_request :head, *args end |
#path ⇒ Object
120 121 122 |
# File 'lib/right-scale-api/base.rb', line 120 def path "#{collection_uri}/#{id}" end |
#post(*args) ⇒ Object
92 93 94 |
# File 'lib/right-scale-api/base.rb', line 92 def post *args send_request :post, *args end |
#put(*args) ⇒ Object
96 97 98 |
# File 'lib/right-scale-api/base.rb', line 96 def put *args send_request :put, *args end |
#reload! ⇒ Object
Reload the attributes of the object
110 111 112 113 114 |
# File 'lib/right-scale-api/base.rb', line 110 def reload! get('')[self.class.api_name].each do |attr, value| self.send :"#{attr}=",value end end |
#send_request(method, *args) ⇒ Object
79 80 81 82 |
# File 'lib/right-scale-api/base.rb', line 79 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
70 71 72 |
# File 'lib/right-scale-api/base.rb', line 70 def update attrs put '', :body => {self.class.api_name => self.class.opts_to_query_opts(attrs)} end |
#uri ⇒ Object
the objects uri on rightscale
105 106 107 |
# File 'lib/right-scale-api/base.rb', line 105 def uri RightScaleAPI::Client.base_uri + path end |