File Coverage

blib/lib/Slack/BlockKit/Block/RichText/Date.pm
Criterion Covered Total %
statement 11 15 73.3
branch 0 4 0.0
condition n/a
subroutine 4 5 80.0
pod 0 1 0.0
total 15 25 60.0


line stmt bran cond sub pod time code
1             package Slack::BlockKit::Block::RichText::Date 0.005;
2             # ABSTRACT: a Block Kit rich text element for a formatted date
3              
4 4     4   27 use Moose;
  4         10  
  4         36  
5 4     4   18948 use MooseX::StrictConstructor;
  4         8  
  4         33  
6              
7             #pod =head1 OVERVIEW
8             #pod
9             #pod This represents a C<date> element in Block Kit, which takes a unix timestamp
10             #pod and displays it in a format appropriate for the reader.
11             #pod
12             #pod B<Be warned!> The element is not documented in the Block Kit documentation,
13             #pod but Slack support disclosed its existence. They said it was just missing
14             #pod documentation, but might it just go away? Who can say.
15             #pod
16             #pod =cut
17              
18 4     4   11962 use v5.36.0;
  4         12  
19              
20             #pod =attr timestamp
21             #pod
22             #pod This is the date (and time) that you want to format, which will be formatted
23             #pod into the reader's own time zone when displayed. It is required, and must be a
24             #pod unix timestamp. (That is: a number of seconds since 1970, as per C<<
25             #pod L<perlfunc/time> >>.
26             #pod
27             #pod =cut
28              
29             has timestamp => (
30             is => 'ro',
31             isa => 'Int', # Maybe this should be stricter, like PosInt, but eh.
32             required => 1,
33             );
34              
35             #pod =attr format
36             #pod
37             #pod This is the format string to be used formatting the timestamp. Because the
38             #pod C<date> rich text element isn't documented in the Block Kit docs (currently),
39             #pod you'll want to find the format specification in the "L<Formatting text for app
40             #pod surfaces|https://api.slack.com/reference/surfaces/formatting#date-formatting>"
41             #pod docs.
42             #pod
43             #pod Something like this is plausible:
44             #pod
45             #pod "{date_short_pretty}, {time}"
46             #pod
47             #pod Probably because of the C<date> element's origin in C<mrkdwn>, it has the odd
48             #pod property that the first character will be capitalized. To suppress this, you
49             #pod can prefix your format string with C<U+200B>, the zero-width space. For
50             #pod example:
51             #pod
52             #pod "\x{200b}{date_short_pretty}, {time}"
53             #pod
54             #pod This is done, by default, in L<Slack::BlockKit::Sugar>'s C<date> function.
55             #pod
56             #pod =cut
57              
58             has format => (
59             is => 'ro',
60             isa => 'Str',
61             );
62              
63             #pod =attr fallback
64             #pod
65             #pod If given, and if the client can't process the given date, this string will be
66             #pod displayed instead. If you put a pre-formatted date string in this, include the
67             #pod time zone, because the reader will expect that it will have been localized.
68             #pod
69             #pod =cut
70              
71             has fallback => (
72             is => 'ro',
73             isa => 'Str',
74             predicate => 'has_fallback',
75             );
76              
77             #pod =attr url
78             #pod
79             #pod If given, the formatted date string will I<also> be a link to this URL.
80             #pod
81             #pod =cut
82              
83             has url => (
84             is => 'ro',
85             isa => 'Str',
86             predicate => 'has_url',
87             );
88              
89 0     0 0   sub as_struct ($self) {
  0            
  0            
90             return {
91 0 0         type => 'date',
    0          
92             timestamp => 0 + $self->timestamp, # 0+ for JSON serialization's sake
93             format => "" . $self->format,
94             ($self->has_fallback ? (fallback => "" + $self->fallback) : ()),
95             ($self->has_url ? (url => "" . $self->url) : ()),
96             };
97             }
98              
99 4     4   27 no Moose;
  4         8  
  4         34  
100             __PACKAGE__->meta->make_immutable;
101              
102             __END__
103              
104             =pod
105              
106             =encoding UTF-8
107              
108             =head1 NAME
109              
110             Slack::BlockKit::Block::RichText::Date - a Block Kit rich text element for a formatted date
111              
112             =head1 VERSION
113              
114             version 0.005
115              
116             =head1 OVERVIEW
117              
118             This represents a C<date> element in Block Kit, which takes a unix timestamp
119             and displays it in a format appropriate for the reader.
120              
121             B<Be warned!> The element is not documented in the Block Kit documentation,
122             but Slack support disclosed its existence. They said it was just missing
123             documentation, but might it just go away? Who can say.
124              
125             =head1 PERL VERSION
126              
127             This module should work on any version of perl still receiving updates from
128             the Perl 5 Porters. This means it should work on any version of perl
129             released in the last two to three years. (That is, if the most recently
130             released version is v5.40, then this module should work on both v5.40 and
131             v5.38.)
132              
133             Although it may work on older versions of perl, no guarantee is made that the
134             minimum required version will not be increased. The version may be increased
135             for any reason, and there is no promise that patches will be accepted to
136             lower the minimum required perl.
137              
138             =head1 ATTRIBUTES
139              
140             =head2 timestamp
141              
142             This is the date (and time) that you want to format, which will be formatted
143             into the reader's own time zone when displayed. It is required, and must be a
144             unix timestamp. (That is: a number of seconds since 1970, as per C<<
145             L<perlfunc/time> >>.
146              
147             =head2 format
148              
149             This is the format string to be used formatting the timestamp. Because the
150             C<date> rich text element isn't documented in the Block Kit docs (currently),
151             you'll want to find the format specification in the "L<Formatting text for app
152             surfaces|https://api.slack.com/reference/surfaces/formatting#date-formatting>"
153             docs.
154              
155             Something like this is plausible:
156              
157             "{date_short_pretty}, {time}"
158              
159             Probably because of the C<date> element's origin in C<mrkdwn>, it has the odd
160             property that the first character will be capitalized. To suppress this, you
161             can prefix your format string with C<U+200B>, the zero-width space. For
162             example:
163              
164             "\x{200b}{date_short_pretty}, {time}"
165              
166             This is done, by default, in L<Slack::BlockKit::Sugar>'s C<date> function.
167              
168             =head2 fallback
169              
170             If given, and if the client can't process the given date, this string will be
171             displayed instead. If you put a pre-formatted date string in this, include the
172             time zone, because the reader will expect that it will have been localized.
173              
174             =head2 url
175              
176             If given, the formatted date string will I<also> be a link to this URL.
177              
178             =head1 AUTHOR
179              
180             Ricardo SIGNES <cpan@semiotic.systems>
181              
182             =head1 COPYRIGHT AND LICENSE
183              
184             This software is copyright (c) 2024 by Ricardo SIGNES.
185              
186             This is free software; you can redistribute it and/or modify it under
187             the same terms as the Perl 5 programming language system itself.
188              
189             =cut