Sha256: b2ce6c47a9de2f47af3ea97ef206c99801ee88b1d4b2284a1d0e9ab9433324be

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

require 'fileutils'
require 'json'

module Shelr

  APP_NAME       = 'shelr'
  API_URL        = ENV['SHELR_LOCAL'] ? 'http://localhost:3000' : 'http://shelr.tv'
  XDG_DATA_DIR   = ENV['XDG_DATA_HOME']   || File.join(ENV['HOME'], '.local', 'share')
  XDG_CONFIG_DIR = ENV['XDG_CONFIG_HOME'] || File.join(ENV['HOME'], '.config')
  DATA_DIR       = File.join(XDG_DATA_DIR,   APP_NAME)
  CONFIG_DIR     = File.join(XDG_CONFIG_DIR, APP_NAME)
  API_KEY_CFG    = File.join(CONFIG_DIR, 'api_key')
  BACKEND_CFG    = File.join(CONFIG_DIR, 'backend')

  autoload :Recorder,  'shelr/recorder.rb'
  autoload :Player,    'shelr/player.rb'
  autoload :Publisher, 'shelr/publisher.rb'
  autoload :TTYRec,    'shelr/ttyrec.rb'

  class << self
    def api_key
      return false unless File.exist?(API_KEY_CFG)
      @api_key ||= File.read(API_KEY_CFG).strip
    end

    def api_key=(key)
      ensure_config_dir_exist
      File.open(API_KEY_CFG, 'w+') { |f| f.puts(key.strip) }
    end

    def backend
      @backend ||= File.read(BACKEND_CFG).strip || 'script'
    end

    def backend=(bin)
      unless ['script', 'ttyrec'].include?(bin)
        puts "Backend should be either `script` or `ttyrec`"
        exit 1
      end
      ensure_config_dir_exist
      File.open(BACKEND_CFG, 'w+') { |f| f.puts(bin.strip) }
    end

    def data_dir(record_id)
      id = record_id.strip == 'last' ? last_id : record_id.to_s
      File.join(DATA_DIR, id)
    end

    def last_id
      File.basename(Dir[File.join(DATA_DIR, '*')].sort.last)
    end

    private

    def ensure_config_dir_exist
      FileUtils.mkdir_p(CONFIG_DIR) unless File.exist?(CONFIG_DIR)
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
shelr-0.11.5 lib/shelr.rb
shelr-0.11.4 lib/shelr.rb
shelr-0.11.3 lib/shelr.rb