Skip to content

Commit d31a9c3

Browse files
zimegClaude
andauthored
feat(block-kit): add url_source element example (#127)
Co-authored-by: Claude <svc-devxp-claude@slack-corp.com>
1 parent 8e4a186 commit d31a9c3

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

‎block-kit/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Read the [docs](https://docs.slack.dev/block-kit/) to learn concepts behind thes
5757
- **[Text](https://docs.slack.dev/reference/block-kit/block-elements/text-element)**: Displays text, optionally with styling. [Implementation](./src/main/java/elements/Text.java).
5858
- **[Time picker](https://docs.slack.dev/reference/block-kit/block-elements/time-picker-element)**: Allows users to enter numerical data into a single-line field. [Implementation](./src/main/java/elements/TimePicker.java).
5959
- **[URL input](https://docs.slack.dev/reference/block-kit/block-elements/url-input-element)**: Allows user to enter a URL into a single-line field. [Implementation](./src/main/java/elements/UrlInput.java).
60+
- **[URL source](https://docs.slack.dev/reference/block-kit/block-elements/url-source-element)**: Displays a URL source for referencing within a task card block. [Implementation](./src/main/java/elements/UrlSource.java).
6061
- **[User](https://docs.slack.dev/reference/block-kit/block-elements/user-element)**: Renders as a mention of a user. [Implementation](./src/main/java/elements/User.java).
6162
- **[Usergroup](https://docs.slack.dev/reference/block-kit/block-elements/usergroup-element)**: Renders as a mention of a user group. [Implementation](./src/main/java/elements/UserGroup.java).
6263
- **[Workflow button](https://docs.slack.dev/reference/block-kit/block-elements/workflow-button-element)**: Allows users to run a link trigger with customizable inputs. [Implementation](./src/main/java/elements/WorkflowButton.java).
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package elements;
2+
3+
import com.slack.api.model.block.Blocks;
4+
import com.slack.api.model.block.TaskCardBlock;
5+
import com.slack.api.model.block.element.UrlSourceElement;
6+
import java.util.List;
7+
8+
/**
9+
* Displays a URL source for referencing within a task card block.
10+
* {@link https://docs.slack.dev/reference/block-kit/block-elements/url-source-element/}
11+
*/
12+
public class UrlSource {
13+
/**
14+
* A URL source element.
15+
*/
16+
public static UrlSourceElement example01() {
17+
UrlSourceElement element = UrlSourceElement.builder()
18+
.url("https://docs.slack.dev/")
19+
.text("Slack API docs")
20+
.build();
21+
return element;
22+
}
23+
24+
/**
25+
* A task card block with URL source elements.
26+
*/
27+
public static TaskCardBlock example02() {
28+
TaskCardBlock block = Blocks.taskCard(t -> t.taskId("task_1")
29+
.title("Scientific findings")
30+
.status("complete")
31+
.sources(List.of(
32+
UrlSourceElement.builder()
33+
.url("https://docs.example.com/")
34+
.text("Tracy's delightful docs")
35+
.build(),
36+
UrlSourceElement.builder()
37+
.url("https://research.example.com/")
38+
.text("Haley's resourceful research")
39+
.build())));
40+
return block;
41+
}
42+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package elements;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import com.google.gson.JsonParser;
6+
import com.slack.api.model.block.TaskCardBlock;
7+
import com.slack.api.model.block.element.UrlSourceElement;
8+
import com.slack.api.util.json.GsonFactory;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class UrlSourceTest {
12+
@Test
13+
public void testExample01() {
14+
UrlSourceElement element = UrlSource.example01();
15+
String actual = GsonFactory.createSnakeCase().toJson(element);
16+
String expected = """
17+
{
18+
"type": "url",
19+
"url": "https://docs.slack.dev/",
20+
"text": "Slack API docs"
21+
}
22+
""";
23+
assertEquals(JsonParser.parseString(expected), JsonParser.parseString(actual));
24+
}
25+
26+
@Test
27+
public void testExample02() {
28+
TaskCardBlock block = UrlSource.example02();
29+
String actual = GsonFactory.createSnakeCase().toJson(block);
30+
String expected = """
31+
{
32+
"type": "task_card",
33+
"task_id": "task_1",
34+
"title": "Scientific findings",
35+
"status": "complete",
36+
"sources": [
37+
{
38+
"type": "url",
39+
"url": "https://docs.example.com/",
40+
"text": "Tracy's delightful docs"
41+
},
42+
{
43+
"type": "url",
44+
"url": "https://research.example.com/",
45+
"text": "Haley's resourceful research"
46+
}
47+
]
48+
}
49+
""";
50+
assertEquals(JsonParser.parseString(expected), JsonParser.parseString(actual));
51+
}
52+
}

0 commit comments

Comments
 (0)