Sha256: 5c9fa8dd5f09d597b9ebd75257114df59e9ec945ef126203a398920e0a0fb574

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module HabiticaCli
  # Handles basic configuration parsing
  # and interacts with Kefir for config storage
  class Config
    def initialize(cli_options)
      @options = cli_options
      @config = Kefir.config('habitica_cli')
    end

    def user_and_api_key
      config = Kefir.config('habitica_cli')
      habit_user = @options[:habit_user] || ENV['HABIT_USER']
      habit_key = @options[:habit_key] || ENV['HABIT_KEY']

      if blank?(habit_user) || blank?(habit_key)
        habit_user = config.get('habit_user')
        habit_key = config.get('habit_key')
      end

      [habit_user, habit_key]
    end

    def usage
      <<-ERR
**Error**: You must provide a habit user and api key
  Do this via:
  - adding `habit_user` and `habit_key` to #{@config.path}
  - setting HABIT_USER and HABIT_KEY in your shell
  - passing --habit_user --habit_key
ERR
    end

    private

    def blank?(obj)
      # rubocop:disable Style/DoubleNegation
      obj.respond_to?(:empty?) ? !!obj.empty? : !obj
      # rubocop:enable Style/DoubleNegation
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
habitica_cli-1.0.2 lib/habitica_cli/config.rb