Class: Travis::Client::AutoLogin

Inherits:
Object
  • Object
show all
Defined in:
lib/travis/client/auto_login.rb

Constant Summary collapse

NoTokenError =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, options = {}) ⇒ AutoLogin

Returns a new instance of AutoLogin.



11
12
13
14
15
16
17
# File 'lib/travis/client/auto_login.rb', line 11

def initialize(session, options = {})
  @session     = session.session
  config_path  = ENV.fetch('TRAVIS_CONFIG_PATH') { File.expand_path('.travis', Dir.home) }
  @config_file = options.fetch(:config_file) { File.expand_path('config.yml', config_path) }
  @auto_token  = options.fetch(:auto_token) { true }
  @raise       = options.fetch(:raise) { true }
end

Instance Attribute Details

#sessionObject (readonly)

Returns the value of attribute session.



9
10
11
# File 'lib/travis/client/auto_login.rb', line 9

def session
  @session
end

Instance Method Details

#authenticateObject



19
20
21
22
# File 'lib/travis/client/auto_login.rb', line 19

def authenticate
  return unless session.access_token = cli_token
  github.with_token { |t| session.github_auth(t) }
end

#cli_configObject



40
41
42
# File 'lib/travis/client/auto_login.rb', line 40

def cli_config
  @cli_config ||= YAML.load_file(@config_file) if File.exist? @config_file
end

#cli_tokenObject



33
34
35
36
37
38
# File 'lib/travis/client/auto_login.rb', line 33

def cli_token
  result   = cli_config
  result &&= result['endpoints']
  result &&= result[session.uri]
  result &&  result['access_token']
end

#githubObject



24
25
26
27
28
29
30
31
# File 'lib/travis/client/auto_login.rb', line 24

def github
  @github         ||= Tools::Github.new(session.config['github']) do |g|
    g.explode       = true
    g.  = false
    g.auto_token    = @auto_token
    g.after_tokens  = proc { raise NoTokenError, "no suitable github token found" } if @raise
  end
end