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



84
85
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
# File 'lib/ayadn/settings.rb', line 84

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[:nowplaying] = {} if conf[:nowplaying].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



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ayadn/settings.rb', line 114

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



126
127
128
# File 'lib/ayadn/settings.rb', line 126

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

.defaultsObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/ayadn/settings.rb', line 162

def self.defaults
  {
    timeline: {
      directed: 1,
      deleted: 0,
      html: 0,
      annotations: 1,
      show_source: true,
      show_symbols: true,
      show_real_name: true,
      show_date: true,
      show_spinner: true,
      show_debug: false
    },
    counts: {
      default: 50,
      unified: 100,
      global: 100,
      checkins: 100,
      conversations: 50,
      photos: 50,
      trending: 100,
      mentions: 100,
      convo: 100,
      posts: 100,
      messages: 50,
      search: 200,
      whoreposted: 50,
      whostarred: 50,
      whatstarred: 100,
      files: 100
    },
    formats: {
      table: {
        width: 75
      }
    },
    colors: {
      id: :blue,
      index: :red,
      username: :green,
      name: :magenta,
      date: :cyan,
      link: :yellow,
      dots: :blue,
      hashtags: :cyan,
      mentions: :red,
      source: :cyan,
      symbols: :green,
      debug: :red
    },
    backup: {
      auto_save_sent_posts: false,
      auto_save_sent_messages: false,
      auto_save_lists: false
    },
    scroll: {
      timer: 3
    },
    nicerank: @default_nr,
    nowplaying: {}
  }
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)


76
77
78
# File 'lib/ayadn/settings.rb', line 76

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

.has_version_file?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/ayadn/settings.rb', line 150

def self.has_version_file?
  File.exist?(@config[:paths][:config] + "/version.yml")
end

.init_configObject



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

def self.init_config
  @config[:version] = VERSION
  @config[:platform] = RbConfig::CONFIG['host_os']
  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

.new_api_file(api_file) ⇒ Object



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

def self.new_api_file(api_file)
  api = API.new
  resp = api.get_config
  api.check_response_meta_code(resp)
  File.write(api_file, resp['data'].to_json)
end

.read_api(api_file) ⇒ Object



144
145
146
147
148
# File 'lib/ayadn/settings.rb', line 144

def self.read_api(api_file)
  content = JSON.parse(File.read(api_file))
  @config[:post_max_length] = content['post']['text_max_length']
  @config[:message_max_length] = content['message']['text_max_length']
end

.read_token_fileObject



80
81
82
# File 'lib/ayadn/settings.rb', line 80

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

.read_version_fileObject



154
155
156
# File 'lib/ayadn/settings.rb', line 154

def self.read_version_file
  YAML.load(File.read(@config[:paths][:config] + "/version.yml"))
end

.restore_defaultsObject



130
131
132
133
# File 'lib/ayadn/settings.rb', line 130

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

.save_configObject



72
73
74
# File 'lib/ayadn/settings.rb', line 72

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

.write_config_file(config_file, options) ⇒ Object



158
159
160
# File 'lib/ayadn/settings.rb', line 158

def self.write_config_file(config_file, options)
  File.write(config_file, options.to_yaml)
end