Class: OVH::Provisioner::APIObject::APIObject

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Comparable
Defined in:
lib/ovh/provisioner/api_object/api_object.rb

Overview

Base API Object

Direct Known Subclasses

DedicatedServer, DomainZone

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, parent = nil) ⇒ APIObject

Returns a new instance of APIObject.



47
48
49
50
51
# 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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



37
38
39
# File 'lib/ovh/provisioner/api_object/api_object.rb', line 37

def id
  @id
end

Class Method Details

.attr_reader(*vars) ⇒ Object

Define @attributes to contain all attr_readers



30
31
32
33
34
35
# 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

.attributesObject

rubocop:disable Style/TrivialAccessors



39
40
41
# File 'lib/ovh/provisioner/api_object/api_object.rb', line 39

def self.attributes # rubocop:disable Style/TrivialAccessors
  @attributes
end

.classnameObject



76
77
78
# File 'lib/ovh/provisioner/api_object/api_object.rb', line 76

def self.classname
  name.split('::').last
end

.entrypoint(parent = nil) ⇒ Object



71
72
73
74
# 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

.underscore(string) ⇒ Object

inspired from Rails’ ActiveSupport



81
82
83
84
85
86
# 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

Instance Method Details

#<=>(other) ⇒ Object



88
89
90
# File 'lib/ovh/provisioner/api_object/api_object.rb', line 88

def <=>(other)
  id <=> other.id
end

#attributesObject



43
44
45
# File 'lib/ovh/provisioner/api_object/api_object.rb', line 43

def attributes
  self.class.attributes
end

#configObject



92
93
94
# File 'lib/ovh/provisioner/api_object/api_object.rb', line 92

def config
  OVH::Provisioner.config
end

#delete(path = '') ⇒ Object



104
105
106
# File 'lib/ovh/provisioner/api_object/api_object.rb', line 104

def delete(path = '')
  call(:delete, ["#{@url}/#{path}"])
end

#get(path = '') ⇒ Object



96
97
98
# File 'lib/ovh/provisioner/api_object/api_object.rb', line 96

def get(path = '')
  call(:get, ["#{@url}/#{path}"])
end

#init_propertiesObject



53
54
55
56
57
58
59
60
61
62
63
64
# 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) ⇒ Object



100
101
102
# File 'lib/ovh/provisioner/api_object/api_object.rb', line 100

def post(path, body = nil)
  call(:post, ["#{@url}/#{path}", body])
end

#to_sObject



66
67
68
69
# 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