initial commit
This commit is contained in:
41
stbf-common/pom.xml
Normal file
41
stbf-common/pom.xml
Normal file
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<artifactId>stbf-parent</artifactId>
|
||||
<groupId>ru.penkrat.stbf</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>stbf-common</artifactId>
|
||||
<name>stbf-common</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>ru.penkrat.stbf</groupId>
|
||||
<artifactId>stbf-api</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,29 @@
|
||||
package ru.penkrat.stbf.common.command;
|
||||
|
||||
import ru.penkrat.stbf.api.Action;
|
||||
import ru.penkrat.stbf.api.BotRequest;
|
||||
import ru.penkrat.stbf.api.BotResponse;
|
||||
import ru.penkrat.stbf.api.Command;
|
||||
import ru.penkrat.stbf.api.CommandChain;
|
||||
import ru.penkrat.stbf.api.RequestMatcher;
|
||||
import ru.penkrat.stbf.tools.RequestMatchers;
|
||||
|
||||
public abstract class AbstractActionCommand implements Command {
|
||||
|
||||
private RequestMatcher matcher;
|
||||
|
||||
public AbstractActionCommand(Action action) {
|
||||
matcher = RequestMatchers.action(action);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(BotRequest botRequest, BotResponse botResponse, CommandChain chain) {
|
||||
if (matcher.match(botRequest)) {
|
||||
doProcess(botRequest, botResponse);
|
||||
}
|
||||
|
||||
chain.processCommand(botRequest, botResponse);
|
||||
}
|
||||
|
||||
protected abstract void doProcess(BotRequest botRequest, BotResponse botResponse);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package ru.penkrat.stbf.common.command;
|
||||
|
||||
import ru.penkrat.stbf.api.Action;
|
||||
import ru.penkrat.stbf.api.BotRequest;
|
||||
import ru.penkrat.stbf.api.BotResponse;
|
||||
import ru.penkrat.stbf.api.Command;
|
||||
import ru.penkrat.stbf.api.CommandChain;
|
||||
import ru.penkrat.stbf.api.RequestMatcher;
|
||||
import ru.penkrat.stbf.api.Screen;
|
||||
import ru.penkrat.stbf.common.screen.TextScreen;
|
||||
import ru.penkrat.stbf.tools.RequestMatchers;
|
||||
|
||||
public class SimpleCommand implements Command {
|
||||
|
||||
private final RequestMatcher matcher;
|
||||
private final Screen screen;
|
||||
|
||||
public SimpleCommand(String input, String output) {
|
||||
this(Action.simple(input), new TextScreen(output));
|
||||
}
|
||||
|
||||
public SimpleCommand(Action action, Screen screen) {
|
||||
this.screen = screen;
|
||||
matcher = RequestMatchers.action(action);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(BotRequest botRequest, BotResponse botResponse, CommandChain chain) {
|
||||
if (matcher.match(botRequest)) {
|
||||
botResponse.send(screen);
|
||||
}
|
||||
|
||||
chain.processCommand(botRequest, botResponse);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package ru.penkrat.stbf.common.screen;
|
||||
|
||||
import lombok.Getter;
|
||||
import ru.penkrat.stbf.api.Action;
|
||||
import ru.penkrat.stbf.api.Keyboard;
|
||||
import ru.penkrat.stbf.api.Screen;
|
||||
import ru.penkrat.stbf.api.KeyboardBuilder;
|
||||
|
||||
@Getter
|
||||
public class TextScreen implements Screen {
|
||||
|
||||
private final String text;
|
||||
|
||||
private final Keyboard keyboard;
|
||||
|
||||
public TextScreen(String text) {
|
||||
this.text = text;
|
||||
this.keyboard = null;
|
||||
}
|
||||
|
||||
public TextScreen(String text, Keyboard keyboard) {
|
||||
this.text = text;
|
||||
this.keyboard = keyboard;
|
||||
}
|
||||
|
||||
public TextScreen(String text, Action btn) {
|
||||
this(text, KeyboardBuilder.singleKey(btn));
|
||||
}
|
||||
|
||||
public TextScreen(String text, Action btn1, Action btn2) {
|
||||
this(text, KeyboardBuilder.newKeyboard().add(btn1).add(btn2).build());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package ru.penkrat.stbf.tools;
|
||||
|
||||
import lombok.experimental.UtilityClass;
|
||||
import ru.penkrat.stbf.api.Action;
|
||||
import ru.penkrat.stbf.api.RequestMatcher;
|
||||
|
||||
@UtilityClass
|
||||
public class RequestMatchers {
|
||||
|
||||
public RequestMatcher action(Action action) {
|
||||
return request -> request.getMessageText()
|
||||
.filter(text -> matchText(action, text))
|
||||
.isPresent();
|
||||
}
|
||||
|
||||
private static boolean matchText(Action action, String inputText) {
|
||||
return inputText.equalsIgnoreCase(action.getText())
|
||||
|| (action.getCmd() != null && inputText.equalsIgnoreCase(action.getCmd()));
|
||||
}
|
||||
}
|
||||
20
stbf-common/src/test/java/ru/penkrat/stbf/AppTest.java
Normal file
20
stbf-common/src/test/java/ru/penkrat/stbf/AppTest.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package ru.penkrat.stbf;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
{
|
||||
/**
|
||||
* Rigorous Test :-)
|
||||
*/
|
||||
@Test
|
||||
public void shouldAnswerWithTrue()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user