Sha256: af8a9c6405e06550c4bac1206f2b857dc8edd60b3e8a61c292eb78074a2ea0ab

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true
require "rest-client"
require "json"
require_relative "../response/api_error"
require_relative "../error/api_error"

module ONEAccess
  module API
    class Base
      class << self
        def get(method, params = {})
          RestClient.get(api_url(method), { params: params }.merge(auth_headers))
        rescue RestClient::Exception => e
          raise create_api_error(e)
        end

        def resource_api_path
          const_get("RESOURCE_API_PATH")
        rescue
          nil
        end

        def api_url(method = nil)
          url = "#{Configuration.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 create_api_error(error)
          api_error_data = Response::APIError.new
          Response::Representer::APIError.new(api_error_data).from_json(error.http_body)
          Error::APIError.new(error.http_code, api_error_data.api_status_code, api_error_data.api_status_message)
        end

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

        def auth_headers
          {
            "X-One-Access-Api-Key" => Configuration.api_key,
            "Authorization"        => "X-One-Access-Master-Api-Key #{Configuration.master_api_key}"
          }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
oneaccess-0.1.0 lib/oneaccess/api/base.rb