Class: Ayadn::Settings

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

Constant Summary collapse

CLIENT_ID =
"hFsCGArAjgJkYBHTHbZnUvzTmL4vaLHL"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



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

def config
  @config
end

.globalObject

Returns the value of attribute global.



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

def global
  @global
end

.optionsObject

Returns the value of attribute options.



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

def options
  @options
end

.user_tokenObject (readonly)

Returns the value of attribute user_token.



11
12
13
# File 'lib/ayadn/settings.rb', line 11

def user_token
  @user_token
end

Class Method Details

.check_for_accountsObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ayadn/settings.rb', line 55

def self.check_for_accounts
  sqlaccounts = Dir.home + "/ayadn/accounts.sqlite"
  status = Status.new
  if File.exist?(sqlaccounts)
    # Ayadn 2.x with already authorized account(s)
    return self.init_sqlite(sqlaccounts)
  else
    if File.exist?(Dir.home + "/ayadn/accounts.db")
      # Ayadn 1.x with already authorized account(s)
      status.deprecated_ayadn
      exit
    else
      # Ayadn without any authorized account (gem installed but no ~/ayadn folder)
      status.not_authorized
      exit
    end
  end
end

.config_fileObject



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

def self.config_file
  config_file = @config.paths.config + "/config.yml"
  if File.exist?(config_file)
    begin
      @options = Preferences.new(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.to_h)
    rescue => e
      Errors.global_error({error: e, caller: caller, data: []})
    end
  end
end

.create_api_fileObject



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ayadn/settings.rb', line 126

def self.create_api_file
  api_file = @config.paths.config + "/api.json"
  if File.exist?(api_file)
    # should be 48h in secs (172800)
    # but since ADN's API won't change any time soon...
    if ( File.ctime(api_file) < (Time.now - 604800) )
      self.new_api_file(api_file)
    end
  else
    self.new_api_file(api_file)
  end
  self.read_api(api_file)
end

.create_version_fileObject



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

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

.get_tokenObject



78
79
80
81
82
83
84
85
# File 'lib/ayadn/settings.rb', line 78

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

.has_token_file?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/ayadn/settings.rb', line 101

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

.init_configObject



87
88
89
90
91
92
93
94
95
# File 'lib/ayadn/settings.rb', line 87

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

.init_sqlite(sqlaccounts) ⇒ Object



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

def self.init_sqlite(sqlaccounts)
  Databases.(Amalgalite::Database.new(sqlaccounts))
end

.load_configObject



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
47
48
49
50
51
52
53
# File 'lib/ayadn/settings.rb', line 14

def self.load_config
  active = self.check_for_accounts
  if active.blank?
    Status.new.not_authorized
    exit
  end
  home = active[3]
  api_file = Dir.home + "/ayadn/.api.yml"
  baseURL = if File.exist?(api_file)
    YAML.load(File.read(api_file))[:root]
  else
    "https://api.app.net"
  end
  config_hash = {
    paths: {
      home: home,
      log: "#{home}/log",
      db: "#{home}/db",
      config: "#{home}/config",
      auth: "#{home}/auth",
      downloads: "#{home}/downloads",
      posts: "#{home}/posts",
      messages: "#{home}/messages",
      lists: "#{home}/lists"
    },
    identity: {
      id: active[1],
      username: active[0],
      handle: active[2]
    },
    api: {
      baseURL: baseURL
    }
  }
  @config = JSON.parse(config_hash.to_json, object_class: OpenStruct)
  global_hash = {scrolling: false, force: false}
  @global = JSON.parse(global_hash.to_json, object_class: OpenStruct)

  @options = Preferences.new(self.defaults)
end

.read_token_fileObject



105
106
107
# File 'lib/ayadn/settings.rb', line 105

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

.restore_defaultsObject



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

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

.save_configObject



97
98
99
# File 'lib/ayadn/settings.rb', line 97

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