Class: TimelyApp::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/timely-app/cli.rb,
sig/timely-app/cli.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CLI

Returns a new instance of CLI.

Parameters:

  • options (Hash[Symbol, untyped]) (defaults to: {})


5
6
7
8
9
10
11
12
# File 'lib/timely-app/cli.rb', line 5

def initialize(options = {})
  @options = options
  @client = TimelyApp::Client.new(
    access_token: options[:access_token] || fetch_access_token,
    account_id: options[:account_id] || ,
    verbose: options[:verbose]
  )
end

Instance Attribute Details

#clientTimelyApp::Client (readonly)

Returns the value of attribute client.

Returns:



3
4
5
# File 'lib/timely-app/cli.rb', line 3

def client
  @client
end

#optionsHash[Symbol, untyped] (readonly)

Returns the value of attribute options.

Returns:

  • (Hash[Symbol, untyped])


3
4
5
# File 'lib/timely-app/cli.rb', line 3

def options
  @options
end

Instance Method Details

#auth(client_id, client_secret) ⇒ void

This method returns an undefined value.

Parameters:

  • client_id (String, nil)
  • client_secret (String, nil)


32
33
34
35
36
37
38
39
40
41
# File 'lib/timely-app/cli.rb', line 32

def auth(client_id, client_secret)
  validate_auth_credentials!(client_id, client_secret)

  auth_client = TimelyApp::Client.new(verbose: options[:verbose])
  code = authorize_via_browser(auth_client, client_id)
  token = fetch_oauth_token(auth_client, client_id, client_secret, code)
  persist_or_print_token(token)
rescue TimelyApp::Error => error
  handle_auth_failure(error, code)
end

#call(cmd, *args) ⇒ Object

Parameters:

  • cmd (String)
  • args (Object)

Returns:

  • (Object)


28
29
30
# File 'lib/timely-app/cli.rb', line 28

def call(cmd, *args)
  client.send(cmd, *args)
end

#check_access_tokenvoid

This method returns an undefined value.



118
119
120
121
122
123
# File 'lib/timely-app/cli.rb', line 118

def check_access_token
  unless fetch_access_token
    puts "No access token found. Run `timely-app auth` to get one."
    exit 1
  end
end

#command_exists?(cmd) ⇒ Boolean

Parameters:

  • cmd (String, nil)

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/timely-app/cli.rb', line 22

def command_exists?(cmd)
  return false if cmd.nil? || cmd.empty?

  client.respond_to?(cmd)
end

#config_file_pathString

Returns:

  • (String)


100
101
102
# File 'lib/timely-app/cli.rb', line 100

def config_file_path
  Dir.home + "/.timelyrc"
end

#fetch_access_tokenObject?

Returns:

  • (Object, nil)


114
115
116
# File 'lib/timely-app/cli.rb', line 114

def fetch_access_token
  read_config_file&.fetch("access_token", nil)
end

#fetch_account_idObject?

Returns:

  • (Object, nil)


110
111
112
# File 'lib/timely-app/cli.rb', line 110

def 
  read_config_file&.fetch("account_id", nil)
end

#get_config(key) ⇒ Object?

Parameters:

  • key (String)

Returns:

  • (Object, nil)


14
15
16
# File 'lib/timely-app/cli.rb', line 14

def get_config(key)
  read_config_file&.fetch(key, nil)
end

#read_config_fileHash[String, untyped]?

Returns:

  • (Hash[String, untyped], nil)


104
105
106
107
108
# File 'lib/timely-app/cli.rb', line 104

def read_config_file
  if File.exist?(config_file_path)
    YAML.load_file(config_file_path)
  end
end

#save_config_file(**options) ⇒ void

This method returns an undefined value.

Parameters:

  • options (Hash[Symbol, untyped])


125
126
127
128
129
130
# File 'lib/timely-app/cli.rb', line 125

def save_config_file(**options)
  config = read_config_file || {}
  config.merge!(options)
  File.write(config_file_path, config.to_yaml)
  puts "Saved to #{config_file_path}"
end

#set_config(key, value) ⇒ void

This method returns an undefined value.

Parameters:

  • key (String)
  • value (Object)


18
19
20
# File 'lib/timely-app/cli.rb', line 18

def set_config(key, value)
  save_config_file(key => value)
end