Class: Configs

Inherits:
Object
  • Object
show all
Defined in:
lib/subcl/configs.rb

Constant Summary collapse

REQUIRED_SETTINGS =
%i{ server username password }
OPTIONAL_SETTINGS =
%i{ max_search_results notify_method random_song_count wildcard_order play_any_on_unknown_command}
DEFAULT_PATH =
File.expand_path('~/.subcl')
DEFAULT_CONFIG =
File.dirname(__FILE__) + "/../../share/subcl.default"
WILDCARD_ORDER_ITEMS =
%i{ song album artist playlist }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = DEFAULT_PATH) ⇒ Configs

Returns a new instance of Configs.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/subcl/configs.rb', line 14

def initialize(file = DEFAULT_PATH)
  @configs = {
    :notifyMethod => "auto",
    :play_any_on_unknown_command => false
  }

  @file = File.expand_path(file)
  unless File.file?(@file)
    if @file == DEFAULT_PATH and Configs.tty?
      ask_create_config
    else
      raise "Config file '#{@file}' not found"
    end
  end

  read_configs
end

Instance Attribute Details

#configsObject

Returns the value of attribute configs.



5
6
7
# File 'lib/subcl/configs.rb', line 5

def configs
  @configs
end

Class Method Details

.tty?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/subcl/configs.rb', line 112

def self.tty?
  system('tty -s')
end

Instance Method Details

#[](key) ⇒ Object



82
83
84
85
# File 'lib/subcl/configs.rb', line 82

def [](key)
  raise "Undefined setting #{key}" unless @configs.has_key? key
  @configs[key]
end

#[]=(key, val) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/subcl/configs.rb', line 87

def []=(key, val)
  settings = REQUIRED_SETTINGS + OPTIONAL_SETTINGS
  settings.each do |name|
    if key == name
      @configs[key] = val
      return
    end
  end
  raise "Undefined setting #{key}"
end

#ask_create_configObject



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

def ask_create_config
  $stderr.puts "No configuration found at #{DEFAULT_PATH}. Create one? [y/n]"
  if $stdin.gets.chomp =~ /[yY]/
    FileUtils.cp(DEFAULT_CONFIG, DEFAULT_PATH)
    $stderr.puts "Created #{DEFAULT_PATH}"
    exit 0
  end
  exit 4
end

#read_configsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/subcl/configs.rb', line 32

def read_configs
  settings = REQUIRED_SETTINGS + OPTIONAL_SETTINGS
  open(@file).each_line do |line|
    next if line.start_with? '#'
    next if line.chomp.empty?

    key, value = line.split(' ', 2)
    value.chomp!

    key = key.to_sym
    if settings.include? key
      @configs[key] = value
    else
      LOGGER.warn { "Unknown setting: '#{key}'" }
    end
  end

  validate_wildcard_order

  REQUIRED_SETTINGS.each do |setting|
    if @configs[setting].nil?
      raise "Missing setting '#{setting}'"
    end
  end
end

#to_hashObject



98
99
100
# File 'lib/subcl/configs.rb', line 98

def to_hash
  @configs
end

#validate_wildcard_orderObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/subcl/configs.rb', line 58

def validate_wildcard_order
  if @configs[:wildcard_order]
    raw_order = @configs[:wildcard_order]
    final_order = []
    raw_order.split(',').each do |item|
      item = item.to_sym
      if WILDCARD_ORDER_ITEMS.include? item
        final_order << item
      else
        LOGGER.warn("Invalid wildcard_order item #{item}")
      end
    end

    WILDCARD_ORDER_ITEMS.each do |item|
      unless final_order.include? item
        LOGGER.warn("wildcard_order is missing #{item}")
        final_order << item
      end
    end

    @configs[:wildcard_order] = final_order
  end
end