Class: Travis::Tools::TokenFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/travis/tools/token_finder.rb

Overview

This is used when running `travis login –auto`

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TokenFinder

Returns a new instance of TokenFinder.


14
15
16
17
18
19
# File 'lib/travis/tools/token_finder.rb', line 14

def initialize(options = {})
  self.netrc   = options[:netrc]  || Netrc.default_path
  self.hub     = options[:hub]    || ENV['HUB_CONFIG'] || '~/.config/hub'
  self.github  = options[:github] || 'github.com'
  self.explode = options[:explode]
end

Instance Attribute Details

#explodeObject

Returns the value of attribute explode


8
9
10
# File 'lib/travis/tools/token_finder.rb', line 8

def explode
  @explode
end

#githubObject

Returns the value of attribute github


8
9
10
# File 'lib/travis/tools/token_finder.rb', line 8

def github
  @github
end

#hubObject

Returns the value of attribute hub


8
9
10
# File 'lib/travis/tools/token_finder.rb', line 8

def hub
  @hub
end

#netrcObject

Returns the value of attribute netrc


8
9
10
# File 'lib/travis/tools/token_finder.rb', line 8

def netrc
  @netrc
end

Class Method Details

.find(options = {}) ⇒ Object


10
11
12
# File 'lib/travis/tools/token_finder.rb', line 10

def self.find(options = {})
  new(options).find
end

Instance Method Details

#findObject


29
30
31
# File 'lib/travis/tools/token_finder.rb', line 29

def find
  find_netrc || find_hub
end

#find_hubObject


41
42
43
44
45
46
47
48
# File 'lib/travis/tools/token_finder.rb', line 41

def find_hub
  return unless File.readable? hub
  data   = YAML.load_file(File.expand_path(hub))
  data &&= Array(data[github])
  data.first['oauth_token'] if data.size == 1
rescue => e
  raise e if explode
end

#find_netrcObject


33
34
35
36
37
38
39
# File 'lib/travis/tools/token_finder.rb', line 33

def find_netrc
  return unless File.readable? netrc
  data = Netrc.read(netrc)[github]
  data.detect { |e| e.size == 40 } if data
rescue => e
  raise e if explode
end