| 1 | package XML::Loy::ActivityStreams; | ||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use XML::Loy with => ( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | prefix => 'activity', | ||||||||||
| 6 | namespace => 'http://activitystrea.ms/schema/1.0/' | ||||||||||
| 7 | 4 | ); | |||||||||
| 8 | |||||||||||
| 9 | # Todo: support to_json | ||||||||||
| 10 | # Todo: verbs and object-types may need namespaces | ||||||||||
| 11 | # Todo: Support ActivityStreams 2 as a anamespace | ||||||||||
| 12 | |||||||||||
| 13 | 4 | ▶ | use Carp qw/carp/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | # No constructor | ||||||||||
| 16 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 17 | 1 | carp 'Only use ' . __PACKAGE__ . ' as an extension to Atom'; | |||||||||
| 18 | 1 | return; | |||||||||
| 19 | }; | ||||||||||
| 20 | |||||||||||
| 21 | # Add or get ActivityStreams actor | ||||||||||
| 22 | 9 | ▶ | sub actor { | ||||||||
|
Subroutine
actor: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||
| 23 | 9 | my $self = shift; | |||||||||
| 24 | |||||||||||
| 25 | # Set actor | ||||||||||
| 26 | 9 | ▶ | if ($_[0]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 5 | my $actor = $self->author( @_ ); | |||||||||
| 28 | |||||||||||
| 29 | 5 | $actor->set('object-type', _check_prefix('person')); | |||||||||
| 30 | 5 | return $actor; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | # Get actor | ||||||||||
| 34 | else { | ||||||||||
| 35 | 4 | my $actor = $self->author->[0]; | |||||||||
| 36 | 4 | ▶ | if ($actor) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | |||||||||||
| 38 | 4 | my $object_type = $actor->children('object-type'); | |||||||||
| 39 | |||||||||||
| 40 | 4 | ▶ | return unless $object_type = $object_type->[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | |||||||||||
| 42 | # Prepend namespace if not defined | ||||||||||
| 43 | 4 | ▶ | if (index($object_type->text, '/') == -1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | $object_type->content( | |||||||||
| 45 | __PACKAGE__->_namespace . lc $object_type->text | ||||||||||
| 46 | ); | ||||||||||
| 47 | }; | ||||||||||
| 48 | |||||||||||
| 49 | 4 | return $actor; | |||||||||
| 50 | }; | ||||||||||
| 51 | }; | ||||||||||
| 52 | |||||||||||
| 53 | 0 | return; | |||||||||
| 54 | }; | ||||||||||
| 55 | |||||||||||
| 56 | |||||||||||
| 57 | # Add or get ActivityStreams verb | ||||||||||
| 58 | 10 | ▶ | sub verb { | ||||||||
|
Subroutine
verb: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||
| 59 | 10 | my $self = shift; | |||||||||
| 60 | |||||||||||
| 61 | # Set verb | ||||||||||
| 62 | 10 | ▶ | if ($_[0]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 5 | return $self->set(verb => _check_prefix($_[0])); | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | # Get verb | ||||||||||
| 67 | else { | ||||||||||
| 68 | 5 | my $verb = $self->children('verb'); | |||||||||
| 69 | |||||||||||
| 70 | 5 | ▶ | return unless $verb = $verb->[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | |||||||||||
| 72 | # Prepend namespace if not defined | ||||||||||
| 73 | 5 | ▶ | if (index($verb->text, '/') == -1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 0 | my $nverb = __PACKAGE__->_namespace . lc $verb->text; | |||||||||
| 75 | 0 | $verb->content($nverb); | |||||||||
| 76 | 0 | return $nverb; | |||||||||
| 77 | }; | ||||||||||
| 78 | |||||||||||
| 79 | 5 | return $verb->text; | |||||||||
| 80 | } | ||||||||||
| 81 | }; | ||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | # Add or get ActivityStreams object | ||||||||||
| 85 | 7 | ▶ | sub object { | ||||||||
|
Subroutine
object: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 86 | 7 | shift->_target_object(object => @_ ); | |||||||||
| 87 | }; | ||||||||||
| 88 | |||||||||||
| 89 | |||||||||||
| 90 | # Add or get ActivityStreams target | ||||||||||
| 91 | 3 | ▶ | sub target { | ||||||||
|
Subroutine
target: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 92 | 3 | shift->_target_object(target => @_ ); | |||||||||
| 93 | }; | ||||||||||
| 94 | |||||||||||
| 95 | |||||||||||
| 96 | 10 | ▶ | sub _target_object { | ||||||||
|
Subroutine
_target_object: called CC=7 SCAR=19.7
| |||||||||||
| 97 | 10 | my $self = shift; | |||||||||
| 98 | 10 | my $type = shift; | |||||||||
| 99 | |||||||||||
| 100 | # Add target or object | ||||||||||
| 101 | 10 | ▶ | if ($_[0]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 4 | my %params = @_; | |||||||||
| 103 | |||||||||||
| 104 | 4 | my $obj = $self->set($type); | |||||||||
| 105 | |||||||||||
| 106 | 4 | ▶ | $obj->id( delete $params{id} ) if exists $params{id}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | |||||||||||
| 108 | 4 | ▶ | if (exists $params{'object-type'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | |||||||||||
| 110 | 3 | my $type = delete $params{'object-type'}; | |||||||||
| 111 | |||||||||||
| 112 | 3 | $obj->set('object-type', _check_prefix($type)); | |||||||||
| 113 | }; | ||||||||||
| 114 | |||||||||||
| 115 | 4 | foreach (keys %params) { | |||||||||
| 116 | 4 | $obj->add('-' . $_ => $params{$_}); | |||||||||
| 117 | }; | ||||||||||
| 118 | |||||||||||
| 119 | 4 | return $obj; | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | # Get target or object | ||||||||||
| 123 | else { | ||||||||||
| 124 | 6 | my $obj = $self->at($type); | |||||||||
| 125 | |||||||||||
| 126 | 6 | ▶ | return unless $obj->[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | |||||||||||
| 128 | 6 | my $object_type = $obj->children('object-type')->[0]; | |||||||||
| 129 | |||||||||||
| 130 | # Prepend namespace if not defined | ||||||||||
| 131 | 6 | ▶ | if (index($object_type->text, '/') == -1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 0 | $object_type->content( | |||||||||
| 133 | __PACKAGE__->_namespace . lc($object_type->text) | ||||||||||
| 134 | ); | ||||||||||
| 135 | }; | ||||||||||
| 136 | |||||||||||
| 137 | 6 | return $obj; | |||||||||
| 138 | }; | ||||||||||
| 139 | }; | ||||||||||
| 140 | |||||||||||
| 141 | |||||||||||
| 142 | # Prefix relative object types and verbs with | ||||||||||
| 143 | # ActivityStreams namespace | ||||||||||
| 144 | 13 | ▶ | sub _check_prefix { | ||||||||
|
Subroutine
_check_prefix: called CC=2 SCAR=6.9
| |||||||||||
| 145 | 13 | ▶ | if (index($_[0], '/') == -1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 146 | 10 | return __PACKAGE__->_namespace . lc $_[0]; | |||||||||
| 147 | }; | ||||||||||
| 148 | 3 | return $_[0]; | |||||||||
| 149 | }; | ||||||||||
| 150 | |||||||||||
| 151 | |||||||||||
| 152 | 4 | 1; | |||||||||
| 153 | |||||||||||
| 154 | |||||||||||
| 155 | __END__ | ||||||||||