Sha256: 291ed04e98246ac694f16cefbea3bee718f04f85a66d33c3e881f5e2b56970b0

Contents?: true

Size: 970 Bytes

Versions: 1

Compression:

Stored size: 970 Bytes

Contents

module Pingfm
  class ConfigNotFound < Exception; end

  class Config
    CONFIG_PATH = (RUBY_PLATFORM =~ /mswin32/ ? ENV['HOMEPATH'] : ENV['HOME'])
    CONFIG_FILE = File.expand_path(File.join(CONFIG_PATH, '.pingfm.yml'))

    class << self

      # Prompts the user for their API key and saves it.
      def ask_for_app_key!
        STDOUT.print 'Enter your Ping.fm User API key (http://ping.fm/key/): '
        STDIN.gets.chomp
      end

      def [](key)
        setup! unless defined?(@@config)
        @@config[key]
      end

      def []=(key, value)
        @@config ||= {}
        @@config[key] = value
        save!
      end

      def save!
        ::File.open(CONFIG_FILE, 'w') do |config_file|
          ::YAML.dump(@@config, config_file)
        end
      end

      def setup!(config_file = CONFIG_FILE)
        raise Pingfm::ConfigNotFound unless File.exists?(config_file)
        @@config = ::YAML.load_file(config_file)
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pingfm-2.1.0 lib/pingfm/config.rb