Sha256: f5afca8672021131f674902063f0bcb4b40b2fd365ee2c931bdd9e4434a88d77

Contents?: true

Size: 868 Bytes

Versions: 12

Compression:

Stored size: 868 Bytes

Contents

require 'yaml'

##
# Define Config spec
module Octoauth
  ##
  # Define default file path
  DEFAULT_FILE = '~/.octoauth.yml'

  ##
  # Configuration object
  class ConfigFile
    attr_reader :file
    attr_accessor :token

    ##
    # Create new Config object, either ephemerally or from a file
    def initialize(params = {})
      @file = params[:file] == :default ? DEFAULT_FILE : params[:file]
      @file = File.expand_path(@file) if @file
      @note = params[:note] || fail(ArgumentError, 'A note must be provided')
      @token = parse
    end

    def write
      new = get
      new[@note] = @token
      File.open(@file, 'w', 0600) { |fh| fh.write new.to_yaml }
    end

    private

    def get
      return {} unless @file && File.exist?(@file)
      YAML.load File.read(File.expand_path(@file))
    end

    def parse
      get[@note]
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
octoauth-1.4.4 lib/octoauth/configfile.rb
octoauth-1.4.0 lib/octoauth/configfile.rb
octoauth-1.3.0 lib/octoauth/configfile.rb
octoauth-1.2.0 lib/octoauth/configfile.rb
octoauth-1.1.0 lib/octoauth/configfile.rb
octoauth-1.0.2 lib/octoauth/configfile.rb
octoauth-1.0.1 lib/octoauth/configfile.rb
octoauth-1.0.0 lib/octoauth/configfile.rb
octoauth-0.0.10 lib/octoauth/configfile.rb
octoauth-0.0.9 lib/octoauth/configfile.rb
octoauth-0.0.8 lib/octoauth/configfile.rb
octoauth-0.0.7 lib/octoauth/configfile.rb