Class: Shard::Credentials

Inherits:
Struct
  • Object
show all
Defined in:
lib/shard/credentials.rb

Constant Summary collapse

GITHUB_API_ENTRY =

# Declarations #

#
"api.github.com"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#passwordObject

Returns the value of attribute password

Returns:

  • (Object)

    the current value of password



6
7
8
# File 'lib/shard/credentials.rb', line 6

def password
  @password
end

#usernameObject

Returns the value of attribute username

Returns:

  • (Object)

    the current value of username



6
7
8
# File 'lib/shard/credentials.rb', line 6

def username
  @username
end

Class Method Details

.currentObject

# Class Methods #

#


22
23
24
25
# File 'lib/shard/credentials.rb', line 22

def self.current
  username, password = Netrc.read[GITHUB_API_ENTRY]
  new(username, password)
end

.saved?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/shard/credentials.rb', line 27

def self.saved?
  current.present?
end

.valid?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/shard/credentials.rb', line 31

def self.valid?
  Octokit.validate_credentials netrc: true
end

Instance Method Details

#netrc_fileObject



51
52
53
# File 'lib/shard/credentials.rb', line 51

def netrc_file
  @netrc_file ||= Netrc.read
end

#present?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/shard/credentials.rb', line 55

def present?
  !(username.to_s == '' || password.to_s == '')
end

#saveObject

# Instance Methods #

#


41
42
43
44
45
46
47
48
49
# File 'lib/shard/credentials.rb', line 41

def save
  if present?
    netrc_file[GITHUB_API_ENTRY] = username, password
  else
    netrc_file.delete GITHUB_API_ENTRY
  end

  netrc_file.save
end