Skip to content

Commit e8c56e8

Browse files
committed
🔇 silent changes: refactor message builder #5 #2
1 parent a66d962 commit e8c56e8

File tree

8 files changed

+260
-6
lines changed

8 files changed

+260
-6
lines changed

Diff for: libs/unify4j-v1.0.0.jar

579 Bytes
Binary file not shown.

Diff for: plugin/gradle.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
ng:
22
name: bot4j
33
version: v1.0.0
4-
enabled_link: false # enable compression and attachment of the external libraries
4+
enabled_link: true # enable compression and attachment of the external libraries
55
jars:
66
# unify4J: Java 1.8 skeleton library offering a rich toolkit of utility functions
77
# for collections, strings, date/time, JSON, maps, and more.

Diff for: plugin/src/main/groovy/org/bot4j/telegram/message/HtmlBuilder.java

+14
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ public HtmlBuilder timestamp() {
7171
return this.timestamp(new Date());
7272
}
7373

74+
@Override
75+
public HtmlBuilder timestampParenthesis() {
76+
String f = String.format("%s%s%s",
77+
Ascii.Punctuation.LEFT_PARENTHESIS,
78+
Time4j.format(new Date(), TimeFormatText.BIBLIOGRAPHY_COMPLETE_EPOCH_PATTERN),
79+
Ascii.Punctuation.RIGHT_PARENTHESIS);
80+
return this.code(f);
81+
}
82+
7483
@Override
7584
public HtmlBuilder vertical(String text) {
7685
message.append(String4j.repeat(Ascii.Symbol.VERTICAL_LINE, 2))
@@ -407,6 +416,11 @@ public HtmlBuilder text(String text) {
407416
return this.space();
408417
}
409418

419+
@Override
420+
public HtmlBuilder text(String text, int repeat) {
421+
return this.text(String4j.repeat(text, repeat));
422+
}
423+
410424
@Override
411425
public HtmlBuilder text(Object value) {
412426
if (value == null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
package org.bot4j.telegram.message;
2+
3+
import org.alpha4j.common.Map4j;
4+
import org.bot4j.telegram.model.enums.TelegramIconMode;
5+
import org.unify4j.common.*;
6+
import org.unify4j.model.builder.HttpStatusBuilder;
7+
import org.unify4j.model.c.Method;
8+
import org.unify4j.model.enums.TimezoneType;
9+
import org.unify4j.model.response.HttpResponse;
10+
11+
import java.io.UnsupportedEncodingException;
12+
import java.util.Date;
13+
import java.util.Map;
14+
import java.util.TimeZone;
15+
16+
public class HttpBuilder extends MarkdownBuilder {
17+
protected final Map4j<String, Object> placeholders;
18+
protected Date start;
19+
protected Date end;
20+
protected String timezone;
21+
22+
public HttpBuilder() {
23+
super();
24+
this.placeholders = new Map4j<>();
25+
}
26+
27+
protected static class Index {
28+
public static final String METHOD_INDEX = "method_index";
29+
public static final String BASE_URL_INDEX = "base_url_index";
30+
public static final String PATH_INDEX = "path_index";
31+
public static final String QUERY_INDEX = "query_index";
32+
public static final String REQUEST_INDEX = "request_index";
33+
public static final String RESPONSE_INDEX = "response_index";
34+
public static final String HEADER_INDEX = "header_index";
35+
public static final String STATUS_INDEX = "code_index";
36+
public static final String API_DESC_INDEX = "api_desc_index";
37+
}
38+
39+
public HttpBuilder apiDesc(String desc) {
40+
this.placeholders.put(Index.API_DESC_INDEX, desc);
41+
return this;
42+
}
43+
44+
public HttpBuilder method(String method) {
45+
this.placeholders.put(Index.METHOD_INDEX, method);
46+
return this;
47+
}
48+
49+
public HttpBuilder method(Method method) {
50+
if (method == null) {
51+
return this;
52+
}
53+
return this.method(method.getName());
54+
}
55+
56+
public HttpBuilder baseUrl(String url) {
57+
this.placeholders.put(Index.BASE_URL_INDEX, url);
58+
return this;
59+
}
60+
61+
public HttpBuilder path(String url) {
62+
this.placeholders.put(Index.PATH_INDEX, url);
63+
return this;
64+
}
65+
66+
public HttpBuilder query(Map<String, ?> queries) {
67+
if (Collection4j.isEmptyMap(queries)) {
68+
return this;
69+
}
70+
this.placeholders.put(Index.QUERY_INDEX, queries);
71+
return this;
72+
}
73+
74+
public HttpBuilder request(Map<String, ?> request) {
75+
if (Collection4j.isEmptyMap(request)) {
76+
return this;
77+
}
78+
this.placeholders.put(Index.REQUEST_INDEX, request);
79+
return this;
80+
}
81+
82+
public HttpBuilder response(Map<String, ?> response) {
83+
if (Collection4j.isEmptyMap(response)) {
84+
return this;
85+
}
86+
this.placeholders.put(Index.RESPONSE_INDEX, response);
87+
return this;
88+
}
89+
90+
public HttpBuilder header(Map<String, ?> headers) {
91+
if (Collection4j.isEmptyMap(headers)) {
92+
return this;
93+
}
94+
this.placeholders.put(Index.HEADER_INDEX, headers);
95+
return this;
96+
}
97+
98+
public HttpBuilder status(int httpStatus) {
99+
this.placeholders.put(Index.STATUS_INDEX, httpStatus);
100+
return this;
101+
}
102+
103+
public HttpBuilder status(HttpResponse response) {
104+
if (response == null) {
105+
return this;
106+
}
107+
return this.status(response.getCode());
108+
}
109+
110+
public HttpBuilder startTime() {
111+
this.start = new Date();
112+
return this;
113+
}
114+
115+
public HttpBuilder startTime(Date date) {
116+
this.start = date;
117+
return this;
118+
}
119+
120+
public HttpBuilder endTime() {
121+
this.end = new Date();
122+
return this;
123+
}
124+
125+
public HttpBuilder endTime(Date date) {
126+
this.end = date;
127+
return this;
128+
}
129+
130+
public HttpBuilder timezone(String timezone) {
131+
this.timezone = timezone;
132+
return this;
133+
}
134+
135+
public HttpBuilder timezone(TimezoneType timezone) {
136+
return this.timezone(timezone.getTimeZoneId());
137+
}
138+
139+
@SuppressWarnings({"unchecked"})
140+
@Override
141+
public String toString() {
142+
if (this.placeholders.containsKey(Index.STATUS_INDEX)) {
143+
int status = (int) this.placeholders.get(Index.STATUS_INDEX);
144+
if (HttpStatusBuilder.isSuccess(status)) {
145+
this.icon(TelegramIconMode.SUCCESS)
146+
.bold(status)
147+
.timestampParenthesis()
148+
.line();
149+
} else {
150+
this.icon(TelegramIconMode.ERROR).bold(status)
151+
.timestampParenthesis()
152+
.line();
153+
}
154+
} else {
155+
this.icon(TelegramIconMode.TS_3).timestamp().line();
156+
}
157+
if (this.placeholders.containsKey(Index.API_DESC_INDEX)) {
158+
this.bold("API").italic((String) this.placeholders.get(Index.API_DESC_INDEX)).line();
159+
}
160+
if (this.placeholders.containsKey(Index.METHOD_INDEX)) {
161+
this.bold((String) this.placeholders.get(Index.METHOD_INDEX));
162+
}
163+
if (this.placeholders.containsKey(Index.BASE_URL_INDEX)) {
164+
String url;
165+
if (this.placeholders.containsKey(Index.PATH_INDEX)) {
166+
url = String.format("%s%s",
167+
this.placeholders.get(Index.BASE_URL_INDEX),
168+
this.placeholders.get(Index.PATH_INDEX));
169+
} else {
170+
url = (String) this.placeholders.get(Index.BASE_URL_INDEX);
171+
}
172+
if (this.placeholders.containsKey(Index.QUERY_INDEX)) {
173+
Map<String, Object> queries = (Map<String, Object>) Json4j.toMapFrom(this.placeholders.get(Index.QUERY_INDEX));
174+
try {
175+
url = Request4j.appendQueryParams(url, queries);
176+
} catch (UnsupportedEncodingException ignored) {
177+
178+
}
179+
}
180+
this.code(String4j.trimWhitespace(url));
181+
}
182+
this.line(2);
183+
if (Object4j.allNotNull(this.start)) {
184+
if (String4j.isEmpty(this.timezone)) {
185+
this.bold("RFT:").timestamp(this.start).line();
186+
} else {
187+
this.bold("RFT:").timestamp(this.start, TimeZone.getTimeZone(this.timezone)).line();
188+
}
189+
}
190+
if (Object4j.allNotNull(this.end)) {
191+
if (String4j.isEmpty(this.timezone)) {
192+
this.bold("RT:").timestamp(this.end).line();
193+
} else {
194+
this.bold("RT:").timestamp(this.end, TimeZone.getTimeZone(this.timezone)).line();
195+
}
196+
}
197+
if (Object4j.allNotNull(this.start, this.end)) {
198+
this.icon(TelegramIconMode.RIGHT_ARROW_1)
199+
.bold("Time:")
200+
.code(Time4j.sinceSmallRecently(this.end, this.start))
201+
.line();
202+
}
203+
if (this.placeholders.containsKey(Index.HEADER_INDEX)) {
204+
this.bold("H:").line();
205+
Map<String, Object> headers = (Map<String, Object>) Json4j.toMapFrom(this.placeholders.get(Index.HEADER_INDEX));
206+
if (Collection4j.isNotEmptyMap(headers)) {
207+
headers.forEach((key, value) -> {
208+
this.code(key).text(":").code(value.toString()).line();
209+
});
210+
}
211+
}
212+
if (this.placeholders.containsKey(Index.REQUEST_INDEX)) {
213+
Map<String, Object> request = (Map<String, Object>) Json4j.toMapFrom(this.placeholders.get(Index.REQUEST_INDEX));
214+
this.bold("Req:").preformatted("json", request);
215+
}
216+
if (this.placeholders.containsKey(Index.RESPONSE_INDEX)) {
217+
Map<String, Object> request = (Map<String, Object>) Json4j.toMapFrom(this.placeholders.get(Index.RESPONSE_INDEX));
218+
this.bold("Resp:").preformatted("json", request);
219+
}
220+
return super.toString();
221+
}
222+
}

Diff for: plugin/src/main/groovy/org/bot4j/telegram/message/HttpMessageBuilder.java

-4
This file was deleted.

Diff for: plugin/src/main/groovy/org/bot4j/telegram/message/MarkdownBuilder.java

+14
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ public MarkdownBuilder timestamp() {
7171
return this.timestamp(new Date());
7272
}
7373

74+
@Override
75+
public MarkdownBuilder timestampParenthesis() {
76+
String f = String.format("%s%s%s",
77+
Ascii.Punctuation.LEFT_PARENTHESIS,
78+
Time4j.format(new Date(), TimeFormatText.BIBLIOGRAPHY_COMPLETE_EPOCH_PATTERN),
79+
Ascii.Punctuation.RIGHT_PARENTHESIS);
80+
return this.code(f);
81+
}
82+
7483
@Override
7584
public MarkdownBuilder vertical(String text) {
7685
message.append(String4j.repeat(Ascii.Symbol.VERTICAL_LINE, 2))
@@ -238,6 +247,11 @@ public MarkdownBuilder text(String text) {
238247
return this.space();
239248
}
240249

250+
@Override
251+
public MarkdownBuilder text(String text, int repeat) {
252+
return this.text(String4j.repeat(text, repeat));
253+
}
254+
241255
@Override
242256
public MarkdownBuilder text(Object value) {
243257
if (value == null) {

Diff for: plugin/src/main/groovy/org/bot4j/telegram/message/MessageBuilder.java

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public interface MessageBuilder<T> {
2727

2828
T timestamp();
2929

30+
T timestampParenthesis();
31+
3032
T vertical(String text);
3133

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

6062
T preformatted(String lang, String text);
6163

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

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

6871
T text(String text);
6972

73+
T text(String text, int repeat);
74+
7075
T text(Object value);
7176

7277
T line();

Diff for: plugin/src/main/groovy/org/bot4j/telegram/model/enums/TelegramIconMode.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public enum TelegramIconMode {
1212
MESSAGE("\uD83D\uDCAC"),
1313
TS_1("\uD83C\uDFAF"),
1414
TS_2("\uD83D\uDDD3"),
15+
TS_3("\uD83D\uDCCD"),
1516
NOTIFY("\uD83D\uDCE3"),
1617
TADA("\uD83C\uDF89"),
1718
SETTING("\uD83D\uDD27"),
@@ -22,7 +23,9 @@ public enum TelegramIconMode {
2223
BUG_1("\uD83D\uDC1E"),
2324
BUG_2("\uD83D\uDC7E"),
2425
BUG_3("\uD83E\uDEB2"),
25-
BUG_4("\uD83D\uDC1B");
26+
BUG_4("\uD83D\uDC1B"),
27+
RIGHT_ARROW_1("\uD83D\uDC49"),
28+
LEFT_ARROW_1("\uD83D\uDC48");
2629

2730
private final String icon;
2831

0 commit comments

Comments
 (0)