Class: Termworld::Commands::UserAction

Inherits:
Object
  • Object
show all
Defined in:
lib/termworld/commands/user_action.rb

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ UserAction

Returns a new instance of UserAction.



6
7
8
# File 'lib/termworld/commands/user_action.rb', line 6

def initialize(name)
  @name = name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, _) ⇒ Object



53
54
55
# File 'lib/termworld/commands/user_action.rb', line 53

def method_missing(method, _)
  puts Utils::Color.reden "#{method} command not found"
end

Instance Method Details

#move(options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/termworld/commands/user_action.rb', line 39

def move(options)
  if options.size != 1 || !%w(up down left right).include?(options.first)
    puts Utils::Color.reden 'Direction must be only up, down, left or right'
    puts "ex) $ termworld user:#{@name} move up"
    return
  end
  direction = options.first.to_sym
  user = Models::User.new(name: @name)
  if !user.bind_local_by_name
    return puts Utils::Color.reden "User:#{@name} is not awake or doesn't exists"
  end
  user.move(direction)
end

#sleep(options) ⇒ Object



24
25
26
27
28
# File 'lib/termworld/commands/user_action.rb', line 24

def sleep(options)
  user = Models::User.new(name: @name)
  user.delete_local(by: :name)
  puts Utils::Color.greenen "User:#{user.name} slept!"
end

#terminal(options) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/termworld/commands/user_action.rb', line 30

def terminal(options)
  user = Models::User.new(name: @name)
  if !user.bind_local_by_name
    return puts Utils::Color.reden "User:#{@name} is not awake or doesn't exists"
  end
  terminal = Terminal::Controller.new(user)
  terminal.run
end

#wakeup(options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/termworld/commands/user_action.rb', line 10

def wakeup(options)
  res = $api_client.call_auth(:get, "/users/#{@name}")
  puts Utils::Color.reden "Login required" if res.code == 401
  puts Utils::Color.reden "User:#{@name} doesn't exsit" if res.code == 404
  user = Models::User.new(JSON.parse(res.body, {symbolize_names: true})[:user])
  user.initialize_position
  user.save_local
  if user.updated
    puts Utils::Color.reden "User:#{user.name} already awake"
  elsif user.created
    puts Utils::Color.greenen "User:#{user.name} woke up!"
  end
end