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