Sha256: f255bfb707a868455f02652711460cb52816bc9213072b2f46d52a7d1c382d24

Contents?: true

Size: 1.34 KB

Versions: 9

Compression:

Stored size: 1.34 KB

Contents

require 'fileutils'
require 'yaml'

module DriveEnv
  class Config
    DEFAULT_CONFIG_FILE = File.expand_path('~/.config/drive_env/config')
    DEFAULT_TOKENS_STORE_FILE = File.expand_path('~/.config/drive_env/tokens.yml')
    DEFAULT_TOKEN_USER_ID = 'default'

    attr_accessor :client_id
    attr_accessor :client_secret

    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

    def migrate
      ## dropped variables in 0.2.pre1
      %W[access_token refresh_token expires_at].each do |v|
        if instance_variable_get("@#{v}")
          remove_instance_variable("@#{v}")
        end
      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.migrate
        obj
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
drive_env-0.4.2 lib/drive_env/config.rb
drive_env-0.4.1 lib/drive_env/config.rb
drive_env-0.4.0 lib/drive_env/config.rb
drive_env-0.3.1 lib/drive_env/config.rb
drive_env-0.3.0 lib/drive_env/config.rb
drive_env-0.2.2 lib/drive_env/config.rb
drive_env-0.2.1 lib/drive_env/config.rb
drive_env-0.2.0 lib/drive_env/config.rb
drive_env-0.2.pre1 lib/drive_env/config.rb