Class: RightScale::Base

Inherits:
Object
  • Object
show all
Includes:
AccessibleFragment
Defined in:
lib/right_api/rightscale.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AccessibleFragment

#get_element, #has_element?, #method_missing

Constructor Details

#initialize(data) ⇒ Base

Returns a new instance of Base.



14
15
16
17
# File 'lib/right_api/rightscale.rb', line 14

def initialize(data)
  raise "You must first establish a connection via Base.establish_connection" unless @@connection
  @data = data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AccessibleFragment

Class Method Details

.all(opts = {}) ⇒ Object



63
64
65
# File 'lib/right_api/rightscale.rb', line 63

def self.all(opts={})
  find_with_options(opts)
end

.class_nameObject



31
32
33
# File 'lib/right_api/rightscale.rb', line 31

def self.class_name
  name.split("::").last.underscore
end

.establish_connection(user, password, account_id) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/right_api/rightscale.rb', line 44

def self.establish_connection(user, password, )
  url = "https://my.rightscale.com/api/acct/#{}"
  params = {'server_url' => url, 'username' => user, 'password' => password}
  
  @@connection = HttpServer.new(params, 60)
  @@connection.get('/login')
  
  params = {'server_url' => "https://my.rightscale.com"}
  
  @@non_api_connection = HttpServer.new(params, 60)
  @@non_api_connection.post('/sessions', 'email' => user, 'password' => password) do |server, response|
    cookies = response.response.get_fields('set-cookie')
    session_cookie = cookies.detect { |cookie| cookie =~ /_session_id/ }
    cookie_header_for_request = session_cookie.split(';').first
    server.headers = server.headers.merge('cookie' => cookie_header_for_request, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
  end
  
end

.find(opts = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/right_api/rightscale.rb', line 71

def self.find(opts=nil)
  if opts.nil?
    raise "Find requires a hash of options or an id"
  elsif opts.is_a?(Hash)
    find_with_options(opts)
  else
    find(:id => opts)
  end
end

.first(opts = {}) ⇒ Object



67
68
69
# File 'lib/right_api/rightscale.rb', line 67

def self.first(opts={})
  find_first_with_options(opts)
end

.plural_nameObject



35
36
37
# File 'lib/right_api/rightscale.rb', line 35

def self.plural_name
  class_name.pluralize
end

.xml_nameObject



39
40
41
# File 'lib/right_api/rightscale.rb', line 39

def self.xml_name
  class_name.dasherize
end

Instance Method Details

#dataObject



23
24
25
# File 'lib/right_api/rightscale.rb', line 23

def data
  @data ||= @@connection.get("/#{plural_name}/#{id}")
end

#idObject



19
20
21
# File 'lib/right_api/rightscale.rb', line 19

def id
  @id ||= href.split('/').last
end

#update_attribute(attribute, value) ⇒ Object



27
28
29
# File 'lib/right_api/rightscale.rb', line 27

def update_attribute(attribute, value)
  @@connection.put("/#{self.class.plural_name}/#{id}", "#{self.class.class_name}[#{attribute}]" => value)
end