class Ayadn::Settings

Constants

CLIENT_ID

Attributes

config[RW]
global[RW]
options[RW]
user_token[R]

Public Class Methods

check_for_accounts() click to toggle source
# 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_file() click to toggle source
# 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_file() click to toggle source
# 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_file() click to toggle source
# 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_token() click to toggle source
# 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?() click to toggle source
# File lib/ayadn/settings.rb, line 101
def self.has_token_file?
  File.exist?(@config.paths.auth + "/token")
end
init_config() click to toggle source
# 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) click to toggle source
# File lib/ayadn/settings.rb, line 74
def self.init_sqlite(sqlaccounts)
  Databases.active_account(Amalgalite::Database.new(sqlaccounts))
end
load_config() click to toggle source
# 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_file() click to toggle source
# File lib/ayadn/settings.rb, line 105
def self.read_token_file
  File.read(@config.paths.auth + "/token")
end
restore_defaults() click to toggle source
# 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_config() click to toggle source
# File lib/ayadn/settings.rb, line 97
def self.save_config
  File.write(@config.paths.config + "/config.yml", @options.to_h.to_yaml)
end

Private Class Methods

defaults() click to toggle source
# File lib/ayadn/settings.rb, line 176
def self.defaults
  {
    timeline: {
      directed: true,
      source: true,
      symbols: true,
      name: true,
      date: true,
      debug: false,
      compact: false
    },
    marker: {
      messages: true
    },
    counts: {
      default: 50,
      unified: 50,
      global: 50,
      checkins: 50,
      conversations: 50,
      photos: 50,
      trending: 50,
      mentions: 50,
      convo: 50,
      posts: 50,
      messages: 20,
      search: 200,
      whoreposted: 20,
      whostarred: 20,
      whatstarred: 100,
      files: 50
    },
    formats: {
      table: {
        width: 75,
        borders: true
      },
      list: {
        reverse: true
      }
    },
    channels: {
      links: true
    },
    colors: {
      id: :blue,
      index: :red,
      username: :green,
      name: :magenta,
      date: :cyan,
      link: :yellow,
      dots: :blue,
      hashtags: :cyan,
      mentions: :red,
      source: :cyan,
      symbols: :green,
      unread: :cyan,
      debug: :red,
      excerpt: :green
    },
    backup: {
      posts: false,
      messages: false,
      lists: false
    },
    scroll: {
      spinner: true,
      timer: 3,
      date: false
    },
    nicerank: {
      threshold: 2.1,
      filter: true,
      unranked: false
    },
    nowplaying: {},
    blacklist: {
      active: true
    }
  }
end
has_version_file?() click to toggle source
# File lib/ayadn/settings.rb, line 164
def self.has_version_file?
  File.exist?(@config.paths.config + "/version.yml")
end
new_api_file(api_file) click to toggle source
# File lib/ayadn/settings.rb, line 151
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) click to toggle source
# File lib/ayadn/settings.rb, line 158
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_version_file() click to toggle source
# File lib/ayadn/settings.rb, line 168
def self.read_version_file
  YAML.load(File.read(@config.paths.config + "/version.yml"))
end
write_config_file(config_file, options) click to toggle source
# File lib/ayadn/settings.rb, line 172
def self.write_config_file(config_file, options)
  File.write(config_file, options.to_yaml)
end