class Turbotlib

Public Class Methods

data_dir() click to toggle source

Returns the path to the data directory.

@return [String] the path to the data directory

# File lib/turbotlib.rb, line 26
def data_dir
  path_to('data')
end
get_var(key) click to toggle source

Returns the value of a variable.

@param [String] a variable name @return the value of the variable

# File lib/turbotlib.rb, line 57
def get_var(key)
  get_vars[key]
end
in_production?() click to toggle source

Returns whether the environment is production.

@return [Boolean] whether the environment is production

# File lib/turbotlib.rb, line 72
def in_production?
  !!ENV['MORPH_URL']
end
log(message) click to toggle source

Logs a message to STDERR.

@param [String] message a log message

# File lib/turbotlib.rb, line 19
def log(message)
  $stderr.puts message
end
save_var(key, val) click to toggle source

Saves and returns the value of a variable.

@param [String] a variable name @param val the value of the variable @return the value of the variable

# File lib/turbotlib.rb, line 46
def save_var(key, val)
  vars = get_vars
  vars[key] = val
  save_vars(vars)
  val
end
sources_dir() click to toggle source

Returns the path to the sources directory.

@return [String] the path to the sources directory

# File lib/turbotlib.rb, line 33
def sources_dir
  if in_production? && !is_admin?
    raise 'Only admins are permitted to write to `sources_dir`'
  else
    path_to('sources')
  end
end
sqlite_magic_connection() click to toggle source

Override default in ScraperWiki gem.

@return [SqliteMagic::Connection] a SQLite connection

# File lib/turbotlib.rb, line 64
def sqlite_magic_connection
  db = "#{data_dir}/data.sqlite"
  @sqlite_magic_connection ||= SqliteMagic::Connection.new(db)
end

Private Class Methods

get_vars() click to toggle source
# File lib/turbotlib.rb, line 82
def get_vars
  begin
    YAML.load_file(vars_path)
  rescue Errno::ENOENT
    {}
  end
end
is_admin?() click to toggle source
# File lib/turbotlib.rb, line 78
def is_admin?
  ENV['USER_ROLES'].to_s.split(',').include?('admin')
end
path_to(dir) click to toggle source
# File lib/turbotlib.rb, line 99
def path_to(dir)
  if in_production?
    "/#{dir}"
  else
    begin
      Dir.mkdir(dir)
    rescue Errno::EEXIST
    end

    dir
  end
end
save_vars(vars) click to toggle source
# File lib/turbotlib.rb, line 90
def save_vars(vars)
  yaml = YAML.dump(vars)
  File.write(vars_path, yaml)
end
vars_path() click to toggle source
# File lib/turbotlib.rb, line 95
def vars_path
  "#{data_dir}/_vars.yml"
end