Sha256: 01f63ba8a88b19ea7542ceaee2a0fa246b63e2fc76467d3a8ca7047c19e63201

Contents?: true

Size: 1.17 KB

Versions: 4

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'], '.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, ".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

4 entries across 4 versions & 2 rubygems

Version Path
instapusher2-0.0.3 lib/instapusher2/configuration.rb
instapusher2-0.0.2 lib/instapusher2/configuration.rb
instapusher2-0.0.1 lib/instapusher2/configuration.rb
instapusher-0.0.1 lib/instapusher2/configuration.rb