| 1 | package Net::StackExchange2::V2::Common; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
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 Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use constant BASE_URL => "https://api.stackexchange.com/2.1/"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 12 | 1 | our @EXPORT = qw(query no_params one_param two_params); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | our $VERSION = "0.05"; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub query { | ||||||||||||||||||||||||||
|
Subroutine
query: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | 0 | my $queryStrHash = pop @_; | |||||||||||||||||||||||||||
| 19 | # print Dumper(@_); | ||||||||||||||||||||||||||||
| 20 | 0 | my $url = join("/",@_); | |||||||||||||||||||||||||||
| 21 | 0 | my @params = (); | |||||||||||||||||||||||||||
| 22 | 0 | ▶ | while ( my ($key, $value) = each(%$queryStrHash) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 0 | push @params, $key."=".$value; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 0 | my $query = '?'.join '&',@params; | |||||||||||||||||||||||||||
| 26 | 0 | my $finalUrl = BASE_URL.$url.$query; | |||||||||||||||||||||||||||
| 27 | 0 | print $finalUrl; | |||||||||||||||||||||||||||
| 28 | 0 | my $ua = LWP::UserAgent->new; | |||||||||||||||||||||||||||
| 29 | 0 | my $response = $ua->get($finalUrl); | |||||||||||||||||||||||||||
| 30 | 0 | return decode_json($response->decoded_content); | |||||||||||||||||||||||||||
| 31 | #StackExchange2 already returns error codes for | ||||||||||||||||||||||||||||
| 32 | #incorrect params and requests | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # if ($response->is_success) { | ||||||||||||||||||||||||||||
| 35 | # return decode_json($response->decoded_content); | ||||||||||||||||||||||||||||
| 36 | # } | ||||||||||||||||||||||||||||
| 37 | # else { | ||||||||||||||||||||||||||||
| 38 | # croak $response->status_line; | ||||||||||||||||||||||||||||
| 39 | # } | ||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub query_post { | ||||||||||||||||||||||||||
|
Subroutine
query_post: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | 0 | my $queryStrHash = pop @_; | |||||||||||||||||||||||||||
| 43 | # print Dumper(%$queryStrHash); | ||||||||||||||||||||||||||||
| 44 | #for the purposes of POST we need all the excess query str stuff to go into POST form data | ||||||||||||||||||||||||||||
| 45 | 0 | my $url = join("/",@_); | |||||||||||||||||||||||||||
| 46 | 0 | my $finalUrl = BASE_URL.$url; | |||||||||||||||||||||||||||
| 47 | 0 | print $finalUrl; | |||||||||||||||||||||||||||
| 48 | 0 | my $ua = LWP::UserAgent->new; | |||||||||||||||||||||||||||
| 49 | #apprantly the second param in the post has to be a hash inside an array | ||||||||||||||||||||||||||||
| 50 | 0 | my $response = $ua->post($finalUrl, [%$queryStrHash]); | |||||||||||||||||||||||||||
| 51 | 0 | return decode_json($response->decoded_content); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub no_params { | ||||||||||||||||||||||||||
|
Subroutine
no_params: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 0 | my $param = shift; | |||||||||||||||||||||||||||
| 55 | 0 | my $config = shift; | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 57 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 58 | 0 | my $queryStr = pop @_; | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | if(defined($queryStr)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | # copy current method keys and vals | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | while(my($key,$val) = each(%$queryStr)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||
| 63 | 0 | $self->{$key} = $val; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if (defined($config) and $config->{no_site} == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $config and $config->{"no_site"} == 1
MC/DC0%0/2
defined $config and $config->{"no_site"} == 1
defined $config
$config->{"no_site"} == 1
Truth table0%0/3
A and B
A= defined $configB= $config->{"no_site"} == 1
| |||||||||||||||||||||||||||||
| 67 | 0 | $self->{site} = ''; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 0 | return query($param, $self); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 0 | } | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub one_param { | ||||||||||||||||||||||||||
|
Subroutine
one_param: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | 0 | my $param1 = shift; | |||||||||||||||||||||||||||
| 75 | 0 | my $param2 = shift; | |||||||||||||||||||||||||||
| 76 | 0 | my $config = shift; | |||||||||||||||||||||||||||
| 77 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||
| 78 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 79 | 0 | my $ids = shift; | |||||||||||||||||||||||||||
| 80 | #you should check if this param is NOT null/empty | ||||||||||||||||||||||||||||
| 81 | 0 | my $q = shift; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if(defined($q)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | # copy current method keys and vals | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | while(my($key,$val) = each(%$q)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||||||||
| 87 | 0 | $self->{$key} = $val; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 0 | my $ids_str = ""; | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | if (ref($ids) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | $ids_str = join(";",@$ids); | |||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||
| 94 | 0 | $ids_str = $ids.""; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | # print Dumper($q); | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if (defined($config)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if(exists $config->{no_site} and $config->{no_site} == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $config->{"no_site"} and $config->{"no_site"} == 1
MC/DC0%0/2
exists $config->{"no_site"} and $config->{"no_site"} == 1
exists $config->{"no_site"}
$config->{"no_site"} == 1
Truth table0%0/3
A and B
A= exists $config->{"no_site"}B= $config->{"no_site"} == 1
| |||||||||||||||||||||||||||||
| 99 | 0 | $self->{site} = ''; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if(exists $config->{post} and $config->{post} == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $config->{"post"} and $config->{"post"} == 1
MC/DC0%0/2
exists $config->{"post"} and $config->{"post"} == 1
exists $config->{"post"}
$config->{"post"} == 1
Truth table0%0/3
A and B
A= exists $config->{"post"}B= $config->{"post"} == 1
| |||||||||||||||||||||||||||||
| 102 | #this is a little icky, but should do for now. | ||||||||||||||||||||||||||||
| 103 | 0 | print Dumper($self); | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | if (not defined($param2)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | return query_post($param1, $ids_str , $self); | |||||||||||||||||||||||||||
| 106 | } else { | ||||||||||||||||||||||||||||
| 107 | 0 | return query_post($param1, $ids_str, $param2, $self); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if (not defined($param2)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | return query($param1, $ids_str , $self); | |||||||||||||||||||||||||||
| 113 | } else { | ||||||||||||||||||||||||||||
| 114 | 0 | return query($param1, $ids_str, $param2, $self); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 0 | } | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub two_params { | ||||||||||||||||||||||||||
|
Subroutine
two_params: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 119 | 0 | my $param1 = shift; | |||||||||||||||||||||||||||
| 120 | 0 | my $param2 = shift; | |||||||||||||||||||||||||||
| 121 | 0 | my $param3 = shift; | |||||||||||||||||||||||||||
| 122 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 123 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 124 | 0 | my $ids = shift; | |||||||||||||||||||||||||||
| 125 | 0 | my $ids_2 = shift; | |||||||||||||||||||||||||||
| 126 | 0 | my $q = shift; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | if(defined($q)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | # copy current method keys and vals | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | while(my($key,$val) = each(%$q)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | { | ||||||||||||||||||||||||||||
| 132 | 0 | $self->{$key} = $val; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 0 | my $ids_str = ""; | |||||||||||||||||||||||||||
| 136 | 0 | ▶ | if (ref($ids) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | $ids_str = join(";",@$ids); | |||||||||||||||||||||||||||
| 138 | } else { | ||||||||||||||||||||||||||||
| 139 | 0 | $ids_str = $ids.""; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 0 | my $ids_str_2 = ""; | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | if (ref($ids_2) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | $ids_str_2 = join(";",@$ids_2); | |||||||||||||||||||||||||||
| 144 | } else { | ||||||||||||||||||||||||||||
| 145 | 0 | $ids_str_2 = $ids_2.""; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | # print Dumper($q); | ||||||||||||||||||||||||||||
| 148 | # if (defined($config) and $config->{no_site} == 1) { | ||||||||||||||||||||||||||||
| 149 | # $self->{site} = ''; | ||||||||||||||||||||||||||||
| 150 | # } | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if (not defined($param3)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 0 | return query($param1, $ids_str, $param2, $ids_str_2, $self); | |||||||||||||||||||||||||||
| 153 | } else { | ||||||||||||||||||||||||||||
| 154 | 0 | return query($param1, $ids_str, $param2, $ids_str_2, $param3, $self); | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | 0 | } | |||||||||||||||||||||||||||
| 158 | 1 | 1; # End of Net::StackExchange2::V2::Common | |||||||||||||||||||||||||||
| 159 | __END__ | ||||||||||||||||||||||||||||