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

.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



42
43
44
45
46
47
# File 'lib/ayadn/settings.rb', line 42

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

.config_fileObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ayadn/settings.rb', line 78

def self.config_file
  config_file = @config[:paths][:config] + "/config.yml"
  if File.exist?(config_file)
    # TODO: system to merge existing config file when future category are added
    begin
      @options = YAML.load(File.read(config_file))
    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



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ayadn/settings.rb', line 96

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



108
109
110
# File 'lib/ayadn/settings.rb', line 108

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

.defaultsObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
# File 'lib/ayadn/settings.rb', line 144

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_nicerank: false,
      show_spinner: true,
      show_debug: false
    },
    counts: {
      default: 50,
      unified: 50,
      global: 50,
      checkins: 50,
      conversations: 50,
      photos: 50,
      trending: 50,
      mentions: 50,
      convo: 50,
      posts: 50,
      messages: 50,
      search: 100,
      whoreposted: 50,
      whostarred: 50,
      whatstarred: 100,
      files: 66
    },
    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,
      nicerank: :cyan,
      debug: :red
    },
    backup: {
      auto_save_sent_posts: false,
      auto_save_sent_messages: false,
      auto_save_lists: false
    },
    scroll: {
      timer: 3
    },
    nicerank: {
      threshold: 2.1,
      cache: 48,
      filter: true,
      filter_unranked: false
    }
  }
end

.get_tokenObject



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

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)


70
71
72
# File 'lib/ayadn/settings.rb', line 70

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

.has_version_file?Boolean

Returns:

  • (Boolean)


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

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

.init_configObject



58
59
60
61
62
63
64
# File 'lib/ayadn/settings.rb', line 58

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
# 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]
    }
  }
  db.close
  @options = self.defaults
end

.new_api_file(api_file) ⇒ Object



119
120
121
122
123
124
# File 'lib/ayadn/settings.rb', line 119

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



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

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



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

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

.read_version_fileObject



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

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

.restore_defaultsObject



112
113
114
115
# File 'lib/ayadn/settings.rb', line 112

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

.save_configObject



66
67
68
# File 'lib/ayadn/settings.rb', line 66

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

.write_config_file(config_file, options) ⇒ Object



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

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