Class: TimelyApp::CLI
- Inherits:
-
Object
- Object
- TimelyApp::CLI
- Defined in:
- lib/timely-app/cli.rb,
sig/timely-app/cli.rbs
Instance Attribute Summary collapse
-
#client ⇒ TimelyApp::Client
readonly
Returns the value of attribute client.
-
#options ⇒ Hash[Symbol, untyped]
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #auth(client_id, client_secret) ⇒ void
- #call(cmd, *args) ⇒ Object
- #check_access_token ⇒ void
- #command_exists?(cmd) ⇒ Boolean
- #config_file_path ⇒ String
- #fetch_access_token ⇒ Object?
- #fetch_account_id ⇒ Object?
- #get_config(key) ⇒ Object?
-
#initialize(options = {}) ⇒ CLI
constructor
A new instance of CLI.
- #read_config_file ⇒ Hash[String, untyped]?
- #save_config_file(**options) ⇒ void
- #set_config(key, value) ⇒ void
Constructor Details
#initialize(options = {}) ⇒ CLI
Returns a new instance of CLI.
5 6 7 8 9 10 11 12 |
# File 'lib/timely-app/cli.rb', line 5 def initialize( = {}) @options = @client = TimelyApp::Client.new( access_token: [:access_token] || fetch_access_token, account_id: [:account_id] || fetch_account_id, verbose: [:verbose] ) end |
Instance Attribute Details
#client ⇒ TimelyApp::Client (readonly)
Returns the value of attribute client.
3 4 5 |
# File 'lib/timely-app/cli.rb', line 3 def client @client end |
#options ⇒ Hash[Symbol, untyped] (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/timely-app/cli.rb', line 3 def @options end |
Instance Method Details
#auth(client_id, client_secret) ⇒ void
This method returns an undefined value.
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: [:verbose]) code = (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
28 29 30 |
# File 'lib/timely-app/cli.rb', line 28 def call(cmd, *args) client.send(cmd, *args) end |
#check_access_token ⇒ void
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
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_path ⇒ String
100 101 102 |
# File 'lib/timely-app/cli.rb', line 100 def config_file_path Dir.home + "/.timelyrc" end |
#fetch_access_token ⇒ Object?
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_id ⇒ Object?
110 111 112 |
# File 'lib/timely-app/cli.rb', line 110 def fetch_account_id read_config_file&.fetch("account_id", nil) end |
#get_config(key) ⇒ Object?
14 15 16 |
# File 'lib/timely-app/cli.rb', line 14 def get_config(key) read_config_file&.fetch(key, nil) end |
#read_config_file ⇒ Hash[String, untyped]?
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.
125 126 127 128 129 130 |
# File 'lib/timely-app/cli.rb', line 125 def save_config_file(**) config = read_config_file || {} config.merge!() 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.
18 19 20 |
# File 'lib/timely-app/cli.rb', line 18 def set_config(key, value) save_config_file(key => value) end |