Class: Fgi::Tokens

Inherits:
Object
  • Object
show all
Extended by:
HttpRequests
Defined in:
lib/fgi/tokens.rb

Class Method Summary collapse

Methods included from HttpRequests

get, post

Class Method Details

.add_token(token) ⇒ Object

Add a new token association for the user’s fgi configuration

Parameters:

  • token (String)

    the token to associate to the git service



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fgi/tokens.rb', line 27

def add_token(token)
  git_service = CONFIG[:git_service_class].new
  token = get_token(git_service) if token.nil?
  if token_valid?(git_service, token)
    create_user_tokens_file(config: CONFIG, git_service: CONFIG[:git_service], token: token)
    puts "\nYour #{git_service} token has been successfully added !\n\n"
  else
    puts "\nOops, seems to be an invalid token. Try again.\n\n"
    exit!
  end
end

.create_user_tokens_file(config:, git_service:, token:) ⇒ Object

Parameters:

  • git_service_name (String)

    the git service to associate a token to

  • token (String)

    the token to associate to the git service



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fgi/tokens.rb', line 10

def create_user_tokens_file(config:, git_service:, token:)
  if File.exist?("#{Dir.home}/.tokens.fgi.yml")
    tokens = YAML.load_file("#{Dir.home}/.tokens.fgi.yml")
    tokens[git_service.to_sym] = { config[:url] => token }
  else
    tokens = {
      git_service => {
        config[:url] => token
      }
    }
  end
  # Shouldn't we define some access restrictions on this file ?
  File.open("#{Dir.home}/.tokens.fgi.yml", 'w') { |f| f.write(tokens.to_yaml) }
end

.get_token(config:, git_service:) ⇒ String, NilClass

Parameters:

  • git_service (Class)

    the current project’s git service class

Returns:

  • (String)

    the current token associated to the project’s git service

  • (NilClass)

    if there is no token associated to the project’s git service



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fgi/tokens.rb', line 42

def get_token(config:, git_service:)
  if File.exist?("#{Dir.home}/.tokens.fgi.yml")
    tokens = YAML.load_file("#{Dir.home}/.tokens.fgi.yml")
    tokens[git_service.to_sym][config[:url]]
  else
    puts "\nPlease enter your #{git_service} token :"
    puts '(use `fgi --help` to check how to get your token)'
    puts '-------------------------------------------------'
    begin
      token = STDIN.gets.chomp
      exit! if token == 'quit'
      return token if token_valid?(git_service, token)
      nil
    rescue Interrupt
      exit!
    end
  end
end

.token_valid?(git_service, token) ⇒ Boolean

Returns true if the token is valid, false otherwise.

Parameters:

  • git_service (Class)

    the current project’s git service class

  • token (String)

    the token to check the validity

Returns:

  • (Boolean)

    true if the token is valid, false otherwise



64
65
66
67
68
69
70
# File 'lib/fgi/tokens.rb', line 64

def token_valid?(git_service, token)
  response = get(
    url:     git_service.routes[:projects],
    headers: { git_service.token_header => token }
  )
  response[:status] == '200'
end