class DK::Config
Instance Configuration Methods
Instance Configuration Methods
Attributes
Public Class Methods
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 @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
Does default configuration file exists
# File lib/draftking/config.rb, line 119 def self.configured? !available_configs.empty? end
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 without quotes
# File lib/draftking/config.rb, line 113 def self.get_input ARGV.clear gets.chomp.gsub(/[\'\"]/, '') end
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
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
# 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 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
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 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
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
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
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
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 this configuration the current default?
# File lib/draftking/config.rb, line 64 def is_default? @filename == @def_conf end
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
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
Easily access config opts
# File lib/draftking/config.rb, line 45 def method_missing(method, *_args) @config.send(method) unless @config.nil? end