Sha256: 354eb1cc8bad3fecf8a1e354bd131440620b0ba98c7c0c17286b6827df68a755

Contents?: true

Size: 1.19 KB

Versions: 9

Compression:

Stored size: 1.19 KB

Contents

# frozen-string-literal: true

require 'readline'
require 'httpx'
require 'pastel'
require 'tty-markdown'

using Rodbot::Refinements

module Rodbot

  # Simulate a chat client
  class Simulator

    # @param sender [String] sender to mimick
    # @param raw [Boolean] whether to display raw Markdown
    def initialize(sender, raw: false)
      @sender, @raw = sender, raw
      @relay = Rodbot::Relay.new
      @pastel = Pastel.new
    end

    def run
      puts nil, "Talking to app on #{Rodbot::Services::App.url} as sender #{@pastel.inverse(@sender)}."
      puts 'Type commands beginning with "!" or empty line to exit.', nil
      while (line = Readline.readline("rodbot> ", true)) && !line.empty?
        puts nil, reply_to(line), nil
      end
      puts
    end

    private

    def reply_to(message)
      return "(no command given)" unless message.match?(/^!/)
      command, argument = message[1..].split(/\s+/, 2)
      text_for @relay.send(:command, command, argument).psub(placeholders)
    end

    def placeholders
      {
        sender: @pastel.inverse(@sender)
      }
    end

    def text_for(markdown)
      @raw ? markdown : TTY::Markdown.parse(markdown, mode: 16).strip
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rodbot-0.4.5 lib/rodbot/simulator.rb
rodbot-0.4.4 lib/rodbot/simulator.rb
rodbot-0.4.3 lib/rodbot/simulator.rb
rodbot-0.4.2 lib/rodbot/simulator.rb
rodbot-0.4.1 lib/rodbot/simulator.rb
rodbot-0.4.0 lib/rodbot/simulator.rb
rodbot-0.3.4 lib/rodbot/simulator.rb
rodbot-0.3.3 lib/rodbot/simulator.rb
rodbot-0.3.2 lib/rodbot/simulator.rb