class Feed2Email::Config

Constants

SEND_METHODS

Public Class Methods

new(path) click to toggle source
# File lib/feed2email/config.rb, line 20
def initialize(path)
  @path = path
end

Private Instance Methods

check_data_type() click to toggle source
# File lib/feed2email/config.rb, line 26
def check_data_type
  if !data.is_a?(Hash)
    raise InvalidConfigDataTypeError,
      "Invalid data type (not a Hash) for config file #{path}"
  end
end
check_existence() click to toggle source
# File lib/feed2email/config.rb, line 33
def check_existence
  if !File.exist?(path)
    raise MissingConfigError, "Missing config file #{path}"
  end
end
check_file() click to toggle source
# File lib/feed2email/config.rb, line 39
def check_file
  check_existence
  check_permissions
  check_syntax
  check_data_type
end
check_option(option) click to toggle source
# File lib/feed2email/config.rb, line 46
def check_option(option)
  if config[option].nil?
    raise MissingConfigOptionError,
      "Option #{option} missing from config file #{path}"
  end
end
check_options() click to toggle source
# File lib/feed2email/config.rb, line 53
def check_options
  check_recipient
  check_sender
  check_send_method
  check_smtp_options if config['send_method'] == 'smtp'
end
check_permissions() click to toggle source
# File lib/feed2email/config.rb, line 60
def check_permissions
  if '%o' % (File.stat(path).mode & 0777) != '600'
    raise InvalidConfigPermissionsError,
      'Invalid permissions for config file' +
      "\nTo fix it, issue: chmod 600 #{path}"
  end
end
check_recipient() click to toggle source
# File lib/feed2email/config.rb, line 68
def check_recipient
  check_option('recipient')
end
check_send_method() click to toggle source
# File lib/feed2email/config.rb, line 72
def check_send_method
  unless SEND_METHODS.include?(config['send_method'])
    raise InvalidConfigOptionError,
      "Option send_method not one of: #{SEND_METHODS.join(' ')}"
  end
end
check_sender() click to toggle source
# File lib/feed2email/config.rb, line 79
def check_sender
  check_option('sender')
end
check_smtp_options() click to toggle source
# File lib/feed2email/config.rb, line 83
def check_smtp_options
  check_option('smtp_host')
  check_option('smtp_port')
  check_option('smtp_user')
  check_option('smtp_pass')
end
check_syntax() click to toggle source
# File lib/feed2email/config.rb, line 90
def check_syntax
  begin
    data
  rescue Psych::SyntaxError
    raise InvalidConfigSyntaxError,
      "Invalid YAML syntax for config file #{path}"
  end
end
config() click to toggle source
# File lib/feed2email/config.rb, line 99
def config
  return @config if @config

  begin
    check_file
    @config = defaults.merge(data)
    check_options
  rescue ConfigError => e
    abort e.message
  end

  @config
end
data() click to toggle source
# File lib/feed2email/config.rb, line 113
def data
  @data ||= YAML.load(File.read(path))
end
defaults() click to toggle source
# File lib/feed2email/config.rb, line 117
def defaults
  {
    'log_level'      => 'info',
    'log_path'       => true,
    'log_shift_age'  => 0,
    'log_shift_size' => 1, # megabyte
    'mail_path'      => File.join(ENV['HOME'], 'Mail'),
    'max_entries'    => 20,
    'send_delay'     => 10,
    'send_method'    => 'file',
    'sendmail_path'  => '/usr/sbin/sendmail',
    'smtp_auth'      => 'login',
    'smtp_starttls'  => true,
  }
end
path() click to toggle source
# File lib/feed2email/config.rb, line 133
def path; @path end