| 1 | package Net::Vimeo; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Clone qw( clone ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use JSON qw( decode_json ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Net::OAuth; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | with 'Net::Vimeo::OAuth'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | our $VERSION = '0.000004'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 17 | 0 | my $orig = shift; | |||||||||||||||||||||||||||
| 18 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | my $args = $class->$orig(@_); | |||||||||||||||||||||||||||
| 21 | 0 | my $api_urls = { | |||||||||||||||||||||||||||
| 22 | api_url => "http://vimeo.com/api/rest/v2", | ||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | return { %$api_urls, %$args }; | |||||||||||||||||||||||||||
| 26 | 1 | }; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | has 'api_url' => ( | ||||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||
| 31 | isa => 'Str', | ||||||||||||||||||||||||||||
| 32 | required => 1, | ||||||||||||||||||||||||||||
| 33 | 0 | ▶ | reader => { api_url => sub { URI->new(shift->{api_url}) } }, | ||||||||||||||||||||||||||
|
Subroutine
api_url: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 34 | 1 | ); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub make_api_request { | ||||||||||||||||||||||||||
|
Subroutine
make_api_request: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 0 | my ( $self, $method, $params ) = @_; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | croak 'You need to authorize your app before making api requests' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->access_token and $self->access_token_secret
MC/DC0%0/2
$self->access_token and $self->access_token_secret
$self->access_token
$self->access_token_secret
Truth table0%0/3
A and B
A= $self->access_tokenB= $self->access_token_secret
| |||||||||||||||||||||||||||||
| 40 | unless ($self->access_token && $self->access_token_secret ); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # API request expects a method ... just check if the method argument is | ||||||||||||||||||||||||||||
| 43 | # defined | ||||||||||||||||||||||||||||
| 44 | carp "API method must be present in arguments( eq: method => 'vimeo.activity.userDid')" && return | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | unless ( $params->{method} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | my $api_req = $self->_api_request($method || 'GET', $params, 'protected resource'); | ||||||||||||||||||||||||||
|
Condition0%0/2
$method || 'GET'
MC/DC0%0/1
$method || 'GET'
$method
Truth table0%0/2
A
A= $method
| |||||||||||||||||||||||||||||
| 48 | 0 | my $res = $self->user_agent->send_request($api_req); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | return $res; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub _api_request { | ||||||||||||||||||||||||||
|
Subroutine
_api_request: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 54 | 0 | my ( $self, $method, $params, $type ) = @_; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | my $http_request; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if ( $method eq 'GET' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 59 | # Build url for request | ||||||||||||||||||||||||||||
| 60 | 0 | my $uri = $self->get_uri_for_request($params); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | $http_request = HTTP::Request->new($method, $uri); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | elsif ( $method eq 'POST' ) { | ||||||||||||||||||||||||||||
| 65 | 0 | $http_request = POST($self->api_url, Content => $self->_query_string_for($params)); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | else { | ||||||||||||||||||||||||||||
| 68 | 0 | croak sprintf( "Unknown HTTP method %s", $method ); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | $self->apply_headers( $http_request, $type, $params ); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | return $http_request; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub apply_headers { | ||||||||||||||||||||||||||
|
Subroutine
apply_headers: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 0 | my ( $self, $req, $type, $params ) = @_; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | return | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | unless ( $self->access_token && $self->access_token_secret ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->access_token and $self->access_token_secret
MC/DC0%0/2
$self->access_token and $self->access_token_secret
$self->access_token
$self->access_token_secret
Truth table0%0/3
A and B
A= $self->access_tokenB= $self->access_token_secret
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | local $Net::OAuth::SKIP_UTF8_DOUBLE_ENCODE_CHECK = 1; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | my $uri = $req->uri->clone; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | my $request = $self->make_oauth_request( | |||||||||||||||||||||||||||
| 87 | $type, | ||||||||||||||||||||||||||||
| 88 | request_url => $uri, | ||||||||||||||||||||||||||||
| 89 | request_method => $req->method, | ||||||||||||||||||||||||||||
| 90 | token => $self->access_token, | ||||||||||||||||||||||||||||
| 91 | token_secret => $self->access_token_secret, | ||||||||||||||||||||||||||||
| 92 | %$params, | ||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | $req->header(authorization => $request->to_authorization_header); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub _encode_args { | ||||||||||||||||||||||||||
|
Subroutine
_encode_args: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 99 | 0 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | return { map { utf8::upgrade($_) unless ref($_); $_ } %$args }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | sub _query_string_for { | ||||||||||||||||||||||||||
|
Subroutine
_query_string_for: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 105 | 0 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | my @pairs; | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | while ( my ($k, $v) = each %$args ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | push @pairs, join '=', map URI::Escape::uri_escape_utf8($_,'^\w.~-'), $k, $v; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | return join '&', @pairs; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub get_uri_for_request { | ||||||||||||||||||||||||||
|
Subroutine
get_uri_for_request: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 116 | 0 | my ( $self, $params ) = @_; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | my $cloned_params = clone $params; | |||||||||||||||||||||||||||
| 119 | 0 | $self->_encode_args($cloned_params); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | my $uri = $self->api_url; | |||||||||||||||||||||||||||
| 122 | 0 | $uri->query($self->_query_string_for($cloned_params)); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | return $uri; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub return_content { | ||||||||||||||||||||||||||
|
Subroutine
return_content: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 128 | 0 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | croak "No request for content retrieving" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | unless $req; | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | return decode_json( $req->content ); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 1 | 1; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head1 NAME | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Net::Vimeo - Make requests via OAuth to Vimeo Advanced API | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head1 VERSION | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Version 0.000004 | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | use Net::Vimeo; | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | my $vimeo = Net::Vimeo->new( consumer_key => 'xxxx', consumer_secret => 'yyyy' ); | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # First you need to get the authorization URL | ||||||||||||||||||||||||||||
| 155 | # If you need permission to upload a video, send the | ||||||||||||||||||||||||||||
| 156 | # permission parameter, otherwise you will only have read permission | ||||||||||||||||||||||||||||
| 157 | my $vimeo_oauth_url = $vimeo_oauth->get_authorization_url( permission => 'write' ); | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # Get the oauth_verifier from that url and | ||||||||||||||||||||||||||||
| 160 | # exchange the request tokens with access tokens | ||||||||||||||||||||||||||||
| 161 | $vimeo->get_access_token( { verifier => 'oauth_verifier' } ); | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # Now you are authorized....you can start playing | ||||||||||||||||||||||||||||
| 164 | my $request_params = { | ||||||||||||||||||||||||||||
| 165 | method => 'vimeo.activity.userDid', | ||||||||||||||||||||||||||||
| 166 | user_id => 'someuserid', | ||||||||||||||||||||||||||||
| 167 | page => 1, | ||||||||||||||||||||||||||||
| 168 | per_page => 10, | ||||||||||||||||||||||||||||
| 169 | format => 'json', | ||||||||||||||||||||||||||||
| 170 | }; | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | my $result = $vimeo->make_api_request( 'GET', $request_params); | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | The canonical documentation for the Advanced API is at L<http://developer.vimeo.com/apis/advanced> | ||||||||||||||||||||||||||||
| 175 | with the method listing residing at L<http://developer.vimeo.com/apis/advanced/methods> | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | The distribution has a simple example bundled, and make sure to study the tests and | ||||||||||||||||||||||||||||
| 178 | L<Net::Vimeo::OAuth> for further insight into the overall process of authentication. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | my $vimeo = Net::Vimeo->new( | ||||||||||||||||||||||||||||
| 183 | consumer_key => 'xxxx', | ||||||||||||||||||||||||||||
| 184 | consumer_secret => 'yyyy', | ||||||||||||||||||||||||||||
| 185 | access_token => 'zzzz', # optional | ||||||||||||||||||||||||||||
| 186 | access_token_secret => 'zzzz_secret' # optional | ||||||||||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | On construction, consumer_key and consumer_secret are mandatory to identify your app | ||||||||||||||||||||||||||||
| 190 | against Vimeo. In case you want your app to be "statically" provided with an access_token, | ||||||||||||||||||||||||||||
| 191 | you may set one on construction, but you'll commonly use the methods around get_access_token() | ||||||||||||||||||||||||||||
| 192 | to ask a user for access so your app can act on behalf of granting users. See L<Net::Vimeo::OAuth> | ||||||||||||||||||||||||||||
| 193 | for the underlying mechanism. | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =head1 METHODS | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =over 4 | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =item get_authorization_url( permission => $scope ); | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | Returns a Vimeo URL (L<URI>-Object) which you will use to send a user to a | ||||||||||||||||||||||||||||
| 202 | page (on Vimeo) where she may accept or decline that your app receives | ||||||||||||||||||||||||||||
| 203 | access to her account. Acceptance means the user issues an access_token | ||||||||||||||||||||||||||||
| 204 | for your app, an alphanumeric string. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =item make_api_request($method, $request_params) | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | After you have your access tokens via OAuth you can start | ||||||||||||||||||||||||||||
| 209 | making requests to Vimeo Advanced API. Arguments necessary to make the call | ||||||||||||||||||||||||||||
| 210 | to Vimeo Advanced API are: C<$method> which is a string representing the HTTP | ||||||||||||||||||||||||||||
| 211 | method needed for your request. The second one is the api request params, you can | ||||||||||||||||||||||||||||
| 212 | find the needed params on Vimeo Advanced API documentation. | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | $vimeo->make_api_request( 'GET', $request_params); | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =back | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | Mirela Iclodean, C<< <imirela at cpan.org> >> | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | L<Net::Vimeo::OAuth> | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head1 BUGS | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Please report any bugs or feature requests to C<bug-net-vimeo at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 229 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Vimeo>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 230 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | perldoc Net::Vimeo | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | You can also look for information at: | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =over 4 | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Vimeo> | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | L<http://annocpan.org/dist/Net-Vimeo> | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | L<http://cpanratings.perl.org/d/Net-Vimeo> | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | L<http://search.cpan.org/dist/Net-Vimeo/> | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =back | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | Copyright 2014 Mirela Iclodean. | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 267 | under the terms of the the Artistic License (2.0). | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||