Sha256: 7e2c0015d6925c1fcc547487f8b9a22e9af486b3069e3bfb6fdc9313771546bc

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require 'fileutils'
require 'yaml'

module DriveEnv
  class Config
    DEFAULT_CONFIG_FILE = File.expand_path('~/.config/drive_env/config')

    attr_accessor :client_id
    attr_accessor :client_secret
    attr_accessor :access_token
    attr_accessor :refresh_token
    attr_accessor :expires_at

    def initialize
      @spreadsheet_aliases = {}
      @config_file = DEFAULT_CONFIG_FILE
    end

    def set_alias_for_spreadsheet(name, url)
      @spreadsheet_aliases[name] = url
    end

    def unset_alias_for_spreadsheet(name)
      @spreadsheet_aliases.delete(name)
    end

    def lookup_spreadsheet_url_by_alias(name)
      @spreadsheet_aliases[name]
    end

    def save
      dir = File.dirname(@config_file)
      if !File.directory?(dir)
        FileUtils.mkdir_p(dir)
      end

      File.open(@config_file, 'w') do |fh|
        fh << YAML.dump(self)
      end
    end

    class << self
      def load(file)
        obj = File.exist?(file) ? YAML.load(File.read(file)) : self.new
        obj.instance_variable_set("@config_file", file)
        obj
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
drive_env-0.1.0 lib/drive_env/config.rb