Profile.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Profile/TypeConstraints.pm ↑ Summary
1 package Net::PMP::Profile;
2 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Data::Clean::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 0 ▶use Net::PMP::Profile::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use Net::PMP::CollectionDoc;
7 use Net::PMP::CollectionDoc::Link;
8  
9 our $VERSION = '0.102';
10  
11 # attributes
12 has 'title' => ( is => 'rw', isa => 'Str', required => 1, );
13 has 'hreflang' =>
14 ( is => 'rw', isa => 'Net::PMP::Type::ISO6391', default => sub {'en'}, );
15 has 'published' =>
16 ( is => 'rw', isa => 'Net::PMP::Type::DateTimeOrStr', coerce => 1, );
17 has 'valid' =>
18 ( is => 'rw', isa => 'Net::PMP::Type::ValidDates', coerce => 1, );
19 has 'tags' => (
20 is => 'rw',
21 isa => 'ArrayRef[Str]',
22 traits => ['Array'],
23 handles => { add_tag => 'push', },
24 );
25 has 'itags' => (
26 is => 'rw',
27 isa => 'ArrayRef[Str]',
28 traits => ['Array'],
29 handles => { add_itag => 'push', },
30 );
31 has 'description' => ( is => 'rw', isa => 'Str', );
32 has 'byline' => ( is => 'rw', isa => 'Str', );
33 has 'guid' => ( is => 'rw', isa => 'Net::PMP::Type::GUID', );
34 has 'href' => ( is => 'rw', isa => 'Net::PMP::Type::Href', coerce => 1 );
35  
36 # links
37 has 'author' => ( is => 'rw', isa => 'Net::PMP::Type::Links', coerce => 1, );
38 has 'copyright' =>
39 ( is => 'rw', isa => 'Net::PMP::Type::Links', coerce => 1, );
40 has 'distributor' =>
41 ( is => 'rw', isa => 'Net::PMP::Type::Links', coerce => 1, );
42 has 'profile' => ( is => 'rw', isa => 'Net::PMP::Type::Links', coerce => 1, );
43 has 'collection' =>
44 ( is => 'rw', isa => 'Net::PMP::Type::Links', coerce => 1, );
45 has 'item' => ( is => 'rw', isa => 'Net::PMP::Type::Links', coerce => 1, );
46 has 'permission' =>
47 ( is => 'rw', isa => 'Net::PMP::Type::Permissions', coerce => 1, );
48 has 'alternate' =>
49 ( is => 'rw', isa => 'Net::PMP::Type::Links', coerce => 1, );
50  
51 sub get_profile_url {'https://api.pmp.io/profiles/base'}
52 sub get_profile_title { ref(shift) }
53  
54 # singleton for class
55 my $cleaner = Data::Clean::JSON->new(
56 DateTime => [ call_method => 'stringify' ],
57 'Net::PMP::CollectionDoc::Link' => [ call_method => 'as_hash' ],
58 'Net::PMP::CollectionDoc::Permission' => [ call_method => 'as_hash' ],
59 SCALAR => ['deref_scalar'],
60 '-ref' => ['replace_with_ref'],
61 '-circular' => 0, #['detect_circular'],
62 '-obj' => ['unbless'],
63 );
64  
65 sub as_doc {
66 my $self = shift;
67  
68 # coerce into hash
69 my %attrs = %{$self};
70  
71 # pull out those attributes which are really links
72 my %links = (
73 profile => [
74 Net::PMP::CollectionDoc::Link->new(
75 href => $self->get_profile_url,
76 title => $self->get_profile_title,
77 )
78 ]
79 );
80  
81 my %class_attrs = map { $_->name => $_ } $self->meta->get_all_attributes;
82  
83 # not an attribute, a top-level key.
84 my $href = delete $attrs{href};
85  
86 for my $k ( keys %attrs ) {
87 if ( exists $class_attrs{$k} ) {
88 my $attr = $class_attrs{$k};
89 my $isa = $attr->{isa};
90  
91 #warn "key $k => isa $isa";
92 if ( $isa eq 'Net::PMP::Type::Links' ) {
93 $links{$k} = delete $attrs{$k};
94 }
95 elsif ( $isa eq 'Net::PMP::Type::Link' ) {
96 $links{$k} = [ delete $attrs{$k} ];
97 }
98 elsif ( $isa eq 'Net::PMP::Type::Permissions' ) {
99 $links{$k} = delete $attrs{$k};
100 }
101 elsif ( $isa eq 'Net::PMP::Type::Permission' ) {
102 $links{$k} = [ delete $attrs{$k} ];
103 }
104 elsif ( $isa eq 'Net::PMP::Type::MediaEnclosures' ) {
105 $links{$k} = [ map { $_->as_hash } @{ delete $attrs{$k} } ];
106 }
107 elsif ( $isa eq 'Net::PMP::Type::MediaEnclosure' ) {
108 $links{$k} = [ delete( $attrs{$k} )->as_hash ];
109 }
110 }
111 }
112  
113 # CollectionDoc can only work with strings
114 my %doc = ( attributes => \%attrs, links => \%links );
115  
116 # only pass href if it is set
117 $doc{href} = $href if $href;
118  
119 # coerce everything into something CollectionDoc can handle.
120 my $clean = $cleaner->clean_in_place( \%doc );
121  
122 return Net::PMP::CollectionDoc->new($clean);
123  
124 }
125  
126 __PACKAGE__->meta->make_immutable();
127  
128 1;
129  
130 __END__
« Profile/TypeConstraints.pm ↑ Summary