Sha256: 516fa86b070ea9789d1a63ead3318d394e05e70c2c3caa83bcefaaf8f53ff609

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

require 'yaml'

# used to read api_key
module Instapusher2
  module Configuration
    extend self
    @_settings = {}
    attr_reader :_settings

    def load(debug = false, filename=nil)
      filename ||= File.join(ENV['HOME'], '.instapusher2')

      unless File.exist? filename
        File.new(filename, File::CREAT|File::TRUNC|File::RDWR, 0644).close
      end

      @_settings = YAML::load_file(filename) || {}

      if debug
        puts @_settings.inspect
      end
    end

    def ask_for_api_key
      puts ""
      puts "Note: Your instapusher API key is available at http://www.instapusher.com/my/api_key"
      puts ""
      puts "Enter your Instapusher API key:"
      api_key = ask
      api_key
    end

    def ask_for_and_write_api_key
      api_key = ask_for_api_key
      instapusher_config = {"api_key" => api_key}
      File.open(File.join(Dir.home, ".instapusher2"), "w") do |file|
        file.write instapusher_config.to_yaml
      end

      puts ""
      puts "You are all set. Start using instapusher2."
    end

    def ask
      $stdin.gets.to_s.strip
    end

    def method_missing(name, *args, &block)
      self.load
      @_settings[name.to_s]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
instapusher2-0.0.4 lib/instapusher2/configuration.rb