Sha256: 041b3e8f19c593686759ec5ad6ffd353225bedba8caf5693ed31845ba01d0d54

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

module Umami
  class Configuration
    UMAMI_CLOUD_URL = "https://api.umami.is".freeze

    attr_reader :uri_base, :request_timeout, :access_token, :username, :password

    def initialize
      @uri_base = nil
      @request_timeout = 120
      @access_token = nil
      @username = nil
      @password = nil
    end

    def uri_base=(url)
      @uri_base = url&.chomp('/')
      validate_configuration
    end

    def access_token=(token)
      @access_token = token
      @username = nil
      @password = nil
      validate_configuration
    end

    def credentials=(creds)
      raise Umami::ConfigurationError, "Both username and password are required" unless creds[:username] && creds[:password]

      @username = creds[:username]
      @password = creds[:password]
      @access_token = nil
      validate_configuration
    end

    def cloud?
      @access_token && @uri_base.nil?
    end

    private

    def validate_configuration
      if cloud?
        @uri_base = UMAMI_CLOUD_URL
        Umami.logger.info "Using Umami Cloud (#{UMAMI_CLOUD_URL})"
      end

      if @uri_base == UMAMI_CLOUD_URL && (@username || @password)
        raise Umami::ConfigurationError, "Username/password authentication is not supported for Umami Cloud"
      end

      if @access_token && (@username || @password)
        Umami.logger.warn "Both access token and credentials provided. Access token will be used."
        @username = nil
        @password = nil
      end

      if @uri_base && @uri_base != UMAMI_CLOUD_URL && !@access_token && !@username && !@password
        raise Umami::ConfigurationError, "Authentication is required for self-hosted instances"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
umami-ruby-0.1.1 lib/umami/configuration.rb
umami-ruby-0.1.0 lib/umami/configuration.rb