Class: DownloadTV::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/download_tv/configuration.rb

Overview

Class used for managing the configuration of the application

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content = {}, force_change = false) ⇒ Configuration

Returns a new instance of Configuration.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/download_tv/configuration.rb', line 9

def initialize(content = {}, force_change = false)
  FileUtils.mkdir_p(File.join(ENV['HOME'], '.config', 'download_tv'))
  @config_path = content[:path] || default_config_path

  if File.exist? @config_path
    load_config
    @content.merge!(content) unless content.empty?
    @content[:ignored]&.map!(&:downcase)
    change_configuration if force_change
  else
    @content = content
    change_configuration
  end
end

Instance Attribute Details

#config_pathObject (readonly)

Returns the value of attribute config_path.



7
8
9
# File 'lib/download_tv/configuration.rb', line 7

def config_path
  @config_path
end

#contentObject (readonly)

Returns the value of attribute content.



7
8
9
# File 'lib/download_tv/configuration.rb', line 7

def content
  @content
end

Instance Method Details

#breaking_changes?(version) ⇒ Boolean

Returns true if a major or minor update has been detected Returns false if a patch has been detected Returns nil if it’s the same version

Returns:

  • (Boolean)


131
132
133
134
135
136
# File 'lib/download_tv/configuration.rb', line 131

def breaking_changes?(version)
  DownloadTV::VERSION.split('.')
                     .zip(version.split('.'))
                     .find_index { |x, y| y < x }
                     &.< 2
end

#change_configurationObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/download_tv/configuration.rb', line 24

def change_configuration
  prompt_for_myep_user
  prompt_for_cookie
  prompt_for_ignored
  prompt_for_filters
  $stdout.flush

  set_default_values
  serialize
end

#clear_pendingObject



146
147
148
149
# File 'lib/download_tv/configuration.rb', line 146

def clear_pending
  @content[:pending].clear
  serialize
end

#default_config_pathObject



123
124
125
# File 'lib/download_tv/configuration.rb', line 123

def default_config_path
  File.join(ENV['HOME'], '.config', 'download_tv', 'config')
end

#default_filtersObject



86
87
88
89
90
91
# File 'lib/download_tv/configuration.rb', line 86

def default_filters
  {
    'includes' => %w[PROPER REPACK],
    'excludes' => %w[2160P 1080P 720P]
  }
end

#load_configObject



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/download_tv/configuration.rb', line 110

def load_config
  source = File.read(@config_path)
  @content = JSON.parse(source, symbolize_names: true)

  @content[:date] = Date.parse(@content[:date]) if @content[:date]

  change_configuration if !@content[:version] || breaking_changes?(@content[:version])
rescue JSON::ParserError
  @content = {}
  change_configuration
  retry
end


142
143
144
# File 'lib/download_tv/configuration.rb', line 142

def print_attr(arg)
  puts @content[arg]
end


138
139
140
# File 'lib/download_tv/configuration.rb', line 138

def print_config
  @content.each { |k, v| puts "#{k}: #{v}" }
end


45
46
47
48
# File 'lib/download_tv/configuration.rb', line 45

def prompt_for_cookie
  print 'Save cookie? (y)/n: '
  @content[:cookie] = !($stdin.gets.chomp.casecmp? 'n')
end

#prompt_for_filtersObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/download_tv/configuration.rb', line 64

def prompt_for_filters
  puts "Current filters: (#{@content[:filters]})" if @content[:filters]

  @content[:filters] = {}

  puts 'Enter a comma-separated list of terms to include: '

  @content[:filters][:includes] = $stdin.gets
                                        .chomp
                                        .split(',')
                                        .map(&:strip)
                                        .map(&:upcase)

  puts 'Enter a comma-separated list of terms to exclude: '

  @content[:filters][:excludes] = $stdin.gets
                                        .chomp
                                        .split(',')
                                        .map(&:strip)
                                        .map(&:upcase)
end

#prompt_for_ignoredObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/download_tv/configuration.rb', line 50

def prompt_for_ignored
  if @content[:ignored]
    puts "Enter a comma-separated list of shows to ignore: (#{@content[:ignored]})"
  else
    puts 'Enter a comma-separated list of shows to ignore: '
  end

  @content[:ignored] = $stdin.gets
                             .chomp
                             .split(',')
                             .map(&:strip)
                             .map(&:downcase)
end

#prompt_for_myep_userObject



35
36
37
38
39
40
41
42
43
# File 'lib/download_tv/configuration.rb', line 35

def prompt_for_myep_user
  if @content[:myepisodes_user]
    print "Enter your MyEpisodes username (#{@content[:myepisodes_user]}) : "
  else
    print 'Enter your MyEpisodes username: '
  end
  input = $stdin.gets.chomp
  @content[:myepisodes_user] = input if input
end

#queue_pending(show) ⇒ Object



151
152
153
154
# File 'lib/download_tv/configuration.rb', line 151

def queue_pending(show)
  @content[:pending] << show
  serialize
end

#serializeObject



105
106
107
108
# File 'lib/download_tv/configuration.rb', line 105

def serialize
  @content[:pending] = @content[:pending].uniq
  File.write(@config_path, JSON.generate(@content))
end

#set_default_valuesObject



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/download_tv/configuration.rb', line 93

def set_default_values
  # When modifying existing config, keeps previous values
  # When creating new one, sets defaults
  @content[:auto] ||= true
  @content[:subs] ||= true
  @content[:grabber] ||= 'TorrentAPI'
  @content[:date] ||= Date.today - 1
  @content[:filters] ||= default_filters
  @content[:pending] ||= []
  @content[:version] = DownloadTV::VERSION
end