Types.pm

filter: Statementstmt 87.5% 70 / 80 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.9% 17 / 21 Podpod n/a totaltotal 85.9% 92 / 107 CC 5 SCAR 4.7
CC
5
Cov
87%
__ANON__
3.0
__ANON__
3.0
__ANON__
2.0
CRAP 1.6
« Entity/Status.pm ↑ Summary Entity/Application.pm »
1 package Mastodon::Types;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = '0.018'; # TRIAL
7  
8 7 ▶use Type::Library -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 ▶use Type::Utils -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Types::Standard qw( Str HashRef Num );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Types::Path::Tiny qw( File to_File);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Class::Load qw( load_class );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 7 duck_type 'UserAgent', [qw( get post delete )];
20  
21 7 class_type 'URI', { class => 'URI' };
22  
23 139 ▶coerce 'URI', from Str, via {
Subroutine __ANON__: called CC=3 SCAR=11.0
24 139 s{^/+}{}g;
25 139 ▶ my $uri = URI->new((m{^https?://} ? q{} : 'https://') . $_);
Branch: 1 branch
BranchTrueFalseTotal
m[^https?://] ? : 85 54 139
26 139 ▶ $uri->scheme('https') unless $uri->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->scheme 0 139 139
27 139 return $uri;
28 7 };
29  
30 # We provide our own DateTime type because the Types::DateTime distribution
31 # is currently undermaintained
32  
33 7 class_type 'DateTime', { class => 'DateTime' };
34  
35 7 class_type 'HTTPResponse', { class => 'HTTP::Response' };
36  
37 coerce 'DateTime',
38 from Num,
39 0 ▶ via { 'DateTime'->from_epoch( epoch => $_ ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 from Str,
41 23 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 23 require DateTime::Format::Strptime;
43 23 DateTime::Format::Strptime->new(
44 pattern => '%FT%T.%3N%Z',
45 on_error => 'croak',
46 )->parse_datetime($_);
47 7 };
48  
49 # Validation here could be improved
50 # It is either a username if a local account, or a username@instance.tld
51 # but what characters are valid?
52 7 declare 'Acct', as Str;
53  
54 declare 'Image',
55 0 ▶ as Str, where { m{^data:image/(?:png|jpeg);base64,[a-zA-Z0-9/+=\n]+$} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
56  
57 0 ▶coerce File, from Str, via {
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 0 require Path::Tiny;
59 0 return Path::Tiny::path( $_ );
60 7 };
61  
62 coerce 'Image',
63 from File->coercibles,
64 0 ▶ via {
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 my $file = to_File($_);
66 0 require Image::Info;
67 0 require MIME::Base64;
68 0 my $type = lc Image::Info::image_type( $file->stringify )->{file_type};
69 0 my $img = "data:image/$type;base64,"
70 . MIME::Base64::encode_base64( $file->slurp_raw );
71 0 return $img;
72 7 };
73  
74 # Entity types
75  
76 7 my @entities = qw(
77 Status Account Instance Attachment Card Context Mention
78 Notification Relationship Report Results Error Tag Application
79 );
80  
81 7 foreach my $name (@entities) {
82 98 class_type $name, { class => "Mastodon::Entity::$name" };
83 21 ▶ coerce $name, from HashRef, via {
Subroutine __ANON__: called CC=1 SCAR=0.0
84 21 load_class "Mastodon::Entity::$name";
85 21 "Mastodon::Entity::$name"->new($_);
86 98 };
87 }
88  
89 7 role_type 'Entity', { role => 'Mastodon::Role::Entity' };
90  
91 coerce 'Instance',
92 from Str,
93 20 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
94 20 require Mastodon::Entity::Instance;
95 20 Mastodon::Entity::Instance->new({
96 uri => $_,
97 });
98 7 };
99  
100 coerce 'Entity',
101 from HashRef,
102 37 ▶ via {
Subroutine __ANON__: called CC=3 SCAR=11.0
103 37 my $hash = $_;
104 37 my $entity;
105  
106 7 ▶ use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 37 foreach my $name (@entities) {
108 176 ▶ $entity = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
109 176 load_class "Mastodon::Entity::$name";
110 176 "Mastodon::Entity::$name"->new($hash);
111 176 };
112 176 ▶ last if defined $entity;
Branch: 1 branch
BranchTrueFalseTotal
if defined $entity 37 139 176
113 }
114  
115 37 return $entity;
116 7 };
117  
118 7 1;
« Entity/Status.pm ↑ Summary Entity/Application.pm »