File Coverage

blib/lib/WWW/Ohloh/API/Stack.pm
Criterion Covered Total %
statement 60 85 70.5
branch 6 12 50.0
condition 1 3 33.3
subroutine 18 21 85.7
pod 3 7 42.8
total 88 128 68.7


line stmt bran cond sub pod time code
1             package WWW::Ohloh::API::Stack;
2             our $AUTHORITY = 'cpan:YANICK';
3              
4 3     3   216630 use strict;
  3         6  
  3         139  
5 3     3   18 use warnings;
  3         6  
  3         223  
6              
7 3     3   21 use Carp;
  3         7  
  3         399  
8 3         31 use Object::InsideOut qw/ WWW::Ohloh::API::Role::Fetchable
9 3     3   1168 WWW::Ohloh::API::Role::LoadXML /;
  3         75900  
10 3     3   1656 use XML::LibXML;
  3         53856  
  3         28  
11 3     3   1484 use Readonly;
  3         5723  
  3         239  
12 3     3   26 use Scalar::Util qw/ weaken /;
  3         8  
  3         211  
13 3     3   645 use Date::Parse;
  3         10523  
  3         431  
14 3     3   919 use Time::Piece;
  3         12816  
  3         31  
15 3     3   426 use Digest::MD5 qw/ md5_hex /;
  3         7  
  3         193  
16              
17 3     3   2049 use WWW::Ohloh::API::StackEntry;
  3         17  
  3         33  
18              
19 3     3   371 use Params::Validate qw/ validate_with /;
  3         26  
  3         2419  
20              
21             our $VERSION = '1.0_1';
22              
23             my @ohloh_of : Field : Arg(ohloh);
24             my @request_url_of : Field : Arg(request_url) : Get( request_url );
25             my @xml_of : Field : Arg(xml);
26              
27             my @api_fields = qw/
28             id
29             updated_at
30             project_count
31             stack_entries
32             account_id
33             account
34             /;
35              
36             __PACKAGE__->create_field( '@' . $_, ":Set(_set_$_)", ":Get($_)" )
37             for qw/ id updated_at project_count account_id /;
38              
39             my @stack_entries_of : Field;
40             my @account_of : Field;
41              
42             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
43              
44 1     1 0 9 sub element_name { return 'stack'; }
45              
46             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47              
48             sub load_xml {
49 5     5 0 56 my ( $self, $dom ) = @_;
50              
51 5         13 for my $f (qw/ id project_count account_id /) {
52 15         1072 my $method = "_set_$f";
53 15         57 $self->$method( $dom->findvalue("$f/text()") );
54             }
55              
56             $self->_set_updated_at(
57 5         473 scalar Time::Piece::gmtime(
58             str2time( $dom->findvalue("updated_at/text()") ) ) );
59              
60 5 100       2383 if ( my ($account_xml) = $dom->findnodes('account[1]') ) {
61 4         137 $account_of[$$self] = WWW::Ohloh::API::Account->new(
62             ohloh => $ohloh_of[$$self],
63             xml => $account_xml,
64             );
65             }
66              
67 5         527 $stack_entries_of[$$self] = [
68             map WWW::Ohloh::API::StackEntry->new(
69             ohloh => $ohloh_of[$$self],
70             xml => $_,
71             ) => $dom->findnodes('stack_entries/stack_entry') ];
72             }
73              
74             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75              
76             sub generate_query_url : Chained(bottom up) {
77 0     0 0 0 my ( $self, @args ) = @_;
78              
79 0         0 my %param = validate_with(
80             params => \@args,
81             spec => { id => 1 },
82             allow_extra => 1
83             );
84 0         0 my $id = $param{id};
85 0         0 delete $param{id};
86              
87 0 0       0 if ( index( $id, '@' ) > -1 ) {
88 0         0 $id = md5_hex($id);
89             }
90              
91 0         0 return ( "accounts/$id/stacks/default.xml", ohloh => $param{ohloh} );
92 3     3   33 }
  3         7  
  3         18  
93              
94             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
95              
96             sub stack_entries {
97 8     8 1 3333 my $self = shift;
98 8         13 return @{ $stack_entries_of[$$self] };
  8         89  
99             }
100              
101             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102              
103             sub as_xml {
104 0     0 1 0 my $self = shift;
105 0         0 my $xml;
106 0         0 my $w = XML::Writer->new( OUTPUT => \$xml );
107              
108 0         0 $w->startTag('stack');
109              
110 0         0 for my $f (qw/ id updated_at project_count account_id /) {
111 0         0 $w->dataElement( $f => $self->$f );
112             }
113              
114 0 0       0 if ( my $account = $account_of[$$self] ) {
115 0         0 $xml .= $account->as_xml;
116             }
117              
118 0 0       0 if ( my @entries = @{ $stack_entries_of[$$self] } ) {
  0         0  
119 0         0 $w->startTag('stack_entries');
120 0         0 $xml .= $_->as_xml for @entries;
121 0         0 $w->endTag;
122             }
123              
124 0         0 $w->endTag;
125              
126 0         0 return $xml;
127             }
128              
129             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
130              
131             sub account {
132 5     5 1 17404 my $self = shift;
133              
134 5         12 my $retrieve = shift;
135              
136 5 100       22 $retrieve = 1 unless defined $retrieve;
137              
138 5 100       18 if ($retrieve) {
139 1   33     8 $account_of[$$self] ||=
140             $ohloh_of[$$self]->fetch_account( $self->account_id );
141             }
142              
143 5         34 return $account_of[$$self];
144              
145             }
146              
147             sub set_account : Private( WWW::Ohloh::API::Account ) {
148 0     0 0   my $self = shift;
149              
150 0           weaken( $account_of[$$self] = shift );
151              
152 0           return;
153 3     3   2345 }
  3         10  
  3         19  
154              
155             'end of WWW::Ohloh::API::Stack';
156              
157             __END__