class Geordi::Settings

Constants

ALLOWED_GLOBAL_SETTINGS
ALLOWED_LOCAL_SETTINGS
GLOBAL_SETTINGS_FILE_NAME
LOCAL_SETTINGS_FILE_NAME
SETTINGS_WARNED

Public Class Methods

new() click to toggle source
# File lib/geordi/settings.rb, line 16
def initialize
  read_settings
end

Public Instance Methods

auto_update_chromedriver() click to toggle source
# File lib/geordi/settings.rb, line 30
def auto_update_chromedriver
  @global_settings["auto_update_chromedriver"] || false
end
auto_update_chromedriver=(value) click to toggle source
# File lib/geordi/settings.rb, line 34
def auto_update_chromedriver=(value)
  @global_settings['auto_update_chromedriver'] = value
  save_global_settings
end
pivotal_tracker_api_key() click to toggle source

Global settings

# File lib/geordi/settings.rb, line 21
def pivotal_tracker_api_key
  @global_settings['pivotal_tracker_api_key'] || gitpt_api_key_old || inquire_pt_api_key
end
pivotal_tracker_api_key=(value) click to toggle source
# File lib/geordi/settings.rb, line 25
def pivotal_tracker_api_key=(value)
  @global_settings['pivotal_tracker_api_key'] = value
  save_global_settings
end
pivotal_tracker_project_ids() click to toggle source
# File lib/geordi/settings.rb, line 39
def pivotal_tracker_project_ids
  local_project_ids = @local_settings['pivotal_tracker_project_ids'] || pt_project_ids_old
  global_project_ids = @global_settings['pivotal_tracker_project_ids']

  local_project_ids = array_wrap_project_ids(local_project_ids)
  global_project_ids = array_wrap_project_ids(global_project_ids)

  project_ids = local_project_ids | global_project_ids

  if project_ids.empty?
    puts
    Geordi::Interaction.warn "Sorry, I could not find a project ID in .geordi.yml :("
    puts

    puts "Please put at least one Pivotal Tracker project id into the .geordi.yml file in this directory, e.g."
    puts
    puts "pivotal_tracker_project_ids:"
    puts "- 123456"
    puts
    puts 'You may add multiple IDs.'
    exit 1
  end

  project_ids
end

Private Instance Methods

array_wrap_project_ids(project_ids) click to toggle source
# File lib/geordi/settings.rb, line 150
def array_wrap_project_ids(project_ids)
  case project_ids
  when Array
    project_ids
  when String
    project_ids.split(/[\s]+/).map(&:to_i)
  when Integer
    [project_ids]
  else
    []
  end
end
check_for_invalid_keys(settings, allowed_keys, file) click to toggle source
# File lib/geordi/settings.rb, line 91
def check_for_invalid_keys(settings, allowed_keys, file)
  return if settings.nil?

  invalid_keys = settings.keys - allowed_keys
  unless invalid_keys.empty?
    Interaction.warn "Unknown settings in #{file}: #{invalid_keys.join(", ")}"
    Interaction.note "Supported settings in #{file} are: #{allowed_keys.join(", ")}"
  end
end
gitpt_api_key_old() click to toggle source

deprecated

# File lib/geordi/settings.rb, line 111
def gitpt_api_key_old
  file_path = File.join(ENV['HOME'], '.gitpt')
  if File.exist?(file_path) && !Util.testing?
    token = YAML.load_file(file_path).fetch :token
    self.pivotal_tracker_api_key = token

    Geordi::Interaction.warn "The ~/.gitpt file is deprecated."
    Geordi::Interaction.note "The contained setting has been moved to ~/.config/geordi/global.yml."
    Geordi::Interaction.note "If you don't need to work with an older version of geordi you can delete ~/.gitpt now."

    token
  end
end
inquire_pt_api_key() click to toggle source
# File lib/geordi/settings.rb, line 125
def inquire_pt_api_key
  Geordi::Interaction.warn 'Your settings are missing or invalid.'
  Geordi::Interaction.warn "Please configure your Pivotal Tracker access."
  token = Geordi::Interaction.prompt('Your API key:').to_s # Just be sure
  self.pivotal_tracker_api_key = token
  puts

  token
end
pt_project_ids_old() click to toggle source

deprecated

# File lib/geordi/settings.rb, line 136
    def pt_project_ids_old
      if File.exist?('.pt_project_id')
        project_ids = File.read('.pt_project_id')
        puts # Make sure to start on a new line (e.g. when invoked after a #print)
        Geordi::Interaction.warn "The usage of the .pt_project_id file is deprecated."
        Geordi::Interaction.note(<<~INSTRUCTIONS)
          Please remove this file from your project and add or extend the .geordi.yml file with the following content:
            pivotal_tracker_project_ids: #{project_ids}
        INSTRUCTIONS

        project_ids
      end
    end
read_settings() click to toggle source
# File lib/geordi/settings.rb, line 67
def read_settings
  global_path = GLOBAL_SETTINGS_FILE_NAME
  local_path = LOCAL_SETTINGS_FILE_NAME

  global_settings = if File.exists?(global_path)
    YAML.safe_load(File.read(global_path))
  end
  local_settings = if File.exists?(local_path)
    YAML.safe_load(File.read(local_path))
  end

  # Prevent duplicate warnings caused by another instance of Settings
  unless ENV[SETTINGS_WARNED]
    check_for_invalid_keys(global_settings, ALLOWED_GLOBAL_SETTINGS, global_path)
    check_for_invalid_keys(local_settings, ALLOWED_LOCAL_SETTINGS, local_path)
    Interaction.warn "Unsupported config file \".firefox-version\". Please remove it." if File.exists?('.firefox-version')

    ENV[SETTINGS_WARNED] = 'true'
  end

  @global_settings = global_settings || {}
  @local_settings = local_settings || {}
end
save_global_settings() click to toggle source
# File lib/geordi/settings.rb, line 101
def save_global_settings
  global_path = GLOBAL_SETTINGS_FILE_NAME
  global_directory = File.dirname(global_path)
  FileUtils.mkdir_p(global_directory) unless File.directory? global_directory
  File.open(global_path, 'w') do |file|
    file.write @global_settings.to_yaml
  end
end