blib/lib/Net/Twitch/Oauth2.pm

filter: Statementstmt 63.3% 71 / 112 Branchbran 35.0% 14 / 40 Conditioncond 25.0% 4 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 78.9% 15 / 19 Podpod 0.0% 0 / 8 totaltotal 50.7% 104 / 205 CC 27 SCAR 27.4
CC
27
Cov
53%
post_access_token
56.0
get_authorization_url
30.0
post
12.0
CRAP 15.4
↑ Summary
1 package Net::Twitch::Oauth2;
2
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 2 ▶use constant ACCESS_TOKEN_URL => 'https://api.twitch.tv/kraken/oauth2/token';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use constant AUTHORIZE_URL => 'https://api.twitch.tv/kraken/oauth2/authorize';
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 2 our $VERSION = '0.06';
15
16 6 ▶sub new {
Subroutine new: called CC=7 SCAR=19.6
Pod undocumented
17 6 my ($class,%options) = @_;
18 6 my $self = {};
19 6 $self->{options} = \%options;
20
21 6 ▶ if (!$options{access_token}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"access_token"}) 3 3 6
22 3 ▶ croak "You must provide your application id when construct new method\n EPI::Twitch::Oauth2->new( application_id => '...' )" unless defined $self->{options}->{application_id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"application_id"} 1 2 3
23 2 ▶ croak "You must provide your application secret when construct new method\n EPI::Twitch::Oauth2->new( application_secret => '...' )" unless defined $self->{options}->{application_secret};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"application_secret"} 1 1 2
24 }
25
26 4 ▶ $self->{browser} = $options{browser} || LWP::UserAgent->new;
Condition66%2/3
$options{'browser'} || 'LWP::UserAgent'->new
l!l&&r!l&&!r
130
MC/DC0%0/2
$options{'browser'} || 'LWP::UserAgent'->new
$options{'browser'} 'LWP::UserAgent'->new
Truth table66%2/3
A || B
A= $options{'browser'}B= 'LWP::UserAgent'->new
ABresult
1X1
011
000
27 4 ▶ $self->{access_token_url} = $options{access_token_url} || ACCESS_TOKEN_URL;
Condition50%1/2
$options{'access_token_url'} || 'https://api.twitch.tv/kraken/oauth2/token'
l!l
04
MC/DC0%0/1
$options{'access_token_url'} || 'https://api.twitch.tv/kraken/oauth2/token'
$options{'access_token_url'}
Truth table50%1/2
A
A= $options{'access_token_url'}
Aresult
11
00
28 4 ▶ $self->{authorize_url} = $options{authorize_url} || AUTHORIZE_URL;
Condition50%1/2
$options{'authorize_url'} || 'https://api.twitch.tv/kraken/oauth2/authorize'
l!l
04
MC/DC0%0/1
$options{'authorize_url'} || 'https://api.twitch.tv/kraken/oauth2/authorize'
$options{'authorize_url'}
Truth table50%1/2
A
A= $options{'authorize_url'}
Aresult
11
00
29 4 $self->{access_token} = $options{access_token};
30
31 4 return bless($self, $class);
32 }
33
34 0 ▶sub get_authorization_url {
Subroutine get_authorization_url: not called CC=5 SCAR=34.0
Pod undocumented
35 0 my ($self,%params) = @_;
36
37 0 ▶ $params{callback} ||= $self->{options}->{callback};
Condition0%0/3
$params{"callback"} ||= $self->{"options"}{"callback"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{"callback"} ||= $self->{"options"}{"callback"}
$params{"callback"} $self->{"options"}{"callback"}
Truth table0%0/3
A ||= B
A= $params{"callback"}B= $self->{"options"}{"callback"}
ABresult
1X1
011
000
38 0 ▶ croak "You must pass a callback parameter with Oauth v2.0" unless defined $params{callback};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"callback"} 0 0 0
39
40 0 ▶ my $scope = join(",", @{$params{scope}}) if defined($params{scope});
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"scope"} 0 0 0
41
42 my $url = $self->{authorize_url}
43 .'?response_type=code'
44 .'&client_id='
45 .uri_escape($self->{options}->{application_id})
46 .'&redirect_uri='
47 0 .uri_escape($params{callback});
48
49 0 ▶ $url .= "&scope=$scope" if $scope;
Branch: 1 branch
BranchTrueFalseTotal
if $scope 0 0 0
50
51 0 return $url;
52 }
53
54 0 ▶sub post_access_token {
Subroutine post_access_token: not called CC=7 SCAR=40.3
Pod undocumented
55 0 my ($self,%params) = @_;
56 0 ▶ $params{callback} ||= $self->{options}->{callback};
Condition0%0/3
$params{"callback"} ||= $self->{"options"}{"callback"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{"callback"} ||= $self->{"options"}{"callback"}
$params{"callback"} $self->{"options"}{"callback"}
Truth table0%0/3
A ||= B
A= $params{"callback"}B= $self->{"options"}{"callback"}
ABresult
1X1
011
000
57 0 ▶ $params{code} ||= $self->{options}->{code};
Condition0%0/3
$params{"code"} ||= $self->{"options"}{"code"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{"code"} ||= $self->{"options"}{"code"}
$params{"code"} $self->{"options"}{"code"}
Truth table0%0/3
A ||= B
A= $params{"code"}B= $self->{"options"}{"code"}
ABresult
1X1
011
000
58
59 0 ▶ croak "You must pass a code parameter with Oauth v2.0" unless defined $params{code};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"code"} 0 0 0
60 0 ▶ croak "You must pass callback URL" unless defined $params{callback};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"callback"} 0 0 0
61 0 $self->{options}->{code} = $params{code};
62
63 ###generating access token URL
64 my $getURL = $self->{access_token_url}
65 .'?client_id='
66 .uri_escape($self->{options}->{application_id})
67 .'&client_secret='
68 .uri_escape($self->{options}->{application_secret})
69 .'&grant_type=authorization_code'
70 .'&redirect_uri='
71 .uri_escape($params{callback})
72 .'&code='
73 0 .uri_escape($params{code});
74
75 0 my $response = $self->{browser}->post($getURL);
76
77 ##got an error response from twitch
78 ##die and display error message
79 0 my $j = JSON::Any->new;
80 0 ▶ if (!$response->is_success){
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
81 0 my $error = $j->jsonToObj($response->content());
82 0 croak "'" .$error->{error}->{type}. "'" . " " .$error->{error}->{message};
83 }
84
85 ##everything is ok proccess response and extract access token
86 0 my $reply = $j->jsonToObj($response->content());
87 0 my $token = $reply->{access_token};
88 0 my $expires = $reply->{refresh_token};
89
90 ###save access token
91 0 ▶ if ($token){
Branch: 1 branch
BranchTrueFalseTotal
if ($token) 0 0 0
92 0 $self->{access_token} = $token;
93 0 return $token;
94 }
95
96 0 croak "can't get access token";
97 }
98
99 2 ▶sub get {
Subroutine get: called CC=6 SCAR=19.2
Pod undocumented
100 2 my ($self,$url,$params) = @_;
101 2 ▶ unless ($self->_has_access_token($url)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_access_token($url)) 2 0 2
102 2 ▶ croak "You must pass access_token" unless defined $self->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"access_token"} 0 2 2
103 2 ▶ $url .= $self->{_has_query} ? '&' : '?';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_has_query'} ? : 0 2 2
104 2 $url .= "oauth_token=" . $self->{access_token};
105 }
106
107 ##construct the new url
108 2 my @array;
109
110 2 ▶ while ( my ($key, $value) = each(%{$params})){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$params;}) 0 2 2
111 0 $value = uri_escape($value);
112 0 push(@array, "$key=$value");
113 }
114
115 2 my $string = join('&', @array);
116 2 ▶ $url .= "&".$string if $string;
Branch: 1 branch
BranchTrueFalseTotal
if $string 0 2 2
117
118 2 my $response = $self->{browser}->get($url);
119 2 my $content = $response->content();
120 2 return $self->_content($content);
121 }
122
123 0 ▶sub post {
Subroutine post: not called CC=3 SCAR=24.8
Pod undocumented
124 0 my ($self,$url,$params) = @_;
125 0 ▶ unless ($self->_has_access_token($url)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_access_token($url)) 0 0 0
126 0 ▶ croak "You must pass access_token" unless defined $self->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"access_token"} 0 0 0
127 0 $params->{oauth_token} = $self->{access_token};
128 }
129 0 my $response = $self->{browser}->post($url,$params);
130 0 my $content = $response->content();
131 0 return $self->_content($content);
132 }
133
134 1 ▶sub delete {
Subroutine delete: called CC=3 SCAR=11.2
Pod undocumented
135 1 my ($self,$url,$params) = @_;
136 1 ▶ unless ($self->_has_access_token($url)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_access_token($url)) 1 0 1
137 1 ▶ croak "You must pass access_token" unless defined $self->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"access_token"} 0 1 1
138 1 $params->{oauth_token} = $self->{access_token};
139 }
140 1 my $response = $self->{browser}->delete($url,$params);
141 1 my $content = $response->content();
142 1 return $self->_content($content);
143 }
144
145 0 ▶sub as_hash {
Subroutine as_hash: not called CC=1 SCAR=6.9
Pod undocumented
146 0 my ($self) = @_;
147 0 my $j = JSON::Any->new;
148 0 return $j->jsonToObj($self->{content});
149 }
150
151 3 ▶sub as_json {
Subroutine as_json: called CC=1 SCAR=0.0
Pod undocumented
152 3 my ($self) = @_;
153 3 return $self->{content};
154 }
155
156 3 ▶sub _content {
Subroutine _content: called CC=1 SCAR=0.0
157 3 my ($self,$content) = @_;
158 3 $self->{content} = $content;
159 3 return $self;
160 }
161
162 3 ▶sub _has_access_token {
Subroutine _has_access_token: called CC=2 SCAR=7.5
163 3 my ($self, $url) = @_;
164 3 my $uri = URI->new($url);
165 3 my %q = $uri->query_form;
166 #also check if we have a query and save result
167 3 $self->{_has_query} = $uri->query();
168 0 ▶ if (grep { $_ eq 'oauth_token' } keys %q) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "oauth_token";} keys %q) 0 3 3
169 0 return 1;
170 }
171 3 return;
172 }
173
174 2 1;
↑ Summary