Sha256: c205eb6fe70bc2c427d637613a1459da3ac82452e30aaff9f0999691e560959b

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module Kentaa
  module Api
    module Resources
      class Base
        attr_reader :config, :options

        def initialize(config, options = {})
          @config = config
          @options = options
        end

        def load
          @response ||= load_resource(options)

          self
        end

        def loaded?
          !@response.nil?
        end

        private

        def attribute_key
          class_name = self.class.name.split('::').last
          class_name.gsub(/([^\^])([A-Z])/, '\1_\2').downcase.to_sym
        end

        def load_resource(_options)
          raise NotImplementedError
        end

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

            @response.body[attribute_key] || {}
          end
        end

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

            @response.body
          end
        end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kentaa-api-0.3.2 lib/kentaa/api/resources/base.rb
kentaa-api-0.3.1 lib/kentaa/api/resources/base.rb
kentaa-api-0.3.0 lib/kentaa/api/resources/base.rb