Module: Jiveapps::Command

Extended by:
Helpers
Defined in:
lib/jiveapps/command.rb,
lib/jiveapps/commands/app.rb,
lib/jiveapps/commands/auth.rb,
lib/jiveapps/commands/base.rb,
lib/jiveapps/commands/help.rb,
lib/jiveapps/commands/keys.rb,
lib/jiveapps/commands/oauth.rb,
lib/jiveapps/commands/livedev.rb,
lib/jiveapps/commands/sharing.rb

Defined Under Namespace

Classes: App, Auth, Base, BaseWithApp, CommandFailed, Help, InvalidCommand, Keys, Livedev, Oauth, Sharing

Class Method Summary collapse

Methods included from Helpers

ask, catch_args, check_git_version, confirm, confirm_command, debug, debug_mode?, display, display_oauth_services, error, get_app_prop_with_default, get_or_set_git_prop, git_version, has_program?, home_directory, run, running_on_a_mac?, running_on_windows?, sh, usage, user_git_version

Class Method Details

.extract_error(body) ⇒ Object



73
74
75
76
# File 'lib/jiveapps/command.rb', line 73

def extract_error(body)
  msg = parse_error_json(body) || 'Internal server error'
  msg.split("\n").map { |line| ' !   ' + line }.join("\n")
end

.extract_not_found(body) ⇒ Object



69
70
71
# File 'lib/jiveapps/command.rb', line 69

def extract_not_found(body)
  body =~ /^[\w\s]+ not found$/ ? body : "Resource not found"
end

.parse(command) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jiveapps/command.rb', line 49

def parse(command)
  parts = command.split(':')
  case parts.size
    when 1
      begin
        return eval("Jiveapps::Command::#{command.capitalize}"), :index
      rescue NameError, NoMethodError
        return Jiveapps::Command::App, command
      end
    when 2
      begin
        return Jiveapps::Command.const_get(parts[0].capitalize), parts[1]
      rescue NameError
        raise InvalidCommand
      end
    else
      raise InvalidCommand
  end
end

.parse_error_json(body) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jiveapps/command.rb', line 78

def parse_error_json(body)
  json = ActiveSupport::JSON.decode(body.to_s)
  error_hash = json['errors']
  return if error_hash.nil?
  results = ["Errors:"]
  error_hash.each do |key, errors|
    errors.each do |error|
      results << "- #{key} #{error}"
    end
  end
  results.join("\n")
rescue StandardError
end

.run(command, args, retries = 0) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jiveapps/command.rb', line 16

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 'jiveapps help' for usage information."
  rescue RestClient::Unauthorized
    if retries < 3
      STDERR.puts "Authentication failure"
      run(command, args, retries+1)
    else
      error "Authentication failure"
    end
  rescue RestClient::ResourceNotFound => e
    error extract_not_found(e.http_body)
  rescue RestClient::RequestFailed => e
    error extract_error(e.http_body) unless e.http_code == 402
  rescue RestClient::RequestTimeout
    error "API request timed out. Please try again, or contact Jive via the community at https://developers.jivesoftware.com if this issue persists."
  rescue CommandFailed => e
    error e.message
  rescue Interrupt => e
    error "\n[canceled]"
  end
end

.run_internal(command, args) ⇒ Object

Raises:



42
43
44
45
46
47
# File 'lib/jiveapps/command.rb', line 42

def run_internal(command, args)
  klass, method = parse(command)
  runner = klass.new(args)
  raise InvalidCommand unless runner.respond_to?(method)
  runner.send(method)
end