Sha256: 3f28e58226927e3d36c049465562e8529d18f2198e430bd313c41cad1b697792

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

require 'time'

module Kentaa
  module Api
    module Resources
      class Resource < Base
        attr_accessor :id

        def initialize(config, id: nil, data: nil, options: {})
          super(config, options)

          if data
            @data = data || {}
            @id = @data.fetch(:id) if @data.key?(:id)
          elsif id
            @id = id
          end
        end

        def load
          super
          @id = data.fetch(:id) if data.key?(:id)

          self
        end

        def save(attributes)
          if id
            @response = update_resource(attributes)
          else
            @response = create_resource(attributes)
            @id = data.fetch(:id) if data.key?(:id)
          end

          self
        end

        def delete
          delete_resource

          nil
        end

        def created_at
          Time.parse(data[:created_at]) if data[:created_at]
        end

        def updated_at
          Time.parse(data[:updated_at]) if data[:updated_at]
        end

        private

        def create_resource
          raise NotImplementedError
        end

        def update_resource
          raise NotImplementedError
        end

        def delete_resource
          raise NotImplementedError
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
kentaa-api-0.7.2 lib/kentaa/api/resources/resource.rb
kentaa-api-0.7.1 lib/kentaa/api/resources/resource.rb
kentaa-api-0.7.0 lib/kentaa/api/resources/resource.rb
kentaa-api-0.6.0 lib/kentaa/api/resources/resource.rb
kentaa-api-0.5.0 lib/kentaa/api/resources/resource.rb