| 1 | package Dancer::Plugin::CORS::Sharing; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use Modern::Perl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | Dancer::Plugin::CORS::Sharing - Helper class for I<sharing> keyword | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 VERSION | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Version 0.01 | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =cut | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 8 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | In order to use many rules with many routes, this helpers class helps you to organize yourself. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | use Dancer::Plugin::CORS; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | sharing->rule( | ||||||||||||||||||||||||||||
| 28 | origin => ..., | ||||||||||||||||||||||||||||
| 29 | credentials => 1 | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | $route = post '/' => sub { ... }; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sharing->add($route); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 METHODS | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head2 new | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | A convient way is to use the implicit form of the module. This means you don't have to call new() self, just start with defining rules and add routes. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | When you want more than one ruleset, obtain a new instance by calling new(): | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | my $sharing = sharing->new; | ||||||||||||||||||||||||||||
| 45 | $sharing->rule(...); | ||||||||||||||||||||||||||||
| 46 | $sharing->add(...); | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub new($%) { | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 0 | my %options = (rules => []); | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | if (blessed $class and $class->isa(__PACKAGE__)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($class) and $class->isa(__PACKAGE__)
MC/DC0%0/2
builtin::blessed($class) and $class->isa(__PACKAGE__)
builtin::blessed($class)
$class->isa(__PACKAGE__)
Truth table0%0/3
A and B
A= builtin::blessed($class)B= $class->isa(__PACKAGE__)
| |||||||||||||||||||||||||||||
| 54 | 0 | %options = (%$class, %options); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 0 | %options = (%options, @_); | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | croak "sharing->new should be called inside a dancer app, not outside" unless exists $options{_add_rule}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | return bless \%options => ref $class || $class; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head2 rule(%options) | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | This method defines a optionset. See L<Dancer::Plugin::CORS::share> for a explaination of valid options. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub rule($%) { | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | my ($self, %options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
rule: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 0 | push @{$self->{rules}} => \%options; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head2 add(@routes) | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | This method finally calls L<Dancer::Plugin::CORS::share> for any route. @routes maybe a list of arrayrefs of L<Dancer::Route> objects or paths. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | Note: L<Dancer::Plugin::CRUD::resource> returns a hash instead of a list. Use values() to obtain the route objects: | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | sharing->add(values(resource(...))); | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub add { | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my ($self, @routes) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 0 | foreach my $routes (@routes) { | |||||||||||||||||||||||||||
| 85 | 0 | ▶ | $routes = [ $routes ] unless ref $routes eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | foreach my $route (@$routes) { | |||||||||||||||||||||||||||
| 87 | 0 | foreach my $options (@{$self->{rules}}) { | |||||||||||||||||||||||||||
| 88 | 0 | $self->{_add_rule}->($route, %$options); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head2 clear | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | This method clears all previously defined rules. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =cut | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub clear { | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | shift->{rules} = []; | ||||||||||||||||||||||||||
|
Subroutine
clear: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | David Zurborg, C<< <zurborg@cpan.org> >> | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =over | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =item L<Dancer::Plugin::CORS> | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =back | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Copyright 2014 David Zurborg, all rights reserved. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | This program is released under the following license: open-source | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 8 | 1; | |||||||||||||||||||||||||||