Sha256: 5dfd8b3dda386a939af70bfe5ff3b7824edfe038506edab6d14255e1352b6b60

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require 'faraday'
require 'json'
require_relative '../response/api_error'
require_relative '../error/api_error'

module ONEAccess
  module API
    class ApiBase
      class << self
        def send_get(method, params = {})
          Http::HttpClient.get(api_url(method), params)
        end

        def send_post(method, body)
          Http::HttpClient.post(api_url(method), body, 'Content-Type' => 'application/json')
        end

        def resource_api_path
          const_get('RESOURCE_API_PATH')
        rescue StandardError
          nil
        end

        def api_url(method = nil)
          url = "#{base_url}#{resource_api_path}"
          return url if method.nil?
          "#{url}/#{method}"
        end

        protected

        def api_path(path)
          raise 'API path has already been set' if resource_api_path
          self.resource_api_path = path
        end

        private

        def base_url
          raise 'base_url must be implemented'
        end

        def resource_api_path=(path)
          const_set('RESOURCE_API_PATH', path)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
oneaccess-1.3.1 lib/oneaccess/api/api_base.rb
oneaccess-1.3.0 lib/oneaccess/api/api_base.rb