class DK::Config

Instance Configuration Methods

Instance Configuration Methods

Attributes

config[RW]
filename[RW]

Public Class Methods

available_configs() click to toggle source

All .dkconfig files in home directory

# File lib/draftking/config.rb, line 124
def self.available_configs
  glob = home_path_file('*' + DK::CONFIG_FILENAME)
  Dir.glob(glob, File::FNM_DOTMATCH).map { |f| f.split('/').last }
end
configure_tumblr_gem(file: nil, keys: nil) click to toggle source

Configure Tumblr gem @param file [String] JSON File with API Keys @param keys [Hash] Hash with API Keys

# File lib/draftking/config.rb, line 81
def self.configure_tumblr_gem(file: nil, keys: nil)
  api_keys = keys || load_api_keys(file: file) || load_api_keys(file: home_path_file(available_configs.first))
  return false if api_keys.nil?
  return false unless api_keys.size == 4
  Tumblr.configure do |config|
    api_keys.each do |key, value|
      config.send(:"#{key}=", value)
    end
  end
  true
end
configured?() click to toggle source

Does default configuration file exists

# File lib/draftking/config.rb, line 119
def self.configured?
  !available_configs.empty?
end
delete_config(file) click to toggle source

Delete a configuration file from the home directory

# File lib/draftking/config.rb, line 141
def self.delete_config(file)
  return false unless File.exist?(home_path_file(file))
  File.delete(home_path_file(file))
  true
rescue
  false
end
get_input() click to toggle source

Get input without quotes

# File lib/draftking/config.rb, line 113
def self.get_input
  ARGV.clear
  gets.chomp.gsub(/[\'\"]/, '')
end
home_path_file(fname) click to toggle source

Path to file in home directory

# File lib/draftking/config.rb, line 130
def self.home_path_file(fname)
  File.join Dir.home, fname
end
load_api_keys(file: nil) click to toggle source

Read API Keys from file @param file [String] JSON File with API Keys

# File lib/draftking/config.rb, line 95
def self.load_api_keys(file: nil)
  file ||= home_path_file(DK::CONFIG_FILENAME)
  exec('dk setup') unless File.exist?(file.to_s)
  validate_keys(DK::Config.new(file: file).api_keys)
end
new(opts = {}) click to toggle source
# File lib/draftking/config.rb, line 38
def initialize(opts = {})
  @filename = opts[:file]
  @config   = OpenStruct.new(load_config(@filename))
  @def_conf = self.class.home_path_file(DK::CONFIG_FILENAME)
end
save_file(config:, account: '', mute: false, filename: nil) click to toggle source

Save Configuration to File

# File lib/draftking/config.rb, line 102
def self.save_file(config:, account: '', mute: false, filename: nil)
  account = '.' + account unless account.empty?
  path = filename || home_path_file(account + DK::CONFIG_FILENAME)
  File.open(path, 'w') { |f| f.write YAML.dump config.to_h }
  puts "\nConfiguration saved to #{path} #{'(Default)' if account.empty?}" unless mute
  path
rescue
  false
end
setup() click to toggle source

Walk user through setup process

# File lib/draftking/config/config_setup.rb, line 5
def self.setup
  config = OpenStruct.new
  config.user_commands = []
  config.api_keys = {}

  setup_display_instructions
  account, as_default = setup_input_config_info(config)
  setup_input_keys(config)

  # Save credentials
  save_file(config: config) if as_default.downcase.include?('y')
  save_file(config: config, account: account)
end
setup_display_instructions() click to toggle source

Setup instructions

# File lib/draftking/config/config_setup.rb, line 20
def self.setup_display_instructions
  puts "\n * Instructions *"
  puts '1. Register a new application for your Tumblr account at https://www.tumblr.com/oauth/apps'
  puts '2. Once complete, browse to https://api.tumblr.com/console/calls/user/info'
  puts '     to get your API keys.'
end
setup_input_config_info(config) click to toggle source

Account input dialog

# File lib/draftking/config/config_setup.rb, line 28
def self.setup_input_config_info(config)
  puts "\n * Configuration Settings *"
  print 'Enter configuration name (account name): '
  account = get_input
  config.config_name = account

  print 'Use this as your default config? (y/N): '
  defconfig = get_input.downcase

  [account, defconfig]
end
setup_input_keys(config) click to toggle source

API Key input dialog

# File lib/draftking/config/config_setup.rb, line 41
def self.setup_input_keys(config)
  puts "\n * API Key Input *"
  print 'Enter consumer key: '
  config.api_keys['consumer_key'] = get_input

  print 'Enter consumer secret: '
  config.api_keys['consumer_secret'] = get_input

  print 'Enter oath token: '
  config.api_keys['oauth_token'] = get_input

  print 'Enter oath token secret: '
  config.api_keys['oauth_token_secret'] = get_input
end
switch_default_config(file, mute = false) click to toggle source

Copy API Keys from alternate file to the default configuration file

# File lib/draftking/config.rb, line 135
def self.switch_default_config(file, mute = false)
  return true if file.eql? DK::CONFIG_FILENAME
  save_file config: DK::Config.new(file: home_path_file(file)).config, mute: mute
end
validate_keys(api_keys) click to toggle source

Check that all 4 keys have been provided @param api_keys [Hash] API Keys

# File lib/draftking/config.rb, line 70
def self.validate_keys(api_keys)
  return nil if api_keys.nil?
  return nil unless api_keys.respond_to?(:keys)
  return {}  unless api_keys.keys.all? { |k| VALID_KEYS.include?(k.to_s) }
  return {}  if api_keys.values.include?(nil)
  api_keys
end

Public Instance Methods

is_default?() click to toggle source

Is this configuration the current default?

# File lib/draftking/config.rb, line 64
def is_default?
  @filename == @def_conf
end
load_config(file = nil) click to toggle source

Load config from passed or default file

# File lib/draftking/config.rb, line 50
def load_config(file = nil)
  file ||= self.class.home_path_file(DK::CONFIG_FILENAME)
  return nil unless File.exist?(file.to_s)
  ConfigAdapter.new(load_yaml_file(file), file).adapt
end
load_yaml_file(file) click to toggle source

Contents of YAML file

# File lib/draftking/config.rb, line 57
def load_yaml_file(file)
  YAML.load_file(file)
rescue
  YAML.parse_file(file)
end
method_missing(method, *_args) click to toggle source

Easily access config opts

# File lib/draftking/config.rb, line 45
def method_missing(method, *_args)
  @config.send(method) unless @config.nil?
end