| 1 | package WWW::SmartSheet; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | our $VERSION = '0.06'; | |||||||||
| 4 | |||||||||||
| 5 | 2 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use MooX::late; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use Carp (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use Data::Dumper qw(Dumper); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use JSON qw(from_json to_json); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 2 | has token => (is => 'ro', required => 1); | |||||||||
| 14 | |||||||||||
| 15 | 2 | has sheets => (is => 'rw', isa => 'ArrayRef'); | |||||||||
| 16 | |||||||||||
| 17 | 2 | my $API_URL = "https://api.smartsheet.com/2.0"; | |||||||||
| 18 | 2 | my @ACCESS_LEVELS = qw(VIEWER EDITOR EDITOR_SHARE ADMIN); | |||||||||
| 19 | |||||||||||
| 20 | 0 | ▶ | sub ua { | ||||||||
|
Subroutine
ua: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 21 | 0 | my ($self) = @_; | |||||||||
| 22 | |||||||||||
| 23 | 0 | my $ua = LWP::UserAgent->new( agent => "WWW::SmartSheet v$VERSION https://github.com/szabgab/WWW-SmartSheet" ); | |||||||||
| 24 | 0 | $ua->timeout(10); | |||||||||
| 25 | 0 | $ua->default_header("Authorization" => "Bearer " . $self->token); | |||||||||
| 26 | 0 | $ua->default_header("Content-Type" => "application/json"); | |||||||||
| 27 | 0 | return $ua; | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | 0 | ▶ | sub get_current_user { | ||||||||
|
Subroutine
get_current_user: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 31 | 0 | my ($self) = @_; | |||||||||
| 32 | |||||||||||
| 33 | 0 | my $current_user = $self->_get('users/me'); | |||||||||
| 34 | 0 | return $current_user; | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 0 | ▶ | sub get_sheets { | ||||||||
|
Subroutine
get_sheets: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 38 | 0 | my ($self, $pagesize, $page) = @_; | |||||||||
| 39 | |||||||||||
| 40 | 0 | my $all_sheets = $self->_get("sheets", | |||||||||
| 41 | ("pageSize" => $pagesize, | ||||||||||
| 42 | "page" => $page, | ||||||||||
| 43 | ) | ||||||||||
| 44 | ); | ||||||||||
| 45 | 0 | return $all_sheets; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 0 | ▶ | sub get_columns { | ||||||||
|
Subroutine
get_columns: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 49 | 0 | my ($self, $sheetid, $pagesize, $page) = @_; | |||||||||
| 50 | |||||||||||
| 51 | 0 | my $cols = $self->_get("sheets/$sheetid/columns", | |||||||||
| 52 | ("pageSize" => $pagesize, | ||||||||||
| 53 | "page" => $page, | ||||||||||
| 54 | ) | ||||||||||
| 55 | ); | ||||||||||
| 56 | 0 | return $cols; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 0 | ▶ | sub share_sheet { | ||||||||
|
Subroutine
share_sheet: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 60 | 0 | my ($self, $sheetid, $email, $access_level) = @_; | |||||||||
| 61 | |||||||||||
| 62 | 0 | my $result = $self->_post("sheets/$sheetid/shares?sendEmail=true", {email => $email, accessLevel => $access_level}); | |||||||||
| 63 | 0 | return $result; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 0 | ▶ | sub create_sheet { | ||||||||
|
Subroutine
create_sheet: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 67 | 0 | my ($self, %args) = @_; | |||||||||
| 68 | |||||||||||
| 69 | 0 | return $self->_post('sheets', \%args); | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | sub delete_sheet { | ||||||||
|
Subroutine
delete_sheet: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 73 | 0 | my ($self, $id) = @_; | |||||||||
| 74 | |||||||||||
| 75 | 0 | $self->_delete("sheets/$id"); | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 0 | ▶ | sub add_column { | ||||||||
|
Subroutine
add_column: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 79 | 0 | my ($self, $sheet_id, $column) = @_; | |||||||||
| 80 | |||||||||||
| 81 | 0 | return $self->_post("sheets/$sheet_id/columns", $column ); | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 0 | ▶ | sub insert_rows { | ||||||||
|
Subroutine
insert_rows: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 85 | 0 | my ($self, $sheet_id, $loc, @rows) = @_; | |||||||||
| 86 | |||||||||||
| 87 | 0 | my @full_rows; | |||||||||
| 88 | 0 | foreach my $row (@rows) { | |||||||||
| 89 | 0 | my $lv = 1; | |||||||||
| 90 | |||||||||||
| 91 | 0 | my %r; | |||||||||
| 92 | 0 | $r{"cells"} = $row; | |||||||||
| 93 | |||||||||||
| 94 | 0 | ▶ | if ($loc =~ m/(=)/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | |||||||||||
| 96 | 0 | my $l = $loc; | |||||||||
| 97 | 0 | ▶ | if ($l =~ m/,above/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 0 | $l =~ s/,above//; | |||||||||
| 99 | 0 | $r{"above"} = 1; | |||||||||
| 100 | } | ||||||||||
| 101 | 0 | my ($k, $v) = split(/=/, $l); | |||||||||
| 102 | 0 | ▶ | if ($v) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | $r{$k}=$v; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | } else { | ||||||||||
| 107 | |||||||||||
| 108 | 0 | $r{"$loc"} = 1; | |||||||||
| 109 | |||||||||||
| 110 | } | ||||||||||
| 111 | 0 | push (@full_rows, \%r); | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 0 | return $self->_post("sheets/$sheet_id/rows", \@full_rows); | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 0 | ▶ | sub get_sheet_by_id { | ||||||||
|
Subroutine
get_sheet_by_id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 118 | 0 | my ($self, $id, $pagesize, $page) = @_; | |||||||||
| 119 | |||||||||||
| 120 | 0 | my $sheet = $self->_get("sheets/$id", | |||||||||
| 121 | ("pageSize" => $pagesize, | ||||||||||
| 122 | "page" => $page, | ||||||||||
| 123 | ) | ||||||||||
| 124 | ); | ||||||||||
| 125 | 0 | return $sheet; | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | 0 | ▶ | sub _post { | ||||||||
|
Subroutine
_post: not called CC=2 SCAR=17.9
| |||||||||||
| 129 | 0 | my ($self, $path, $data) = @_; | |||||||||
| 130 | |||||||||||
| 131 | 0 | my $url = "$API_URL/$path"; | |||||||||
| 132 | 0 | my $ua = $self->ua; | |||||||||
| 133 | 0 | my $json = to_json($data); | |||||||||
| 134 | |||||||||||
| 135 | 0 | my $req = HTTP::Request->new( 'POST', $url ); | |||||||||
| 136 | 0 | $req->content( $json ); | |||||||||
| 137 | 0 | my $res = $ua->request( $req ); | |||||||||
| 138 | |||||||||||
| 139 | 0 | ▶ | Carp::croak $res->status_line . $res->content if not $res->is_success; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | 0 | return from_json $res->decoded_content; | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | 0 | ▶ | sub _get { | ||||||||
|
Subroutine
_get: not called CC=5 SCAR=34.0
| |||||||||||
| 144 | 0 | my ($self, $path, %params) = @_; | |||||||||
| 145 | |||||||||||
| 146 | 0 | my $paramstr; | |||||||||
| 147 | 0 | my $url = "$API_URL/$path"; | |||||||||
| 148 | |||||||||||
| 149 | 0 | foreach my $param (keys %params) { | |||||||||
| 150 | |||||||||||
| 151 | 0 | ▶ | if (!$params{$param}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 152 | # ignore empty params | ||||||||||
| 153 | 0 | next; | |||||||||
| 154 | } | ||||||||||
| 155 | |||||||||||
| 156 | 0 | $paramstr .= $param . "=" . $params{$param} . "&"; | |||||||||
| 157 | } | ||||||||||
| 158 | |||||||||||
| 159 | 0 | ▶ | if ($paramstr) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 160 | 0 | $paramstr =~ s/&$//; | |||||||||
| 161 | 0 | $url .= "?$paramstr"; | |||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | 0 | my $res = $self->ua->get($url); | |||||||||
| 165 | 0 | ▶ | Carp::croak $res->status_line . $res->content if not $res->is_success; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 166 | 0 | return from_json $res->decoded_content; | |||||||||
| 167 | } | ||||||||||
| 168 | |||||||||||
| 169 | 0 | ▶ | sub _delete { | ||||||||
|
Subroutine
_delete: not called CC=2 SCAR=17.9
| |||||||||||
| 170 | 0 | my ($self, $path) = @_; | |||||||||
| 171 | |||||||||||
| 172 | 0 | my $url = "$API_URL/$path"; | |||||||||
| 173 | 0 | my $res = $self->ua->delete($url); | |||||||||
| 174 | 0 | ▶ | Carp::croak $res->status_line . $res->content if not $res->is_success; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 175 | 0 | return from_json $res->decoded_content; | |||||||||
| 176 | } | ||||||||||
| 177 | |||||||||||
| 178 | 2 | 1; | |||||||||
| 179 | |||||||||||
| 180 | __END__ | ||||||||||