blib/lib/WWW/FBX/API.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/script/fbx-test.pl ↑ Summary blib/lib/WWW/FBX.pm »
1 package WWW::FBX::API;
2 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Moose();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp::Clan qw/^(?:WWW::FBX|Moose|Class::MOP)/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=2.6
9  
10 Moose::Exporter->setup_import_methods(
11 with_caller => [ qw/api_url base_url fbx_api_method/ ],
12 );
13  
14 my ($_api_url, $_base_url) = ( "", "http://mafreebox.free.fr" );
15  
16 sub api_url { $_api_url = $_[1]; }
17 sub base_url { $_base_url = $_[1]; }
18  
19 sub fbx_api_method {
20 my $caller = shift;
21 my $name = shift;
22 my %options = (
23 @_,
24 );
25  
26 #Remove trailing _
27 $name =~ s/_$//;
28  
29 my $class = Moose::Meta::Class->initialize($caller);
30
31 my ($arg_names, $path) = @options{qw/required path/};
32 $arg_names = $options{params} if @$arg_names == 0 && @{$options{params}} == 1;
33
34 my $code = sub {
35 my $self = shift;
36
37 # copy callers args since we may add ->{source}
38 my $args = ref $_[-1] eq 'HASH' ? { %{pop @_} } : {};
39
40 croak sprintf "$name expected %d args", scalar @$arg_names if @_ > @$arg_names;
41
42 # promote positional args to named args
43 for ( my $i = 0; @_; ++$i ) {
44 my $param = $arg_names->[$i];
45 croak "duplicate param $param: both positional and named"
46 if exists $args->{$param};
47
48 $args->{$param} = shift;
49 }
50
51 # promote boolean parameters
52 for my $boolean_arg ( @{ $options{booleans} } ) {
53 if ( exists $args->{$boolean_arg} ) {
54 next if $args->{$boolean_arg} =~ /^true|false$/;
55 $args->{$boolean_arg} = $args->{$boolean_arg} ? 'true' : 'false';
56 }
57 }
58
59 # replace placeholder arguments
60 my $local_path = $path;
61 $local_path =~ s/:(\w+)/delete $args->{$1} or croak "required arg '$1' missing"/eg;
62 $local_path .= delete $args->{suff} if exists $args->{suff};
63  
64 my $uri = URI->new($_base_url . $_api_url . "/$local_path");
65  
66 return $self->_json_request(
67 $options{method},
68 $uri,
69 $args,
70 $options{content_type}
71 );
72 };
73 #Add method with name and Class::MOP::Method
74 $class->add_method(
75 $name,
76 WWW::FBX::Meta::Method->new(
77 name => $name,
78 package_name => $caller,
79 body => $code,
80 %options,
81 ),
82 );
83  
84 }
85  
86 package WWW::FBX::Meta::Method;
87 use Moose;
88 use Carp::Clan qw/^(?:WWW::FBX|Moose|Class::MOP)/;
89 extends 'Moose::Meta::Method';
90
91 use namespace::autoclean;
92
93 has description => ( isa => 'Str', is => 'ro', required => 1 );
94 has path => ( isa => 'Str', is => 'ro', required => 1 );
95 has method => ( isa => 'Str', is => 'ro', default => 'GET' );
96 has params => ( isa => 'ArrayRef[Str]', is => 'ro', default => sub { [] } );
97 has required => ( isa => 'ArrayRef[Str]', is => 'ro', default => sub { [] } );
98 has returns => ( isa => 'Str', is => 'ro', predicate => 'has_returns' );
99 has booleans => ( isa => 'ArrayRef[Str]', is => 'ro', default => sub { [] } );
100 has content_type => ( isa => 'Str', is => 'ro', default => '' );
101 has suff => ( isa => 'Str', is => 'ro', default => '' );
102
103 #Build hash where keys are attribute names
104 my %valid_attribute_names = map { $_->init_arg => 1 }
105 __PACKAGE__->meta->get_all_attributes;
106
107 sub new {
108 my $class = shift;
109 my %args = @_;
110  
111 #Stack arguments that are not expected attributes
112 my @invalid_attributes = grep { !$valid_attribute_names{$_} } keys %args;
113 croak "unexpected argument(s): @invalid_attributes" if @invalid_attributes;
114
115 #Create method
116 $class->SUPER::wrap(@_);
117 }
118  
119 1;
120 __END__
121  
122 =encoding utf-8
123  
124 =head1 NAME
125  
126 WWW::FBX::API - Freebox API sugar
127  
128 =head1 SYNOPSIS
129  
130 use WWW::FBX::API;
131  
132 =head1 DESCRIPTION
133  
134 WWW::FBX::API is API sugar
135  
136 =head1 LICENSE
137  
138 Copyright (C) Laurent Kislaire.
139  
140 This library is free software; you can redistribute it and/or modify
141 it under the same terms as Perl itself.
142  
143 =head1 AUTHOR
144  
145 Laurent Kislaire E<lt>teebeenator@gmail.comE<gt>
146  
147 =cut
148  
« blib/script/fbx-test.pl ↑ Summary blib/lib/WWW/FBX.pm »