Class: DK::Config

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

Overview

Instance Configuration Methods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Config

Returns a new instance of Config.



38
39
40
41
42
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *_args) ⇒ Object

Easily access config opts



45
46
47
# File 'lib/draftking/config.rb', line 45

def method_missing(method, *_args)
  @config.send(method) unless @config.nil?
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



37
38
39
# File 'lib/draftking/config.rb', line 37

def config
  @config
end

#filenameObject

Returns the value of attribute filename.



37
38
39
# File 'lib/draftking/config.rb', line 37

def filename
  @filename
end

Class Method Details

.available_configsObject

All .dkconfig files in home directory



124
125
126
127
# 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) ⇒ Object

Configure Tumblr gem

Parameters:

  • file (String) (defaults to: nil)

    JSON File with API Keys

  • keys (Hash) (defaults to: nil)

    Hash with API Keys



81
82
83
84
85
86
87
88
89
90
91
# 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?Boolean

Does default configuration file exists

Returns:

  • (Boolean)


119
120
121
# File 'lib/draftking/config.rb', line 119

def self.configured?
  !available_configs.empty?
end

.delete_config(file) ⇒ Object

Delete a configuration file from the home directory



141
142
143
144
145
146
147
# 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_inputObject

Get input without quotes



113
114
115
116
# File 'lib/draftking/config.rb', line 113

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

.home_path_file(fname) ⇒ Object

Path to file in home directory



130
131
132
# File 'lib/draftking/config.rb', line 130

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



95
96
97
98
99
# 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

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

Save Configuration to File



102
103
104
105
106
107
108
109
110
# File 'lib/draftking/config.rb', line 102

def self.save_file(config:, account: '', mute: false, filename: nil)
   = '.' +  unless .empty?
  path = filename || home_path_file( + DK::CONFIG_FILENAME)
  File.open(path, 'w') { |f| f.write YAML.dump config.to_h }
  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
16
17
# File 'lib/draftking/config/config_setup.rb', line 5

def self.setup
  config = OpenStruct.new
  config.user_commands = []
  config.api_keys = {}

  setup_display_instructions
  , 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: )
end

.setup_display_instructionsObject

Setup instructions



20
21
22
23
24
25
# 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) ⇒ Object

Account input dialog



28
29
30
31
32
33
34
35
36
37
38
# 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): '
   = get_input
  config.config_name = 

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

  [, defconfig]
end

.setup_input_keys(config) ⇒ Object

API Key input dialog



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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) ⇒ Object

Copy API Keys from alternate file to the default configuration file



135
136
137
138
# 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) ⇒ Object

Check that all 4 keys have been provided

Parameters:

  • api_keys (Hash)

    API Keys



70
71
72
73
74
75
76
# 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

Instance Method Details

#is_default?Boolean

Is this configuration the current default?

Returns:

  • (Boolean)


64
65
66
# File 'lib/draftking/config.rb', line 64

def is_default?
  @filename == @def_conf
end

#load_config(file = nil) ⇒ Object

Load config from passed or default file



50
51
52
53
54
# 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) ⇒ Object

Contents of YAML file



57
58
59
60
61
# File 'lib/draftking/config.rb', line 57

def load_yaml_file(file)
  YAML.load_file(file)
rescue
  YAML.parse_file(file)
end