class Spinna::Config

Takes care of reading the configuration file and exposing the settings.

Constants

DEFAULT_HISTORY_SIZE

Default number of albums to keep in the history.

DEFAULT_NUMBER_OF_PICKS

Default number of albums to pick when spinna is run.

Attributes

config_file[RW]

The name of the config file to load. (ex: config.yml)

data_dir[RW]

The directory where spinna stores its data.

history_size[RW]

The number previously picked albums to keep in the history. Defaults to DEFAULT_NUMBER_OF_PICKS.

number_of_picks[RW]

The number of picks to fetch if not explicitly given.

source_dir[RW]

The directory where all the albums are stored.

Public Class Methods

new(opts = {}) click to toggle source
# File lib/spinna/config.rb, line 30
def initialize(opts = {})
  @data_dir = opts[:data_dir] || ENV['SPINNA_DATA_DIR'] || File.join(Dir.home, '.spinna')
  @config_file = opts[:config_file] || 'config.yml'
  ensure_data_dir_exists
  ensure_configuration_exists
  parse_configuration
end

Private Instance Methods

config_file_exists?() click to toggle source
# File lib/spinna/config.rb, line 85
def config_file_exists?
  File.exists?(config_path)
end
config_path() click to toggle source
# File lib/spinna/config.rb, line 81
def config_path
  File.join(data_dir, config_file)
end
ensure_configuration_exists() click to toggle source
# File lib/spinna/config.rb, line 65
def ensure_configuration_exists
  unless config_file_exists?
    raise Spinna::ConfigFileNotFoundError, "Could not find the configuration file. Please create it at #{config_path}."
  end
end
ensure_data_dir_exists() click to toggle source
# File lib/spinna/config.rb, line 58
def ensure_data_dir_exists
  if !File.exists?(data_dir)
    Dir.mkdir(data_dir, 0700)
    raise Spinna::ConfigFileNotFoundError, "Could not find the configuration file. Please create it at #{config_path}."
  end
end
parse_configuration() click to toggle source
# File lib/spinna/config.rb, line 40
def parse_configuration
  config = read_configuration_file
  @source_dir = config['source_dir']
  @history_size = config['history_size'] || DEFAULT_HISTORY_SIZE
  @number_of_picks = config['number_of_picks'] || DEFAULT_NUMBER_OF_PICKS
  validate_config
end
read_configuration_file() click to toggle source
# File lib/spinna/config.rb, line 71
def read_configuration_file
  begin
    config = YAML.load_file(File.join(data_dir, config_file))
    raise StandardError unless config.is_a?(Hash)
  rescue StandardError
    raise Spinna::InvalidConfigFileError, "Could not parse the configuration file. Please make sure it is valid YAML."
  end
  config
end
validate_config() click to toggle source
# File lib/spinna/config.rb, line 48
def validate_config
  unless source_dir
    raise Spinna::InvalidConfigFileError, "You must set a source_dir in your configuration file."
  end

  unless Dir.exists?(source_dir)
    raise Spinna::InvalidSourceDirError, "The source_dir #{source_dir} does not exist."
  end
end