Class: MTCLI::CLI
- Inherits:
-
Thor
show all
- Defined in:
- lib/mtcli/cli.rb
Overview
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *_args) ⇒ Object
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/mtcli/cli.rb', line 141
def method_missing(method, *_args)
config = Config.current
unless config
puts 'No current setting.'
return
end
opts = {
base_url: config.base_url,
client_id: 'mtcli'
}
opts[:access_token] = config.access_token if config.access_token
client = MT::DataAPI::Client.new(opts)
begin
res = client.call(method, options)
puts res.to_json
if opts['access_token'] && res['error'] && res['error']['code'] == 401
config.access_token = nil
config.save
puts 'Removed invalid access token.'
end
rescue
puts $ERROR_INFO
end
end
|
Instance Method Details
#add(name, base_url) ⇒ Object
52
53
54
55
56
|
# File 'lib/mtcli/cli.rb', line 52
def add(name, base_url)
puts "Cannot add #{name}." unless Config.create(name, base_url: base_url)
puts "Added #{name}."
end
|
#current(name = nil) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/mtcli/cli.rb', line 36
def current(name = nil)
if name.nil?
unless Config.current
puts 'Current setting does not exist.'
return
end
puts Config.current
else
Config.current = name
puts "Current setting is #{name}."
end
end
|
#delete(name) ⇒ Object
64
65
66
67
68
69
70
71
|
# File 'lib/mtcli/cli.rb', line 64
def delete(name)
unless Config.delete(name)
puts "Cannot delete #{name}"
return
end
puts "Deleted #{name}"
end
|
#list ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/mtcli/cli.rb', line 12
def list
configs = Config.all
if configs.empty?
puts 'No MT settings are registered.'
return
end
configs.each do |config|
puts config
end
end
|
#login(username, password) ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/mtcli/cli.rb', line 92
def login(username, password)
config = Config.current
unless config
puts 'No current setting.'
return
end
client = MT::DataAPI::Client.new(base_url: config.base_url,
client_id: 'mtcli')
client.call('authenticate', username: username,
password: password)
unless client.access_token
puts 'Login failed.'
return
end
config.access_token = client.access_token
config.save
puts 'Login succeeded.'
end
|
#logout ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/mtcli/cli.rb', line 115
def logout
config = Config.current
unless config
puts 'No current setting.'
return
end
unless config.logged_in?
puts 'Not logged in.'
return
end
client = MT::DataAPI::Client.new(base_url: config.base_url,
client_id: 'mtcli',
access_token: config.access_token)
res = client.call('revoke_authentication')
return unless res['status'] == 'success'
config.access_token = nil
config.save
puts 'Logged out.'
end
|
#rename(name, new_name) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/mtcli/cli.rb', line 74
def rename(name, new_name)
unless Config.get(name)
puts "#{name} is not registered."
return
end
if Config.get(new_name)
puts "#{new_name} exists."
return
end
unless Config.rename(name, new_name)
puts 'Renaming failed.'
return
end
puts "Renamed #{name} to #{new_name}."
end
|
#respond_to_missing?(method) ⇒ Boolean
137
138
139
|
# File 'lib/mtcli/cli.rb', line 137
def respond_to_missing?(method)
super
end
|
#show(name) ⇒ Object
25
26
27
28
29
30
31
32
33
|
# File 'lib/mtcli/cli.rb', line 25
def show(name)
config = Config.get(name)
unless config
puts "#{name} is not registered."
return
end
puts config
end
|
#update(_name) ⇒ Object
59
60
61
|
# File 'lib/mtcli/cli.rb', line 59
def update(_name)
puts 'update'
end
|