Class: RightScaleAPI::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/right-scale-api/base.rb

Direct Known Subclasses

Account, Account::SubResource

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Base

Returns a new instance of Base.



54
55
56
57
58
59
60
61
62
# File 'lib/right-scale-api/base.rb', line 54

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

#hrefObject

Returns the value of attribute href.



4
5
6
# File 'lib/right-scale-api/base.rb', line 4

def href
  @href
end

#idObject

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_nameObject



50
51
52
# File 'lib/right-scale-api/base.rb', line 50

def self.api_name
  name.demodulize.underscore
end

.attributes(attrs = nil) ⇒ Object

attributes that directly correspond to the api’s



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/right-scale-api/base.rb', line 7

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/right-scale-api/base.rb', line 31

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'].first))
end

.get(id) ⇒ Object



27
28
29
# File 'lib/right-scale-api/base.rb', line 27

def self.get id
  new :id => id
end

Instance Method Details

#delete(*args) ⇒ Object



93
94
95
# File 'lib/right-scale-api/base.rb', line 93

def delete *args
  send_request :delete, *args
end

#destroyObject



68
69
70
# File 'lib/right-scale-api/base.rb', line 68

def destroy
  delete ''
end

#get(*args) ⇒ Object



77
78
79
# File 'lib/right-scale-api/base.rb', line 77

def get *args
  send_request :get, *args
end

#head(*args) ⇒ Object



81
82
83
# File 'lib/right-scale-api/base.rb', line 81

def head *args
  send_request :head, *args
end

#post(*args) ⇒ Object



85
86
87
# File 'lib/right-scale-api/base.rb', line 85

def post *args
  send_request :post, *args
end

#put(*args) ⇒ Object



89
90
91
# File 'lib/right-scale-api/base.rb', line 89

def put *args
  send_request :put, *args
end

#reload!Object



101
102
103
104
105
# File 'lib/right-scale-api/base.rb', line 101

def reload!
  get('')[self.class.api_name].each do |attr, value|
    self.send :"#{attr}=",value
  end
end

#send_request(method, *args) ⇒ Object



72
73
74
75
# File 'lib/right-scale-api/base.rb', line 72

def send_request method, *args
  args[0] = "#{path}#{args[0]}"
  RightScaleAPI::Client.send method, *args
end

#update(attrs) ⇒ Object



64
65
66
# File 'lib/right-scale-api/base.rb', line 64

def update attrs
  put '', :body => {self.class.api_name =>  self.class.opts_to_query_opts(attrs)}
end

#uriObject



97
98
99
# File 'lib/right-scale-api/base.rb', line 97

def uri
  RightScaleAPI::Client.base_uri + path
end