File Coverage

blib/lib/WebService/FogBugz.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package WebService::FogBugz;
2              
3 6     6   93914 use warnings;
  6         10  
  6         185  
4 6     6   23 use strict;
  6         7  
  6         227  
5              
6             our $VERSION = '0.1.1';
7              
8             #----------------------------------------------------------------------------
9             # Library Modules
10              
11 6     6   36905 use LWP::UserAgent;
  6         227912  
  6         219  
12 6     6   2720 use WebService::FogBugz::Config;
  6         15  
  6         78  
13 6     6   4479 use XML::Liberal;
  0            
  0            
14             use XML::LibXML;
15              
16             #----------------------------------------------------------------------------
17             # Public API
18              
19             sub new {
20             my $class = shift;
21             my $param = { @_ };
22             my $atts = {};
23              
24             my $self = bless $atts, $class;
25             $self->{UA} = $param->{ua} || LWP::UserAgent->new;
26             $self->{UA}->agent(__PACKAGE__.'/'.$VERSION);
27             $self->{PARSER} = XML::Liberal->new('LibXML');
28              
29             die 'no configuration parameters'
30             unless $param;
31              
32             $self->{CONFIG} = WebService::FogBugz::Config->new( %$param );
33             die 'no configuration parameters'
34             unless $self->{CONFIG};
35              
36             die 'no instance of FogBugz specified '
37             unless $self->{CONFIG}->base_url;
38             die 'no login details of FogBugz instance specified '
39             unless ($self->{CONFIG}->token || ($self->{CONFIG}->email && $self->{CONFIG}->password));
40              
41             $self->logon;
42             die 'unable to log into the specified instance of FogBugz'
43             unless $self->{token};
44              
45             # $self->{COMMAND} = WebService::FogBugz::Command->new(
46             # base_url => $self->{CONFIG}->base_url,
47             # token => $self->{token}
48             # );
49              
50             return $self;
51             }
52              
53             sub logon {
54             my $self = shift;
55              
56             if($self->{CONFIG}->token) {
57             $self->{token} = $self->{CONFIG}->token;
58              
59             } else {
60             my $res = $self->{UA}->get(
61             $self->{CONFIG}->base_url
62             . '?cmd=logon'
63             . '&email=' . $self->{CONFIG}->email
64             . '&password=' . $self->{CONFIG}->password);
65              
66             return if ($self->_is_error($res->content));
67            
68             my $doc = $self->{PARSER}->parse_string($res->content);
69             $self->{token} = $doc->findvalue("//*[local-name()='response']/*[local-name()='token']/text()");
70             }
71              
72             return $self->{token};
73             }
74              
75             sub logoff {
76             my $self = shift;
77             my $res = $self->{UA}->get(
78             $self->{CONFIG}->base_url
79             . '?cmd=logoff'
80             . '&token=' . $self->{token});
81              
82             return if ($self->_is_error($res->content));
83              
84             delete $self->{token};
85             return;
86             }
87              
88             sub request_method {
89             my ($self, $cmd, $param) = @_;
90             my $query = join('', map {'&' . $_ . '=' . $param->{$_}} keys(%$param));
91             my $res = $self->{UA}->get(
92             $self->{CONFIG}->base_url
93             . '?cmd=' . $cmd
94             . '&token=' . $self->{token}
95             . $query);
96              
97             return if ($self->_is_error($res->content));
98              
99             return $res->content;
100             }
101              
102             sub _is_error {
103             my ($self, $content) = @_;
104             $content =~ s/<\?xml\s+.*?\?>//g;
105             return 1 unless($content && $content =~ /
106              
107             my $doc = $self->{PARSER}->parse_string($content);
108             $self->{error}{code} = $doc->findvalue("//*[local-name()='response']/*[local-name()='error']/\@code");
109             $self->{error}{msg} = $doc->findvalue("//*[local-name()='response']/*[local-name()='error']/text()");
110             return $self->{error}{code} ? '1' : '0';
111             }
112              
113             1;
114              
115             __END__