| 1 | package Net::StackExchange::Core; | ||||||||||
| 2 | 4 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 4 | $Net::StackExchange::Core::VERSION = '0.102740'; | |||||||||
| 4 | } | ||||||||||
| 5 | |||||||||||
| 6 | # ABSTRACT: Subroutines for internal use | ||||||||||
| 7 | |||||||||||
| 8 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 4 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 4 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 4 | ▶ | use Scalar::Util qw{ blessed }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 4 | ▶ | use URI::Escape; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | # transform hash keys and corresponding values to query string and return it | ||||||||||
| 17 | 1 | ▶ | sub _get_query_string { | ||||||||
|
Subroutine
_get_query_string: called CC=3 SCAR=11.0
| |||||||||||
| 18 | 1 | my $parametres = shift; | |||||||||
| 19 | 1 | my @query_string; | |||||||||
| 20 | |||||||||||
| 21 | 1 | ▶ | while ( my ( $key, $value ) = each %{$parametres} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | 10 | $key = uri_escape($key ); | |||||||||
| 23 | 10 | $value = uri_escape($value); | |||||||||
| 24 | |||||||||||
| 25 | 10 | ▶ | next unless defined $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | |||||||||||
| 27 | 4 | push @query_string, "$key=$value"; | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | 1 | return join '&', @query_string; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | # prepare a hash with attributes and their values, and return it | ||||||||||
| 34 | 1 | ▶ | sub _get_parametres { | ||||||||
|
Subroutine
_get_parametres: called CC=2 SCAR=6.9
| |||||||||||
| 35 | 1 | my $self = shift; | |||||||||
| 36 | 1 | my @attributes = $self->_get_request_attributes(); | |||||||||
| 37 | |||||||||||
| 38 | 1 | my $parametres; | |||||||||
| 39 | 1 | for my $attribute (@attributes) { | |||||||||
| 40 | 10 | $parametres->{$attribute} = $self->$attribute(); | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 1 | return $parametres; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 1 | ▶ | sub _execute { | ||||||||
|
Subroutine
_execute: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 1 | my $self = shift; | |||||||||
| 48 | 1 | my $se = $self->_NSE(); | |||||||||
| 49 | 1 | my $network = $se->network(); | |||||||||
| 50 | 1 | my $version = $se->version(); | |||||||||
| 51 | 1 | my $id = $self->id(); | |||||||||
| 52 | |||||||||||
| 53 | 1 | my $route = blessed($self); | |||||||||
| 54 | 1 | $route =~ s/^Net::StackExchange::|::Request$//g; | |||||||||
| 55 | 1 | $route = lc $route; | |||||||||
| 56 | |||||||||||
| 57 | 1 | my $url = "http://api.$network/$version/$route/$id"; | |||||||||
| 58 | |||||||||||
| 59 | 1 | my $ua = LWP::UserAgent->new( 'agent' => 'Mozilla' ); | |||||||||
| 60 | 1 | my $parametres = _get_parametres($self); | |||||||||
| 61 | 1 | my $query_string = _get_query_string($parametres); | |||||||||
| 62 | 1 | $url = "$url?$query_string"; | |||||||||
| 63 | 1 | my $ua_response = $ua->get($url); | |||||||||
| 64 | |||||||||||
| 65 | 1 | return $ua_response->decoded_content(); | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 4 | 1; | |||||||||
| 69 | |||||||||||
| 70 | __END__ | ||||||||||