Class: RightScaleAPI::Base

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

Direct Known Subclasses

Account, Account::SubResource

Class Method Summary collapse

Instance Method Summary collapse

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_nameObject

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

Parameters:

  • attrs (Array<Symbol>) (defaults to: nil)

    a list of attributes an object type has



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

Parameters:

  • opts (Hash)

    attributes of the created object



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

Parameters:

  • id (Fixnum)


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

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

Instance Method Details

#collection_uriObject



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

#destroyObject

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

#pathObject



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

Parameters:

  • attrs (Hash)

    the updated 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

#uriObject

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