Sha256: 3af9b8aca7175942cb1fb484571b4b5f72c01c7e8f7fc473f89731fc7e1af449

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module Tikkie
  module Api
    module Resources
      # Base class for all resources.
      class Base
        attr_reader :config, :options

        def initialize(config, options = {})
          @config = config
          @options = options
          @body = options.delete(:body) if options.key?(:body)
        end

        def load
          @response ||= load_resource

          self
        end

        def save(attributes = {})
          @response = create_resource(attributes)

          self
        end

        def delete
          delete_resource

          nil
        end

        def loaded?
          !@response.nil?
        end

        private

        def load_resource
          raise NotImplementedError
        end

        def create_resource
          raise NotImplementedError
        end

        def delete_resource
          raise NotImplementedError
        end

        def body
          @body ||= begin
            load unless loaded?

            @response.body
          end
        end

        def request
          @request ||= Tikkie::Api::Request.new(config)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tikkie-api-2.0.0 lib/tikkie/api/resources/base.rb