Sha256: d756bf0d70d0c866b02928565fe35f56aaa5ca7284a9c6f2dcc17efb163539f9

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 KB

Contents

module Pushover
	# The user module, saves any user information provided.
	module User
		# The User class, for a single instance of it.
		# @!attribute name
		# 	@return [String] the name of the user.
		# @!attribute token
		#   @return [String] the token of the user.
		class User
			attr_accessor :name
			attr_accessor :token

			def initialize(name, token)
				@name = name
				@token = token
				Bini::Config[:users] = {} if !Bini::Config[:users]
				Bini::Config[:users][name] = token
			end

		end

		extend self

		# Find the token in the users, or pass on the word to try direct access.
		# @param [String] word the search token, can be an apikey or appname.
		# @return [String] return the apikey (if it can find one) or the word itself.
		def find(word)
			return Bini::Config[:users][word] if Bini::Config[:users] && Bini::Config[:users][word]
			word
		end

		# Add an application to the config file and save it.
		# @param [String] token is the token to be used.
		# @param [String] name is the short name that can be referenced later.
		# @return [Boolean] return the results of the save attempt.
		def add(name, token)
			User.new name, token
			Bini::Config.save
		end

		def remove(name)
			Bini::Config[:users].delete name if Bini::Config[:users]
		end

		# Return the current user selected, or the first one saved.
		def current_user
			# did something get supplied on the cli? try to find it.
			if Bini::Options[:user]
				@current_user = Bini::Options[:user]
			elsif !@current_user
				@current_user = find Bini::Config[:users].values.first if Bini::Config[:users]
			end

			@current_user
		end

		# Set the current_user to whatever you want it to be.
		def current_user=(user)
			@current_user = user
		end

		# Will return true if it can find a user either via the cli or save file.
		def current_user?
			return true if current_user
			return nil
		end
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pushover-1.0.4 lib/pushover/user.rb
pushover-1.0.3 lib/pushover/user.rb
pushover-1.0.2 lib/pushover/user.rb