Module: RightScale::Api::Base

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BaseConnection

#connection

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

the following two methods are used to access the @params hash in a friendly way



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 361

def method_missing(method_name, *args)
  mn = method_name.to_s
  assignment = mn.gsub!(/=/,"")
  mn_dash = mn.gsub(/_/,"-")
  if @params[mn]
    if assignment
      @params[mn] = args[0]
      @params[mn_dash] = args[0]
    end
    return @params[mn]
  elsif @params[mn_dash]
    if assignment
      @params[mn_dash] = args[0]
      @params[mn] = args[0]
    end
    return @params[mn_dash]
  elsif @params[mn.to_sym]
    return @params[mn.to_sym]
  elsif assignment
    @params[mn] = args[0]
    @params[mn_dash] = args[0]
    return @params[mn]
  else
    return nil
    #raise "called unknown method #{method_name} with #{args.inspect}"
  end
end

Instance Attribute Details

#paramsObject

The params hash of attributes for direct manipulation



332
333
334
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 332

def params
  @params
end

Instance Method Details

#[](name) ⇒ Object



389
390
391
392
393
394
395
396
397
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 389

def [](name)
  try_these = [name.to_s, name.to_s.gsub(/_/,'-'), name.to_sym]
  try_these.each do |t|
    if @params[t]
      return @params[t]
    end
  end
  nil
end

#[]=(name, val) ⇒ Object



399
400
401
402
403
404
405
406
407
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 399

def []=(name,val)
  try_these = [name.to_s, name.to_s.gsub(/_/,'-'), name.to_sym]
  try_these.each do |t|
    if @params[t]
      @params[t] = val
    end
  end
  val
end

#destroyObject



355
356
357
358
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 355

def destroy
  my_href = URI.parse(self.href)
  connection.delete(my_href.path)
end

#initialize(params = {}) ⇒ Object



333
334
335
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 333

def initialize(params = {})
  @params = params
end

#reloadObject



350
351
352
353
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 350

def reload
  uri = URI.parse(self.href)
  @params ? @params.merge!(connection.get(uri.path)) : @params = connection.get(uri.path)
end

#resource_plural_nameObject



337
338
339
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 337

def resource_plural_name
  self.class.to_s.underscore.pluralize
end

#resource_singular_nameObject



341
342
343
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 341

def resource_singular_name
  self.class.to_s.underscore
end

#rs_idObject



409
410
411
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 409

def rs_id
  self.href.split(/\//).last
end

#saveObject



345
346
347
348
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 345

def save
  uri = URI.parse(self.href)
  connection.put(uri.path, resource_singular_name.to_sym => @params)
end