Module: Lingohub::Command
- Extended by:
- Helpers
- Defined in:
- lib/lingohub/command.rb,
lib/lingohub/commands/auth.rb,
lib/lingohub/commands/base.rb,
lib/lingohub/commands/help.rb,
lib/lingohub/commands/project.rb,
lib/lingohub/commands/version.rb,
lib/lingohub/commands/resource.rb
Defined Under Namespace
Classes: Auth, Base, BaseWithApp, CommandFailed, Help, InvalidCommand, Project, Resource, Version
Class Method Summary
collapse
Methods included from Helpers
ask, confirm, confirm_command, deprecate, display, error, format_date, git, has_git?, home_directory, redisplay, retry_on_exception, run_command, running_on_a_mac?, running_on_windows?, shell
Class Method Details
73
74
75
76
|
# File 'lib/lingohub/command.rb', line 73
def (body)
msg = parse_error_xml(body) || parse_error_json(body) || parse_error_plain(body) || 'Internal server error ' + body
msg.split("\n").map { |line| ' ! ' + line }.join("\n")
end
|
69
70
71
|
# File 'lib/lingohub/command.rb', line 69
def (body)
body =~ /^[\w\s]+ not found$/ ? body : "Resource not found"
end
|
.parse(command) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/lingohub/command.rb', line 48
def parse(command)
parts = command.split(':')
case parts.size
when 1
begin
return eval("Lingohub::Command::#{command.capitalize}"), :index
rescue NameError, NoMethodError
return Lingohub::Command::Project, command.to_sym
end
else
begin
const = Lingohub::Command
command = parts.pop
parts.each { |part| const = const.const_get(part.capitalize) }
return const, command.to_sym
rescue NameError
raise InvalidCommand
end
end
end
|
.parse_error_json(body) ⇒ Object
85
86
87
88
89
|
# File 'lib/lingohub/command.rb', line 85
def parse_error_json(body)
json = OkJson.decode(body.to_s)
json['error'] || json.to_s
rescue OkJson::ParserError
end
|
.parse_error_plain(body) ⇒ Object
91
92
93
94
|
# File 'lib/lingohub/command.rb', line 91
def parse_error_plain(body)
return unless body.respond_to?(:headers) && body.[:content_type].include?("text/plain")
body.to_s
end
|
.parse_error_xml(body) ⇒ Object
78
79
80
81
82
83
|
# File 'lib/lingohub/command.rb', line 78
def parse_error_xml(body)
xml_errors = REXML::Document.new(body).elements.to_a("//errors/error")
msg = xml_errors.map { |a| a.text }.join(" / ")
return msg unless msg.empty?
rescue Exception
end
|
.run(command, args, retries = 0) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/lingohub/command.rb', line 15
def run(command, args, retries=0)
begin
run_internal 'auth:reauthorize', args.dup if retries > 0
run_internal(command, args.dup)
rescue InvalidCommand
error "Unknown command. Run 'lingohub help' for usage information."
rescue RestClient::RequestFailed => e
error (e.http_body) unless e.http_code == 402
rescue RestClient::RequestTimeout
error "API request timed out. Please try again, or contact [email protected] if this issue persists."
rescue CommandFailed => e
error e.message
rescue Interrupt => e
error "\n[canceled]"
end
end
|
.run_internal(command, args, lingohub = nil) ⇒ Object
41
42
43
44
45
46
|
# File 'lib/lingohub/command.rb', line 41
def run_internal(command, args, lingohub=nil)
klass, method = parse(command)
runner = klass.new(args, lingohub)
raise InvalidCommand unless runner.respond_to?(method)
runner.send(method)
end
|