Sha256: a66c1512ac53fb3a93f0f144d0afcce9f2e09585c7cc90b66a74a851cc9384df

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

require 'yaml'

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

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

      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, ".instapusher"), "w") do |file|
        file.write instapusher_config.to_yaml
      end

      puts ""
      puts "You are all set. Start using instapusher."
    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

2 entries across 2 versions & 1 rubygems

Version Path
instapusher-0.0.34 lib/instapusher/configuration.rb
instapusher-0.0.33 lib/instapusher/configuration.rb