class OVH::Provisioner::APIObject::APIObject

Base API Object

Attributes

id[R]

Public Class Methods

attr_reader(*vars) click to toggle source

Define @attributes to contain all attr_readers

Calls superclass method
# File lib/ovh/provisioner/api_object/api_object.rb, line 30
def self.attr_reader(*vars)
  @attributes ||= superclass.attributes.dup unless superclass == Object
  @attributes ||= []
  @attributes.concat vars
  super
end
attributes() click to toggle source
# File lib/ovh/provisioner/api_object/api_object.rb, line 39
def self.attributes # rubocop:disable Style/TrivialAccessors
  @attributes
end
classname() click to toggle source
# File lib/ovh/provisioner/api_object/api_object.rb, line 76
def self.classname
  name.split('::').last
end
entrypoint(parent = nil) click to toggle source
# File lib/ovh/provisioner/api_object/api_object.rb, line 71
def self.entrypoint(parent = nil)
  classpath = underscore(classname).tr('_', '/')
  parent.nil? ? "/#{classpath}" : "#{parent}/#{classpath}"
end
new(id, parent = nil) click to toggle source
# File lib/ovh/provisioner/api_object/api_object.rb, line 47
def initialize(id, parent = nil)
  @id = id
  @url = "#{self.class.entrypoint(parent)}/#{normalize(id)}"
  @futures = {}
end
underscore(string) click to toggle source

inspired from Rails' ActiveSupport

# File lib/ovh/provisioner/api_object/api_object.rb, line 81
def self.underscore(string)
  res = string.gsub(/::/, '/')
  res.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
  res.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  res.tr('-', '_').downcase
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/ovh/provisioner/api_object/api_object.rb, line 88
def <=>(other)
  id <=> other.id
end
attributes() click to toggle source
# File lib/ovh/provisioner/api_object/api_object.rb, line 43
def attributes
  self.class.attributes
end
config() click to toggle source
# File lib/ovh/provisioner/api_object/api_object.rb, line 92
def config
  OVH::Provisioner.config
end
delete(path = '') click to toggle source
# File lib/ovh/provisioner/api_object/api_object.rb, line 104
def delete(path = '')
  call(:delete, ["#{@url}/#{path}"])
end
get(path = '') click to toggle source
# File lib/ovh/provisioner/api_object/api_object.rb, line 96
def get(path = '')
  call(:get, ["#{@url}/#{path}"])
end
init_properties() click to toggle source
# File lib/ovh/provisioner/api_object/api_object.rb, line 53
def init_properties
  unless @completed
    # In ATOM concurrency, this is enough to avoid a race condition.
    @completed = true
    infos = private_methods(false).grep(/^set_/)
    infos.each { |i| @futures[i] = future.send(i) }
  end
  @futures.each_pair do |key, future|
    future.value
    @futures.delete(key)
  end
end
post(path, body = nil) click to toggle source
# File lib/ovh/provisioner/api_object/api_object.rb, line 100
def post(path, body = nil)
  call(:post, ["#{@url}/#{path}", body])
end
to_s() click to toggle source
# File lib/ovh/provisioner/api_object/api_object.rb, line 66
def to_s
  "#{self.class.entrypoint}: #{id}\n" \
  "#{attributes.map { |s| "- #{send(s)}" }.join("\n")}"
end

Private Instance Methods

call(method, args) click to toggle source
# File lib/ovh/provisioner/api_object/api_object.rb, line 110
def call(method, args)
  OVH::Provisioner.client.send(method, *args)
rescue OVH::RESTError => error
  error.to_s.split(':')[-1].strip
end
normalize(id) click to toggle source
# File lib/ovh/provisioner/api_object/api_object.rb, line 116
def normalize(id)
  id = id.gsub('/', '%2F') if id.is_a? String
  id
end