Class: Ayadn::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/ayadn/settings.rb

Constant Summary collapse

AYADN_CLIENT_ID =
"hFsCGArAjgJkYBHTHbZnUvzTmL4vaLHL"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



8
9
10
# File 'lib/ayadn/settings.rb', line 8

def config
  @config
end

.default_nrObject (readonly)

Returns the value of attribute default_nr.



9
10
11
# File 'lib/ayadn/settings.rb', line 9

def default_nr
  @default_nr
end

.optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/ayadn/settings.rb', line 8

def options
  @options
end

.user_tokenObject (readonly)

Returns the value of attribute user_token.



9
10
11
# File 'lib/ayadn/settings.rb', line 9

def user_token
  @user_token
end

Class Method Details

.check_for_accounts(acc_db) ⇒ Object



48
49
50
51
52
53
# File 'lib/ayadn/settings.rb', line 48

def self.check_for_accounts(acc_db)
  unless File.exist?(acc_db)
    puts Status.not_authorized
    exit
  end
end

.config_fileObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ayadn/settings.rb', line 86

def self.config_file
  config_file = @config[:paths][:config] + "/config.yml"
  if File.exist?(config_file)
    begin
      conf = YAML.load(File.read(config_file))

      # force delete obsolete keys (because legacy versions of the config file)
      conf[:timeline].delete_if {|k,_| k == :show_nicerank}
      conf[:colors].delete_if {|k,_| k == :nicerank}
      # force create mandatory keys (idem)
      conf[:nicerank] = @default_nr if conf[:nicerank].nil? || conf[:nicerank].size != 4
      conf[:timeline][:show_debug] = false if conf[:timeline][:show_debug].nil?
      conf[:timeline][:show_spinner] = true if conf[:timeline][:show_spinner].nil?
      conf[:colors][:debug] = :red if conf[:colors][:debug].nil?
      conf[:colors][:unread] = :cyan if conf[:colors][:unread].nil?
      conf[:nowplaying] = {} if conf[:nowplaying].nil?
      conf[:movie] = {hashtag: 'nowwatching'} if conf[:movie].nil?
      conf[:tvshow] = {hashtag: 'nowwatching'} if conf[:tvshow].nil?
      conf[:formats][:list] = {reverse: true} if conf[:formats][:list].nil?
      conf[:timeline][:compact] = false if conf[:timeline][:compact].nil?
      conf[:timeline][:show_channel_oembed] = true if conf[:timeline][:show_channel_oembed].nil?
      conf[:marker] = {update_messages: true} if conf[:marker].nil?

      @options = conf
      self.write_config_file(config_file, @options)
    rescue => e
      Errors.global_error({error: e, caller: caller, data: []})
    end
  else
    begin
      self.write_config_file(config_file, @options)
    rescue => e
      Errors.global_error({error: e, caller: caller, data: []})
    end
  end
end

.create_api_fileObject



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ayadn/settings.rb', line 123

def self.create_api_file
  api_file = @config[:paths][:config] + "/api.json"
  if File.exist?(api_file)
    if ( File.ctime(api_file) < (Time.now - 172800) ) #48h in secs
      self.new_api_file(api_file)
    end
  else
    self.new_api_file(api_file)
  end
  self.read_api(api_file)
end

.create_version_fileObject



135
136
137
# File 'lib/ayadn/settings.rb', line 135

def self.create_version_file
  File.write(@config[:paths][:config] + "/version.yml", {version: @config[:version]}.to_yaml)
end

.get_tokenObject



55
56
57
58
59
60
61
62
# File 'lib/ayadn/settings.rb', line 55

def self.get_token
  if self.has_token_file?
    @user_token = self.read_token_file
  else
    puts Status.not_authorized
    exit
  end
end

.has_token_file?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/ayadn/settings.rb', line 78

def self.has_token_file?
  File.exist?(@config[:paths][:auth] + "/token")
end

.init_configObject



64
65
66
67
68
69
70
71
72
# File 'lib/ayadn/settings.rb', line 64

def self.init_config
  @config[:version] = VERSION
  @config[:platform] = RbConfig::CONFIG['host_os']
  @config[:ruby] = RUBY_VERSION
  @config[:locale] = ENV["LANG"]
  self.config_file
  self.create_api_file
  self.create_version_file
end

.load_configObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ayadn/settings.rb', line 12

def self.load_config
  acc_db = Dir.home + "/ayadn/accounts.db"
  self.check_for_accounts(acc_db)
  db = Databases.init acc_db
  active = db['ACTIVE']
  home = db[active][:path]
  @config = {
    paths: {
      home: home,
      log: "#{home}/log",
      db: "#{home}/db",
      pagination: "#{home}/pagination",
      config: "#{home}/config",
      auth: "#{home}/auth",
      downloads: "#{home}/downloads",
      backup: "#{home}/backup",
      posts: "#{home}/posts",
      messages: "#{home}/messages",
      lists: "#{home}/lists"
    },
    identity: {
      id: db[active][:id],
      username: db[active][:username],
      handle: db[active][:handle]
    }
  }
  @default_nr = {
    threshold: 2.1,
    cache: 48,
    filter: true,
    filter_unranked: false
  }
  db.close
  @options = self.defaults
end

.read_token_fileObject



82
83
84
# File 'lib/ayadn/settings.rb', line 82

def self.read_token_file
  File.read(@config[:paths][:auth] + "/token")
end

.restore_defaultsObject



139
140
141
142
# File 'lib/ayadn/settings.rb', line 139

def self.restore_defaults
  self.load_config
  File.write(@config[:paths][:config] + "/config.yml", @options.to_yaml)
end

.save_configObject



74
75
76
# File 'lib/ayadn/settings.rb', line 74

def self.save_config
  File.write(@config[:paths][:config] + "/config.yml", @options.to_yaml)
end