Class: Bugsink::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/bugsink/config.rb

Overview

Configuration management for BugSink API client

Defined Under Namespace

Classes: ConfigError

Constant Summary collapse

DOTFILE =
'.bugsink'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



12
13
14
15
16
# File 'lib/bugsink/config.rb', line 12

def initialize
  @api_key = ENV['BUGSINK_API_KEY']
  @host = ENV.fetch('BUGSINK_HOST', 'https://bugs.kopernici.cz')
  @project_id = read_project_id
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



8
9
10
# File 'lib/bugsink/config.rb', line 8

def api_key
  @api_key
end

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/bugsink/config.rb', line 8

def host
  @host
end

#project_idObject (readonly)

Returns the value of attribute project_id.



8
9
10
# File 'lib/bugsink/config.rb', line 8

def project_id
  @project_id
end

Instance Method Details

#authorization_headerObject



26
27
28
# File 'lib/bugsink/config.rb', line 26

def authorization_header
  { 'Authorization' => "Bearer #{api_key}" }
end

#project_id_present?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/bugsink/config.rb', line 42

def project_id_present?
  !project_id.nil?
end

#set_project_id(id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bugsink/config.rb', line 30

def set_project_id(id)
  if ENV['BUGSINK_PROJECT_ID'] && !ENV['BUGSINK_PROJECT_ID'].empty?
    # Don't write to file if env var is set (env var takes precedence)
    # Just update the in-memory value
    @project_id = id
    return
  end

  File.write(dotfile_path, "PROJECT_ID=#{id}\n")
  @project_id = id
end

#to_sObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bugsink/config.rb', line 46

def to_s
  project_display = if project_id
    source = ENV['BUGSINK_PROJECT_ID'] && !ENV['BUGSINK_PROJECT_ID'].empty? ? 'env' : 'file'
    "#{project_id} (from #{source})"
  else
    'not set'
  end

  <<~CONFIG
    BugSink Configuration:
      Host: #{host}
      API Key: #{api_key ? "#{api_key[0..8]}...#{api_key[-8..]}" : 'not set'}
      Project ID: #{project_display}
  CONFIG
end

#valid?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/bugsink/config.rb', line 18

def valid?
  !api_key.nil? && !api_key.empty?
end

#validate!Object

Raises:



22
23
24
# File 'lib/bugsink/config.rb', line 22

def validate!
  raise ConfigError, 'BUGSINK_API_KEY environment variable is required' unless valid?
end