Skip to content

Commit

Permalink
🔇 silent changes: refactor message builder #5 #2
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jul 6, 2024
1 parent a66d962 commit e8c56e8
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 6 deletions.
Binary file modified libs/unify4j-v1.0.0.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion plugin/gradle.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ng:
name: bot4j
version: v1.0.0
enabled_link: false # enable compression and attachment of the external libraries
enabled_link: true # enable compression and attachment of the external libraries
jars:
# unify4J: Java 1.8 skeleton library offering a rich toolkit of utility functions
# for collections, strings, date/time, JSON, maps, and more.
Expand Down
14 changes: 14 additions & 0 deletions plugin/src/main/groovy/org/bot4j/telegram/message/HtmlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ public HtmlBuilder timestamp() {
return this.timestamp(new Date());
}

@Override
public HtmlBuilder timestampParenthesis() {
String f = String.format("%s%s%s",
Ascii.Punctuation.LEFT_PARENTHESIS,
Time4j.format(new Date(), TimeFormatText.BIBLIOGRAPHY_COMPLETE_EPOCH_PATTERN),
Ascii.Punctuation.RIGHT_PARENTHESIS);
return this.code(f);
}

@Override
public HtmlBuilder vertical(String text) {
message.append(String4j.repeat(Ascii.Symbol.VERTICAL_LINE, 2))
Expand Down Expand Up @@ -407,6 +416,11 @@ public HtmlBuilder text(String text) {
return this.space();
}

@Override
public HtmlBuilder text(String text, int repeat) {
return this.text(String4j.repeat(text, repeat));
}

@Override
public HtmlBuilder text(Object value) {
if (value == null) {
Expand Down
222 changes: 222 additions & 0 deletions plugin/src/main/groovy/org/bot4j/telegram/message/HttpBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
package org.bot4j.telegram.message;

import org.alpha4j.common.Map4j;
import org.bot4j.telegram.model.enums.TelegramIconMode;
import org.unify4j.common.*;
import org.unify4j.model.builder.HttpStatusBuilder;
import org.unify4j.model.c.Method;
import org.unify4j.model.enums.TimezoneType;
import org.unify4j.model.response.HttpResponse;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;

public class HttpBuilder extends MarkdownBuilder {
protected final Map4j<String, Object> placeholders;
protected Date start;
protected Date end;
protected String timezone;

public HttpBuilder() {
super();
this.placeholders = new Map4j<>();
}

protected static class Index {
public static final String METHOD_INDEX = "method_index";
public static final String BASE_URL_INDEX = "base_url_index";
public static final String PATH_INDEX = "path_index";
public static final String QUERY_INDEX = "query_index";
public static final String REQUEST_INDEX = "request_index";
public static final String RESPONSE_INDEX = "response_index";
public static final String HEADER_INDEX = "header_index";
public static final String STATUS_INDEX = "code_index";
public static final String API_DESC_INDEX = "api_desc_index";
}

public HttpBuilder apiDesc(String desc) {
this.placeholders.put(Index.API_DESC_INDEX, desc);
return this;
}

public HttpBuilder method(String method) {
this.placeholders.put(Index.METHOD_INDEX, method);
return this;
}

public HttpBuilder method(Method method) {
if (method == null) {
return this;
}
return this.method(method.getName());
}

public HttpBuilder baseUrl(String url) {
this.placeholders.put(Index.BASE_URL_INDEX, url);
return this;
}

public HttpBuilder path(String url) {
this.placeholders.put(Index.PATH_INDEX, url);
return this;
}

public HttpBuilder query(Map<String, ?> queries) {
if (Collection4j.isEmptyMap(queries)) {
return this;
}
this.placeholders.put(Index.QUERY_INDEX, queries);
return this;
}

public HttpBuilder request(Map<String, ?> request) {
if (Collection4j.isEmptyMap(request)) {
return this;
}
this.placeholders.put(Index.REQUEST_INDEX, request);
return this;
}

public HttpBuilder response(Map<String, ?> response) {
if (Collection4j.isEmptyMap(response)) {
return this;
}
this.placeholders.put(Index.RESPONSE_INDEX, response);
return this;
}

public HttpBuilder header(Map<String, ?> headers) {
if (Collection4j.isEmptyMap(headers)) {
return this;
}
this.placeholders.put(Index.HEADER_INDEX, headers);
return this;
}

public HttpBuilder status(int httpStatus) {
this.placeholders.put(Index.STATUS_INDEX, httpStatus);
return this;
}

public HttpBuilder status(HttpResponse response) {
if (response == null) {
return this;
}
return this.status(response.getCode());
}

public HttpBuilder startTime() {
this.start = new Date();
return this;
}

public HttpBuilder startTime(Date date) {
this.start = date;
return this;
}

public HttpBuilder endTime() {
this.end = new Date();
return this;
}

public HttpBuilder endTime(Date date) {
this.end = date;
return this;
}

public HttpBuilder timezone(String timezone) {
this.timezone = timezone;
return this;
}

public HttpBuilder timezone(TimezoneType timezone) {
return this.timezone(timezone.getTimeZoneId());
}

@SuppressWarnings({"unchecked"})
@Override
public String toString() {
if (this.placeholders.containsKey(Index.STATUS_INDEX)) {
int status = (int) this.placeholders.get(Index.STATUS_INDEX);
if (HttpStatusBuilder.isSuccess(status)) {
this.icon(TelegramIconMode.SUCCESS)
.bold(status)
.timestampParenthesis()
.line();
} else {
this.icon(TelegramIconMode.ERROR).bold(status)
.timestampParenthesis()
.line();
}
} else {
this.icon(TelegramIconMode.TS_3).timestamp().line();
}
if (this.placeholders.containsKey(Index.API_DESC_INDEX)) {
this.bold("API").italic((String) this.placeholders.get(Index.API_DESC_INDEX)).line();
}
if (this.placeholders.containsKey(Index.METHOD_INDEX)) {
this.bold((String) this.placeholders.get(Index.METHOD_INDEX));
}
if (this.placeholders.containsKey(Index.BASE_URL_INDEX)) {
String url;
if (this.placeholders.containsKey(Index.PATH_INDEX)) {
url = String.format("%s%s",
this.placeholders.get(Index.BASE_URL_INDEX),
this.placeholders.get(Index.PATH_INDEX));
} else {
url = (String) this.placeholders.get(Index.BASE_URL_INDEX);
}
if (this.placeholders.containsKey(Index.QUERY_INDEX)) {
Map<String, Object> queries = (Map<String, Object>) Json4j.toMapFrom(this.placeholders.get(Index.QUERY_INDEX));
try {
url = Request4j.appendQueryParams(url, queries);
} catch (UnsupportedEncodingException ignored) {

}
}
this.code(String4j.trimWhitespace(url));
}
this.line(2);
if (Object4j.allNotNull(this.start)) {
if (String4j.isEmpty(this.timezone)) {
this.bold("RFT:").timestamp(this.start).line();
} else {
this.bold("RFT:").timestamp(this.start, TimeZone.getTimeZone(this.timezone)).line();
}
}
if (Object4j.allNotNull(this.end)) {
if (String4j.isEmpty(this.timezone)) {
this.bold("RT:").timestamp(this.end).line();
} else {
this.bold("RT:").timestamp(this.end, TimeZone.getTimeZone(this.timezone)).line();
}
}
if (Object4j.allNotNull(this.start, this.end)) {
this.icon(TelegramIconMode.RIGHT_ARROW_1)
.bold("Time:")
.code(Time4j.sinceSmallRecently(this.end, this.start))
.line();
}
if (this.placeholders.containsKey(Index.HEADER_INDEX)) {
this.bold("H:").line();
Map<String, Object> headers = (Map<String, Object>) Json4j.toMapFrom(this.placeholders.get(Index.HEADER_INDEX));
if (Collection4j.isNotEmptyMap(headers)) {
headers.forEach((key, value) -> {
this.code(key).text(":").code(value.toString()).line();
});
}
}
if (this.placeholders.containsKey(Index.REQUEST_INDEX)) {
Map<String, Object> request = (Map<String, Object>) Json4j.toMapFrom(this.placeholders.get(Index.REQUEST_INDEX));
this.bold("Req:").preformatted("json", request);
}
if (this.placeholders.containsKey(Index.RESPONSE_INDEX)) {
Map<String, Object> request = (Map<String, Object>) Json4j.toMapFrom(this.placeholders.get(Index.RESPONSE_INDEX));
this.bold("Resp:").preformatted("json", request);
}
return super.toString();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ public MarkdownBuilder timestamp() {
return this.timestamp(new Date());
}

@Override
public MarkdownBuilder timestampParenthesis() {
String f = String.format("%s%s%s",
Ascii.Punctuation.LEFT_PARENTHESIS,
Time4j.format(new Date(), TimeFormatText.BIBLIOGRAPHY_COMPLETE_EPOCH_PATTERN),
Ascii.Punctuation.RIGHT_PARENTHESIS);
return this.code(f);
}

@Override
public MarkdownBuilder vertical(String text) {
message.append(String4j.repeat(Ascii.Symbol.VERTICAL_LINE, 2))
Expand Down Expand Up @@ -238,6 +247,11 @@ public MarkdownBuilder text(String text) {
return this.space();
}

@Override
public MarkdownBuilder text(String text, int repeat) {
return this.text(String4j.repeat(text, repeat));
}

@Override
public MarkdownBuilder text(Object value) {
if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public interface MessageBuilder<T> {

T timestamp();

T timestampParenthesis();

T vertical(String text);

T vertical(Object value);
Expand Down Expand Up @@ -59,6 +61,7 @@ public interface MessageBuilder<T> {

T preformatted(String lang, String text);

@SuppressWarnings({"UnusedReturnValue"})
T preformatted(String lang, Object value);

T preformatted(String lang, Path filename);
Expand All @@ -67,6 +70,8 @@ public interface MessageBuilder<T> {

T text(String text);

T text(String text, int repeat);

T text(Object value);

T line();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public enum TelegramIconMode {
MESSAGE("\uD83D\uDCAC"),
TS_1("\uD83C\uDFAF"),
TS_2("\uD83D\uDDD3"),
TS_3("\uD83D\uDCCD"),
NOTIFY("\uD83D\uDCE3"),
TADA("\uD83C\uDF89"),
SETTING("\uD83D\uDD27"),
Expand All @@ -22,7 +23,9 @@ public enum TelegramIconMode {
BUG_1("\uD83D\uDC1E"),
BUG_2("\uD83D\uDC7E"),
BUG_3("\uD83E\uDEB2"),
BUG_4("\uD83D\uDC1B");
BUG_4("\uD83D\uDC1B"),
RIGHT_ARROW_1("\uD83D\uDC49"),
LEFT_ARROW_1("\uD83D\uDC48");

private final String icon;

Expand Down

0 comments on commit e8c56e8

Please sign in to comment.