Class: Envirobly::AccessToken

Inherits:
Object
  • Object
show all
Includes:
Colorize
Defined in:
lib/envirobly/access_token.rb

Constant Summary collapse

APP_NAME =
"envirobly"

Constants included from Colorize

Colorize::BLUE, Colorize::BOLD, Colorize::FAINT, Colorize::GREEN, Colorize::RED, Colorize::RESET, Colorize::YELLOW

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Colorize

#bold, #cross, #display_config_errors, #downwards_arrow_to_right, #faint, #green, #green_check, #red, #yellow

Constructor Details

#initialize(token = ENV["ENVIROBLY_ACCESS_TOKEN"].presence, shell: nil) ⇒ AccessToken

Returns a new instance of AccessToken.



42
43
44
45
46
47
48
49
50
# File 'lib/envirobly/access_token.rb', line 42

def initialize(token = ENV["ENVIROBLY_ACCESS_TOKEN"].presence, shell: nil)
  @shell = shell

  if token.blank? && File.exist?(self.class.path)
    @token = File.read(self.class.path)
  else
    @token = token
  end
end

Instance Attribute Details

#shellObject (readonly)

Returns the value of attribute shell.



12
13
14
# File 'lib/envirobly/access_token.rb', line 12

def shell
  @shell
end

Class Method Details

.destroyObject



15
16
17
18
19
# File 'lib/envirobly/access_token.rb', line 15

def destroy
  if File.exist?(path)
    FileUtils.rm path
  end
end

.pathObject



37
38
39
# File 'lib/envirobly/access_token.rb', line 37

def path
  Pathname.new(storage_dir).join "access_token"
end

.storage_base_dirObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/envirobly/access_token.rb', line 21

def storage_base_dir
  if dir = ENV["ENVIROBLY_CLI_DATA_HOME"].presence
    return dir
  end

  if Gem.win_platform?
    ENV["APPDATA"] || File.join(Dir.home, "AppData", "Roaming")
  else
    ENV["XDG_DATA_HOME"] || File.join(Dir.home, ".local", "share")
  end
end

.storage_dirObject



33
34
35
# File 'lib/envirobly/access_token.rb', line 33

def storage_dir
  File.join(storage_base_dir, APP_NAME)
end

Instance Method Details

#as_http_bearerObject



58
59
60
# File 'lib/envirobly/access_token.rb', line 58

def as_http_bearer
  "Bearer #{@token}"
end

#require!Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/envirobly/access_token.rb', line 62

def require!
  return if @token.present?

  shell.say "This action requires you to be signed in."
  shell.say "Please visit https://on.envirobly.com/profile/access_tokens"
  shell.say "to generate an access token and then paste it in here."
  shell.say

  set
end

#saveObject



52
53
54
55
56
# File 'lib/envirobly/access_token.rb', line 52

def save
  FileUtils.mkdir_p self.class.storage_dir
  File.write self.class.path, @token
  File.chmod 0600, self.class.path
end

#setObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/envirobly/access_token.rb', line 73

def set
  @token = nil

  while @token.blank?
    begin
      @token = shell.ask("Access Token:", echo: false)
    rescue Interrupt
      shell.say
      shell.say_error "Cancelled"
      exit
    end

    api = Api.new(access_token: self, exit_on_error: false)

    # TODO: Eventually replace with custom `whoami` API that returns name, email...
    if api.list_accounts.success?
      save
      shell.say
      shell.say "Successfully signed in "
      shell.say green_check
    else
      shell.say
      shell.say_error "This token is invalid. Please try again"
      @token = nil
    end
  end
end