Top Level Namespace

Defined Under Namespace

Modules: Waylon

Instance Method Summary collapse

Instance Method Details

#adminuserObject

Handle demo chat REPL



49
50
51
52
53
# File 'lib/waylon/rspec/test_server.rb', line 49

def adminuser
  @adminuser ||= Waylon::RSpec::TestUser.find_or_create(name: "Charles Montgomery Burns", handle: "monty")
  Waylon::Group.new("admins").add(@adminuser)
  @adminuser
end

#chatroomObject



55
56
57
# File 'lib/waylon/rspec/test_server.rb', line 55

def chatroom
  @chatroom ||= Waylon::RSpec::TestChannel.new(0)
end

#handle_exit_inputObject



59
60
61
62
63
64
65
66
67
# File 'lib/waylon/rspec/test_server.rb', line 59

def handle_exit_input
  if @admin_enabled
    puts "Switching back to a normal user."
    @admin_enabled = false
  else
    puts "Talk to you later!"
    exit
  end
end

#handle_input(body, from: this_user, privately: true) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/waylon/rspec/test_server.rb', line 97

def handle_input(body, from: this_user, privately: true)
  if %w[bye exit leave quit].include?(body)
    handle_exit_input
  elsif ["su", "su -", "su admin", "su - admin"].include?(body)
    puts 'Admin enabled! Use "exit" to go back to a normal user.'
    @admin_enabled = true
  elsif @admin_enabled && %w[irb pry].include?(body)
    pry # rubocop:disable Link/Debugger
  else
    message_count = Waylon::RSpec::TestSense.sent_messages.size

    Waylon::RSpec::TestSense.perform(msg_details(body, from, privately))
    Waylon::RSpec::TestWorker.handle(Waylon::RSpec::TestSense.fake_queue)
    result = Waylon::RSpec::TestSense.sent_messages[message_count..].join("\n")
    puts("(@#{Waylon::RSpec::TestUser.whoami.handle}) >> #{result}")
  end
end

#msg_details(body, from, privately) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/waylon/rspec/test_server.rb', line 69

def msg_details(body, from, privately)
  details = {
    user_id: from.id,
    text: body,
    created_at: Time.now
  }
  if privately
    details[:type] = :private
    details[:receiver_id] = robot.id
  else
    details[:type] = :channel
    details[:channel_id] = channel ? channel.id : chatroom.id
  end
  details
end

#robotObject



85
86
87
# File 'lib/waylon/rspec/test_server.rb', line 85

def robot
  @robot ||= Waylon::RSpec::TestUser.new(0)
end

#testuserObject



89
90
91
# File 'lib/waylon/rspec/test_server.rb', line 89

def testuser
  @testuser ||= Waylon::RSpec::TestUser.new(1)
end

#this_userObject



93
94
95
# File 'lib/waylon/rspec/test_server.rb', line 93

def this_user
  @admin_enabled ? adminuser : testuser
end