SmartSheet.pm

filter: Statementstmt 34.2% 24 / 70 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 6 / 18 Podpod 66.6% 6 / 9 totaltotal 34.9% 36 / 103 CC 4 SCAR 10.5
CC
4
Cov
32%
_post
6.0
_get
6.0
_delete
6.0
CRAP 2.9
↑ Summary SmartSheet/Sheet.pm »
1 package WWW::SmartSheet;
2 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use MooX::late;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '0.01';
6  
7 # Example error message, (status_line and content):
8 # 400 Bad Request{"errorCode":5026,"message":"Your account has reached the maximum number of sheets allowed for your trial. To save additional sheets, you must upgrade to a paid plan."}
9  
10 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Data::Dumper qw(Dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use JSON qw(from_json to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 has token => (is => 'ro', required => 1);
16  
17 2 has sheets => (is => 'rw', isa => 'ArrayRef');
18  
19 2 my $API_URL = "https://api.smartsheet.com/1.1";
20 2 my @ACCESS_LEVELS = qw(VIEWER EDITOR EDITOR_SHARE ADMIN);
21  
22 sub ua {
23 0 ▶ my ($self) = @_;
Subroutine ua: not called CC=1 SCAR=6.9
Pod undocumented
24  
25 0 my $ua = LWP::UserAgent->new( agent => "WWW::SmartSheet v$VERSION https://github.com/szabgab/WWW-SmartSheet" );
26 0 $ua->timeout(10);
27 0 $ua->default_header("Authorization" => "Bearer " . $self->token);
28 0 $ua->default_header("Content-Type" => "application/json");
29 0 return $ua;
30 }
31  
32  
33 sub get_sheets {
34 0 ▶ my ($self) = @_;
Subroutine get_sheets: not called CC=1 SCAR=6.9
Pod documented
35  
36 0 my $all_sheets = $self->_get('sheets');
37 0 $self->sheets($all_sheets);
38 0 return $all_sheets;
39 }
40  
41  
42  
43 # TODO how can I make sure that get_sheet is called if sheets is empty ? is that the lazy attribute?
44 sub get_columns {
45 0 ▶ my ($self, $sheet_number) = @_;
Subroutine get_columns: not called CC=1 SCAR=6.9
Pod documented
46  
47 0 my $sheets = $self->sheets;
48 0 $self->_get("sheet/$sheets->[$sheet_number]{id}/columns");
49 }
50  
51  
52 sub share_sheet {
53 0 ▶ my ($self, $sheet_id, $email, $access_level) = @_;
Subroutine share_sheet: not called CC=1 SCAR=6.9
Pod documented
54  
55 0 $self->_post("sheet/$sheet_id/shares?sendEmail=true", {email => $email, accessLevel => $access_level});
56 }
57  
58  
59 sub create_sheet {
60 0 ▶ my ($self, %args) = @_;
Subroutine create_sheet: not called CC=1 SCAR=6.9
Pod documented
61  
62 0 return $self->_post('sheets', \%args);
63 }
64  
65 sub delete_sheet {
66 0 ▶ my ($self, $id) = @_;
Subroutine delete_sheet: not called CC=1 SCAR=6.9
Pod undocumented
67 0 $self->_delete("sheet/$id");
68 }
69  
70  
71 sub add_column {
72 0 ▶ my ($self, $sheet_id, $column) = @_;
Subroutine add_column: not called CC=1 SCAR=6.9
Pod documented
73  
74 0 return $self->_post("sheet/$sheet_id/columns", $column );
75 }
76  
77  
78 sub insert_rows {
79 0 ▶ my ($self, $sheet_id, $rows, %args) = @_;
Subroutine insert_rows: not called CC=1 SCAR=6.9
Pod documented
80  
81 0 my $res = $self->ua->get("$API_URL/sheet/$sheet_id/columns");
82  
83 #_post("sheet/$sheet_id/rows", $rows)
84 }
85  
86 sub get_sheet_by_id {
87 0 ▶ my ($self, $id) = @_;
Subroutine get_sheet_by_id: not called CC=1 SCAR=6.9
Pod undocumented
88 0 my $data = $self->_get("sheet/$id");
89 0 require WWW::SmartSheet::Sheet;
90 0 WWW::SmartSheet::Sheet->new($data);
91 }
92  
93 sub _post {
94 0 ▶ my ($self, $path, $data) = @_;
Subroutine _post: not called CC=2 SCAR=17.9
95  
96 0 my $url = "$API_URL/$path";
97 0 my $ua = $self->ua;
98 0 my $json = to_json($data);
99 #warn $json;
100 #warn Dumper $ua->default_headers;
101  
102 0 my $req = HTTP::Request->new( 'POST', $url );
103 0 $req->content( $json );
104 0 my $res = $ua->request( $req );
105 #my $res = $ua->post($url, Content => $json);
106  
107 0 ▶ Carp::croak $res->status_line . $res->content if not $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
108 0 return from_json $res->decoded_content;
109 }
110  
111 sub _get {
112 0 ▶ my ($self, $path) = @_;
Subroutine _get: not called CC=2 SCAR=17.9
113  
114 0 my $url = "$API_URL/$path";
115 0 my $res = $self->ua->get($url);
116 0 ▶ Carp::croak $res->status_line . $res->content if not $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
117 0 return from_json $res->decoded_content;
118 }
119  
120 sub _delete {
121 0 ▶ my ($self, $path) = @_;
Subroutine _delete: not called CC=2 SCAR=17.9
122  
123 0 my $url = "$API_URL/$path";
124 0 my $res = $self->ua->delete($url);
125 0 ▶ Carp::croak $res->status_line . $res->content if not $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
126 0 return from_json $res->decoded_content;
127 }
128  
129  
130  
131  
132  
133 2 1;
134  
135 __END__
136  
137 =pod
138  
139 =head1 NAME
140  
141 WWW::SmartSheet
142  
143 =head1 VERSION
144  
145 version 0.01
146  
147 =head2 get_sheets
148  
149 =head2 get_columns
150  
151 Given the number of the sheet, returns an array of the column definitions each column is a hash:
152 title
153 type (TEXT_NUMBER, PICKLIST, ... )
154 index
155 id
156 primary ??
157 options (for PICKLIST)
158  
159 Probably it should be the name of the sheet.
160  
161 =head2 share_sheet
162  
163 sheet_id
164 email => 'foo@examples.com',
165 access_level => one of the following strings: VIEWER EDITOR EDITOR_SHARE ADMIN
166  
167 =head2 create_sheet
168  
169 $w->create_sheet(
170 name => 'Name of the sheet',
171 columns => [
172  
173 { title => "Baked Good", type => 'TEXT_NUMBER', primary => 1 },
174 { title => "Baker", type => 'CONTACT_LIST' },
175 { title => 'Price Per Item', type => 'TEXT_NUMBER' },
176 { title => "Gluten Free?", "type":"CHECKBOX", "symbol":"FLAG"},
177 { title => 'Status', type => 'PICKLIST', options => ['Started', 'Finished' , 'Delivered'] }
178 ]);
179  
180 =head2 add_column
181  
182 $w->add_column($sheet_id, { title => 'Delivered', type => 'DATE', index => 5})
183  
184 =head2 insert_rows
185  
186 $w->insert_rows($sheet_id,
187 {
188 toTop => JSON::true,
189 rows => [ {
190 cells => [
191 {"columnId":column_info[0]['id'], "value":"Brownies"},
192 {"columnId":column_info[1]['id'], "value":"julieanne@smartsheet.com","strict": False},
193 {"columnId":column_info[2]['id'], "value":"$1", "strict":False},
194 {"columnId":column_info[3]['id'], "value":True},
195 {"columnId":column_info[4]['id'], "value":"Finished"},
196 {"columnId":column_info[5]['id'], "value": "None", "strict":False}]
197 },
198  
199 =head1 OTHER
200  
201 The code of this client is free software.
202 Access to the services of L<Smartsheet|http://www.smartsheet.com/> requires registration and payment.
203  
204 L<API Documentation|http://www.smartsheet.com/developers/api-documentation>
205  
206 =head1 AUTHOR
207  
208 Gabor Szabo <szabgab@cpan.org>
209  
210 =head1 COPYRIGHT AND LICENSE
211  
212 This software is copyright (c) 2013 by Gabor Szabo.
213  
214 This is free software; you can redistribute it and/or modify it under
215 the same terms as the Perl 5 programming language system itself.
216  
217 =cut
↑ Summary SmartSheet/Sheet.pm »