File Coverage

blib/lib/Games/AssaultCube/MasterserverQuery/Response.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             # Declare our package
2             package Games::AssaultCube::MasterserverQuery::Response;
3              
4             # import the Moose stuff
5 1     1   2571 use Moose;
  0            
  0            
6             use MooseX::StrictConstructor;
7              
8             # Initialize our version
9             use vars qw( $VERSION );
10             $VERSION = '0.04';
11              
12             # get some utility stuff
13             use Games::AssaultCube::Utils qw( parse_masterserverresponse );
14              
15             # TODO improve validation for everything here, ha!
16              
17             has 'masterserver' => (
18             isa => 'Str',
19             is => 'ro',
20             required => 1,
21             );
22              
23             has 'servers' => (
24             isa => 'ArrayRef[HashRef]',
25             is => 'ro',
26             default => sub { [] },
27             );
28              
29             has 'num_servers' => (
30             isa => 'Int',
31             is => 'ro',
32             lazy => 1,
33             default => sub {
34             my $self = shift;
35             return scalar @{ $self->servers };
36             },
37             );
38              
39             has 'response' => (
40             isa => 'HTTP::Response',
41             is => 'ro',
42             required => 1,
43             );
44              
45             has 'tohash' => (
46             isa => 'HashRef',
47             is => 'ro',
48             lazy => 1,
49             default => sub {
50             my $self = shift;
51             my $data = {
52             masterserver => $self->masterserver,
53             servers => [ map { { ip => $_->{ip}, port => $_->{port} } } @{ $self->servers } ],
54             };
55             return $data;
56             },
57             );
58              
59             sub BUILDARGS {
60             my $class = shift;
61              
62             # Normally, we would be created by Games::AssaultCube::MasterserverQuery and contain 2 args
63             if ( @_ == 2 && ref $_[0] && $_[0]->isa( 'Games::AssaultCube::MasterserverQuery' ) ) {
64             # call the parse method
65             return {
66             masterserver => $_[0]->server,
67             servers => parse_masterserverresponse( $_[1] ),
68             response => $_[1],
69             };
70             } else {
71             return $class->SUPER::BUILDARGS(@_);
72             }
73             }
74              
75             # from Moose::Manual::BestPractices
76             no Moose;
77             __PACKAGE__->meta->make_immutable;
78              
79             1;
80             __END__
81              
82             =for stopwords masterserver tohash URI hashrefs ip
83              
84             =head1 NAME
85              
86             Games::AssaultCube::MasterserverQuery::Response - Holds the various data from a MasterserverQuery response
87              
88             =head1 SYNOPSIS
89              
90             use Games::AssaultCube::MasterserverQuery;
91             my $query = Games::AssaultCube::MasterserverQuery->new;
92             #my $query = Games::AssaultCube::MasterserverQuery->new( 'http://foo.com/get.do' );
93             #my $query = Games::AssaultCube::MasterserverQuery->new({ server => 'http://foo.com/get.do', timeout => 5 });
94             my $response = $query->run;
95             if ( defined $response ) {
96             print "There is a total of " . $response->num_servers " servers in the list!\n";
97             } else {
98             print "Masterserver is not responding!\n";
99             }
100              
101             =head1 ABSTRACT
102              
103             This module holds the various data from a MasterserverQuery response
104              
105             =head1 DESCRIPTION
106              
107             This module holds the response data from an AssaultCube MasterserverQuery. Normally you will not use this class
108             directly, but via the L<Games::AssaultCube::MasterserverQuery> class.
109              
110             =head2 Attributes
111              
112             You can get the various data by fetching the attribute. Valid attributes are:
113              
114             =head3 masterserver
115              
116             The URI of the masterserver we queried
117              
118             =head3 servers
119              
120             An arrayref of hashrefs of servers in the list
121              
122             The hashref contains the following keys: ip and port
123              
124             =head3 num_servers
125              
126             A convenience accessor returning the number of servers in the list
127              
128             =head3 response
129              
130             The HTTP::Response object in case you wanted to poke around
131              
132             =head3 tohash
133              
134             A convenience accessor returning "vital" data in a hashref for easy usage
135              
136             =head1 AUTHOR
137              
138             Apocalypse E<lt>apocal@cpan.orgE<gt>
139              
140             Props goes to Getty and the BS clan for the support!
141              
142             This project is sponsored by L<http://cubestats.net>
143              
144             =head1 COPYRIGHT AND LICENSE
145              
146             Copyright 2009 by Apocalypse
147              
148             This library is free software; you can redistribute it and/or modify
149             it under the same terms as Perl itself.
150              
151             =cut