Class: GLPI::SDK::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/glpi/sdk/resource.rb

Direct Known Subclasses

Profile, Ticket, User

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Resource

Returns a new instance of Resource.



4
5
6
# File 'lib/glpi/sdk/resource.rb', line 4

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



53
54
55
# File 'lib/glpi/sdk/resource.rb', line 53

def method_missing(name, *args, &block)
  @attributes.key?(name.to_s) ? @attributes[name.to_s] : super
end

Class Method Details

.all(session, **params) ⇒ Object Also known as: where



35
36
37
38
39
40
41
# File 'lib/glpi/sdk/resource.rb', line 35

def self.all(session, **params)
  item_type = params.key?(:item_type) ? params.delete(:item_type) : type_name
  endpoint = "/#{item_type}"
  endpoint += "?#{HashConverter.to_query(params)}" if params
  response = session.request :get, endpoint
  response.map { |item| new(item) }
end

.create(session, attributes = {}) ⇒ Object



8
9
10
11
12
13
# File 'lib/glpi/sdk/resource.rb', line 8

def self.create(session, attributes = {})
  item_type = attributes.key?(:item_type) ? attributes.delete(:item_type) : type_name
  payload = { input: attributes }.to_json
  response = session.request :post, "/#{item_type}", body: payload
  find(session, response['id'], item_type)
end

.destroy(session, id, item_type = nil) ⇒ Object



22
23
24
25
26
27
# File 'lib/glpi/sdk/resource.rb', line 22

def self.destroy(session, id, item_type = nil)
  item_type ||= type_name
  payload = { force_purge: true }.to_json
  response = session.request :delete, "/#{item_type}/#{id}", body: payload
  response[0][id.to_s]
end

.find(session, id, item_type = nil) ⇒ Object



29
30
31
32
33
# File 'lib/glpi/sdk/resource.rb', line 29

def self.find(session, id, item_type = nil)
  item_type ||= type_name
  response = session.request :get, "/#{item_type}/#{id}"
  new(response)
end

.type_nameObject



47
48
49
# File 'lib/glpi/sdk/resource.rb', line 47

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

.update(session, id, attributes = {}) ⇒ Object



15
16
17
18
19
20
# File 'lib/glpi/sdk/resource.rb', line 15

def self.update(session, id, attributes = {})
  item_type = attributes.key?(:item_type) ? attributes.delete(:item_type) : type_name
  payload = { input: attributes }.to_json
  session.request :put, "/#{item_type}/#{id}", body: payload
  find(session, id, item_type)
end