Sha256: 857369441bc6e0bfadba81a084039b6653ae50af2f0fc3325fa3845d473d50f8

Contents?: true

Size: 1.12 KB

Versions: 3

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

3 entries across 3 versions & 1 rubygems

Version Path
oneaccess-1.2.0 lib/oneaccess/api/api_base.rb
oneaccess-1.1.0 lib/oneaccess/api/api_base.rb
oneaccess-1.0.1 lib/oneaccess/api/api_base.rb