Class: Fgi::Tokens
- Inherits:
-
Object
- Object
- Fgi::Tokens
- Extended by:
- HttpRequests
- Defined in:
- lib/fgi/tokens.rb
Class Method Summary collapse
-
.add_token(token) ⇒ Object
Add a new token association for the user’s fgi configuration.
- .create_user_tokens_file(config:, git_service:, token:) ⇒ Object
- .get_token(config:, git_service:) ⇒ String, NilClass
-
.token_valid?(git_service, token) ⇒ Boolean
True if the token is valid, false otherwise.
Methods included from HttpRequests
Class Method Details
.add_token(token) ⇒ Object
Add a new token association for the user’s fgi configuration
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
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
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.
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 |