File Coverage

lib/OAuthomatic/ServerDef.pm
Criterion Covered Total %
statement 24 58 41.3
branch 0 12 0.0
condition 0 3 0.0
subroutine 8 14 57.1
pod 2 2 100.0
total 34 89 38.2


line stmt bran cond sub pod time code
1             package OAuthomatic::ServerDef;
2             # ABSTRACT: Predefined URLs for some services
3              
4 1     1   742 use strict;
  1         2  
  1         35  
5 1     1   4 use warnings;
  1         2  
  1         31  
6 1     1   3 use feature 'state';
  1         1  
  1         71  
7 1     1   438 use namespace::sweep;
  1         19636  
  1         6  
8              
9              
10              
11 1     1   57 use base 'Exporter';
  1         2  
  1         84  
12             our @EXPORT = qw/oauthomatic_predefined_list oauthomatic_predefined_for_name/;
13              
14 1     1   4 use Try::Tiny;
  1         1  
  1         51  
15 1     1   4 use Scalar::Util qw/blessed reftype/;
  1         2  
  1         48  
16             use Module::Pluggable
17 1         7 require => 1, # Just warns, so let's keep it
18             search_path => ["OAuthomatic::ServerDef"],
19 1     1   461 sub_name => '_oauthomatic_predefined_list';
  1         6997  
20              
21              
22             sub _calculate_predefined {
23 0     0     my %predefined;
24 0           foreach my $predef_module (_oauthomatic_predefined_list()) {
25 0 0         unless($predef_module =~ /^OAuthomatic::ServerDef::(.*)$/x) {
26             # This should never happen, but let's just ignore
27 0           warn "OAuthomatic predef: skipping incorrect module $predef_module\n";
28 0           next;
29             }
30 0           my $short_module_name = $1;
31              
32 0           my $predef;
33 0 0         unless($predef_module->can('server')) {
34 0           warn "OAuthomatic predef: bad plugin, module $predef_module does not contain server() function\n";
35 0           next;
36             }
37             try {
38 0     0     $predef = $predef_module->server();
39             } catch {
40 0     0     warn "OAuthomatic predef: bad plugin, function $predef_module" . "::server() failed: $_\n";
41 0           };
42 0 0         next unless $predef;
43 0           my $predef_type = blessed $predef;
44 0 0         unless( $predef_type eq 'OAuthomatic::Server' ) {
45 0   0       $predef_type ||= reftype \$predef;
46 0           warn "OAuthomatic predef: bad plugin, function $predef_module" . "::server() returned invalid value (expected object of type OAuthomatic::Server, got $predef_type)\n";
47 0           next;
48             }
49 0           my $site_name = $predef->site_name;
50 0 0         unless($site_name eq $short_module_name) {
51 0           warn "OAuthomatic predef: bad plugin, $predef_module provided object with bad site_name (got '$site_name', expected '$short_module_name' - matching module name)\n";
52 0           next;
53             }
54 0           $predefined{ $site_name } = $predef;
55             }
56 0           return \%predefined;
57             }
58              
59             sub _predefined {
60 0     0     state $predefined = _calculate_predefined();
61 0           return $predefined;
62             }
63              
64              
65             sub oauthomatic_predefined_list {
66 0     0 1   my $predefined = _predefined();
67 0           return values %$predefined;
68             }
69              
70              
71             sub oauthomatic_predefined_for_name {
72 0     0 1   my $name = shift;
73 0           my $predefined = _predefined();
74 0 0         if( exists $predefined->{$name} ) {
75 0           return $predefined->{$name};
76             }
77             OAuthomatic::Error::Generic->throw(
78 0           ident => "No such predefined server: $name",
79             extra => "Currently known servers: "
80             . join(", ", sort keys %$predefined)
81             . "\n"
82             . "Maybe you should install OAuthomatic::ServerDef::$name?\n",
83             );
84             }
85              
86             1;
87              
88             __END__
89              
90             =pod
91              
92             =encoding UTF-8
93              
94             =head1 NAME
95              
96             OAuthomatic::ServerDef - Predefined URLs for some services
97              
98             =head1 VERSION
99              
100             version 0.01
101              
102             =head1 DESCRIPTION
103              
104             Manages list of definitions of selected OAuth endpoints. This module
105             is mostly used internally, whenever someone writes:
106              
107             OAuthomatic->new(
108             server => 'SomeName',
109             );
110              
111             it is used to look up appropriate definition.
112              
113             Run script L<oauthomatic_predefined_servers> to list all currently
114             known endpoints.
115              
116             To add server to the list, define module named
117             C<OAuthomatic::ServerDef::ServerName>:
118              
119             package OAuthomatic::ServerDef::ServerName;
120             use strict;
121             use warnings;
122             use OAuthomatic::Server;
123              
124             sub server {
125             return OAuthomatic::Server->new(
126             site_name => 'ServerName', # Must match package name
127             oauth_temporary_url => 'https://...',
128             # ... And the rest
129             );
130             }
131             1;
132              
133             =head1 EXPORTS FUNCTIONS
134              
135             =head2 oauthomatic_predefined_list
136              
137             Returns list of all predefined servers (list of L<OAuthomatic::Server> objects).
138              
139             =head2 oauthomatic_predefined_for_name(ServerName)
140              
141             Returns predefined object for given name, or dies.
142              
143             =head1 AUTHOR
144              
145             Marcin Kasperski <Marcin.Kasperski@mekk.waw.pl>
146              
147             =head1 COPYRIGHT AND LICENSE
148              
149             This software is copyright (c) 2015 by Marcin Kasperski.
150              
151             This is free software; you can redistribute it and/or modify it under
152             the same terms as the Perl 5 programming language system itself.
153              
154             =cut