blib/lib/Net/Surveymonkey.pm untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 130 CC 6 SCAR 18.6
CC
6
Cov
0%
call
20.0
_build_key
6.0
_build_token
6.0
CRAP 6.4
1 package Net::Surveymonkey;
2 {
3 $Net::Surveymonkey::VERSION = '0.03';
4 }
5  
6 # $Id: Surveymonkey.pm 5312 2015-01-19 06:05:03Z liosha $
7  
8 # ABSTRACT: Surveymonkey API client
9  
10  
11 use 5.010;
12 use strict;
13 use warnings;
14 use utf8;
15  
16 use Mouse;
17  
18 use Carp;
19 use JSON;
20 use LWP::UserAgent;
21 use Log::Any '$log';
22 use File::Slurp;
23 use List::Util qw/first/;
24  
25  
26 our $SM_API_URL ||= 'https://api.surveymonkey.net/v2';
27 our $SM_API_KEY;
28 our $SM_API_KEY_FILE;
29 our $SM_API_TOKEN;
30 our $SM_API_TOKEN_FILE;
31  
32  
33 has key => (
34 is => 'rw',
35 isa => 'Str',
36 lazy_build => 1,
37 clearer => '_clear_key',
38 );
39  
40  
41 has key_file => (
42 is => 'rw',
43 isa => 'Str',
44 trigger => sub { shift()->_clear_key() } ,
45 );
46  
47 sub _build_key {
48 my ($self) = @_;
49  
50 if ( my $file = $self->key_file || $SM_API_KEY_FILE ) {
51 return $self->_get_key_from_file($file);
52 }
53  
54 return $SM_API_KEY;
55 }
56  
57  
58  
59 has token => (
60 is => 'rw',
61 isa => 'Str',
62 lazy_build => 1,
63 clearer => '_clear_token',
64 );
65  
66  
67  
68 has token_file => (
69 is => 'rw',
70 isa => 'Str',
71 trigger => sub { shift()->_clear_token() } ,
72 );
73  
74  
75 sub _build_token {
76 my ($self) = @_;
77  
78 if ( my $file = $self->token_file || $SM_API_TOKEN_FILE ) {
79 return $self->_get_key_from_file($file);
80 }
81  
82 return $SM_API_TOKEN;
83 }
84  
85 sub _get_key_from_file {
86 my ($self, $file) = @_;
87  
88 # first non-empty not commented string
89 my $str = first {/^[^#]/} map {s/\s+$//r} map {s/^\s+//r} split /\n/x, read_file $file;
90 return $str;
91 }
92  
93  
94  
95  
96  
97  
98  
99  
100 our %METHOD_ALIAS = (
101 create_flow => 'batch/create_flow',
102 get_survey_list => 'surveys/get_survey_list',
103 get_survey_details => 'surveys/get_survey_details',
104 get_collector_list => 'surveys/get_collector_list',
105 create_collector => 'collectors/create_collector',
106 get_respondent_list => 'surveys/get_respondent_list',
107 get_responses => 'surveys/get_responses',
108 get_response_counts => 'surveys/get_response_counts',
109 get_user_details => 'user/get_user_details',
110 get_template_list => 'templates/get_template_list',
111 send_flow => 'batch/send_flow',
112 );
113  
114  
115  
116 sub call
117 {
118 my ($self, $method => $data) = @_;
119  
120 $method = $METHOD_ALIAS{$method} || $method;
121  
122 my $url = "$SM_API_URL/$method?api_key=" . $self->key();
123 my $payload = encode_json $data;
124  
125 my %header = (
126 "Authorization" => "bearer " . $self->token(),
127 "Content-Type" => "application/json",
128 );
129  
130 $log->trace("Call: $method $payload") if $log->is_trace();
131 my $response = LWP::UserAgent->new()->post($url, %header, Content => $payload);
132 my $resp_content = $response->decoded_content();
133 $log->trace("Response: $resp_content") if $log->is_trace();
134  
135 my $result = decode_json $resp_content;
136  
137 croak "SM API call failed: $result->{errmsg}" if $result->{status};
138  
139 return $result->{data};
140 }
141  
142  
143  
144  
145 while (my ($alias, $sm_method) = each %METHOD_ALIAS) {
146 my $sub = sub {
147 my $self = shift;
148 return $self->call( $sm_method => @_);
149 };
150 __PACKAGE__->meta->add_method($alias => $sub);
151 }
152  
153 __PACKAGE__->meta->make_immutable();
154  
155 1;
156  
157 __END__