Class: DK::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/draftking/config.rb,
lib/draftking/config/config_setup.rb

Overview

Instance Configuration Methods

Class Method Summary collapse

Class Method Details

.available_configsObject

All .dkconfig files in home directory



66
67
68
69
# File 'lib/draftking/config.rb', line 66

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) ⇒ Object

Configure tumblr gem

Parameters:

  • file (String) (defaults to: nil)

    JSON File with API Keys

  • keys (Hash) (defaults to: nil)

    Hash with API Keys



19
20
21
22
23
24
25
26
27
28
# File 'lib/draftking/config.rb', line 19

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?
  Tumblr.configure do |config|
    api_keys.each do |key, value|
      config.send(:"#{key}=", value)
    end
  end
  true
end

.configured?Boolean

Does default configuration file exists

Returns:

  • (Boolean)


61
62
63
# File 'lib/draftking/config.rb', line 61

def self.configured?
  !available_configs.empty?
end

.delete_config(file) ⇒ Object

Delete a configuration file from the home directory



83
84
85
86
87
88
# File 'lib/draftking/config.rb', line 83

def self.delete_config(file)
  File.delete(home_path_file(file))
  true
rescue
  false
end

.get_inputObject

Get input without quotes



55
56
57
58
# File 'lib/draftking/config.rb', line 55

def self.get_input
  ARGV.clear
  gets.chomp.gsub(/[\'\"]/, '')
end

.home_path_file(fname) ⇒ Object

Path to file in home directory



72
73
74
# File 'lib/draftking/config.rb', line 72

def self.home_path_file(fname)
  File.join Dir.home, fname
end

.load_api_keys(file: nil) ⇒ Object

Read API Keys from file

Parameters:

  • file (String) (defaults to: nil)

    JSON File with API Keys



32
33
34
35
36
37
38
39
40
41
# File 'lib/draftking/config.rb', line 32

def self.load_api_keys(file: nil)
  file ||= home_path_file(DK::CONFIG_FILENAME)
  return nil unless File.exist?(file.to_s)
  keys = begin
    YAML.load_file(file)
  rescue
    YAML.parse_file(file)
  end
  validate_keys(keys)
end

.save_file(config:, account: '', mute: false) ⇒ Object

Save Configuration to File



44
45
46
47
48
49
50
51
52
# File 'lib/draftking/config.rb', line 44

def self.save_file(config:, account: '', mute: false)
   = '.' +  unless .empty?
  path = home_path_file( + DK::CONFIG_FILENAME)
  File.open(path, 'w') { |f| f.write YAML.dump config }
  puts "\nConfiguration saved to #{path} #{'(Default)' if .empty?}" unless mute
  path
rescue
  false
end

.setupObject

Walk user through setup process



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/draftking/config/config_setup.rb', line 5

def self.setup
  config = {}

  setup_display_instructions
  , as_default = setup_input_config_info
  setup_input_keys(config)

  # Save credentials
  save_file(config: config) if as_default.downcase.include?('y')
  save_file(config: config, account: )
end

.setup_display_instructionsObject

Setup instructions



18
19
20
21
22
23
# File 'lib/draftking/config/config_setup.rb', line 18

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_infoObject

Account input dialog



26
27
28
29
30
31
32
33
34
35
# File 'lib/draftking/config/config_setup.rb', line 26

def self.setup_input_config_info
  puts "\n * Configuration Settings *"
  print 'Enter configuration name (account name): '
   = get_input

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

  [, defconfig]
end

.setup_input_keys(config) ⇒ Object

API Key input dialog



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/draftking/config/config_setup.rb', line 38

def self.setup_input_keys(config)
  puts "\n * API Key Input *"
  print 'Enter consumer key: '
  config['consumer_key'] = get_input

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

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

  print 'Enter oath token secret: '
  config['oauth_token_secret'] = get_input
end

.switch_default_config(file, mute = false) ⇒ Object

Copy API Keys from alternate file to the default configuration file



77
78
79
80
# File 'lib/draftking/config.rb', line 77

def self.switch_default_config(file, mute = false)
  return true if file.eql? DK::CONFIG_FILENAME
  save_file config: load_api_keys(file: home_path_file(file)), mute: mute
end

.validate_keys(api_keys) ⇒ Object

Check that all 4 keys have been provided

Parameters:

  • api_keys (Hash)

    API Keys



8
9
10
11
12
13
14
# File 'lib/draftking/config.rb', line 8

def self.validate_keys(api_keys)
  return nil if api_keys.nil?
  return nil unless api_keys.respond_to?(:keys)
  return nil unless VALID_KEYS.all? { |k| api_keys.keys.include?(k) }
  return nil if api_keys.values.include?(nil)
  api_keys
end