Sha256: 555453e97a3e75aa2b0535ba013a74e5e54ca26afe20304cdd02b34d73789569

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

module GLPI
  module SDK
    class Resource
      def initialize(attributes = {})
        @attributes = attributes
      end

      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

      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

      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

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

      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

      class << self
        alias where all
      end

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

      private

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

      def respond_to_missing?(name, include_private = false)
        @attributes.key?(name.to_s)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
glpi-sdk-ruby-0.0.2 lib/glpi/sdk/resource.rb