Pachube.pm

filter: Statementstmt 100% 64 / 64 Branchbran 100% 26 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 100% 107 / 107 CC 17 SCAR 19.0
CC
17
Cov
100%
create
13.0
_request
4.0
feed
2.0
CRAP 6.7
↑ Summary Pachube/Feed.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Net::Pachube;
4 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 $Net::Pachube::VERSION = '1.102900';
6 }
7  
8 # ABSTRACT: Perl extension for accessing pachube.com
9  
10  
11 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use base qw/Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Net::Pachube::Feed;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 __PACKAGE__->mk_accessors(qw/key url user_agent/);
20 2 __PACKAGE__->mk_ro_accessors(qw/http_response/);
21  
22  
23 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
24 2 my $pkg = shift;
25 $pkg->SUPER::new({ url => 'http://www.pachube.com/api',
26 user_agent => LWP::UserAgent->new(),
27 key => $ENV{PACHUBE_API_KEY},
28 2 @_ });
29 }
30  
31  
32 6 ▶sub feed {
Subroutine feed: called CC=2 SCAR=6.9
Pod documented
33 6 my ($self, $feed_id, $fetch) = @_;
34 6 ▶ $fetch = 1 unless (defined $fetch);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fetch 5 1 6
35 6 Net::Pachube::Feed->new(id => $feed_id, pachube => $self, fetch => $fetch);
36 }
37  
38  
39 6 ▶sub create {
Subroutine create: called CC=13 SCAR=25.6
Pod documented
40 6 my $self = shift;
41 6 my %p = @_;
42 6 ▶ exists $p{title} or croak "New feed should have a 'title' attribute.\n";
Branch: 1 branch
BranchTrueFalseTotal
exists $p{"title"} or croak("New feed should have a 'title' attribute.\n") 5 1 6
43 5 my $xml = q{<?xml version="1.0" encoding="UTF-8"?>
44 <eeml xmlns="http://www.eeml.org/xsd/005"
45 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
46 xsi:schemaLocation="http://www.eeml.org/xsd/005 http://www.eeml.org/xsd/005/005.xsd" version="5">
47 };
48 my %args =
49 (
50 5 title => [ $p{title} ],
51 );
52 5 foreach (qw/description icon website email/) {
53 20 ▶ $args{$_} = [$p{$_}] if (exists $p{$_});
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{$_} 4 16 20
54 }
55 5 my %location = ();
56 5 foreach (qw/exposure domain disposition/) {
57 15 ▶ $location{$_} = $p{$_} if (exists $p{$_});
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{$_} 3 12 15
58 }
59 5 foreach (qw/lat lon ele/) {
60 15 ▶ $location{$_} = [$p{$_}] if (exists $p{$_});
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{$_} 3 12 15
61 }
62 5 ▶ $location{name} = [$p{location_name}] if (exists $p{location_name});
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{"location_name"} 1 4 5
63 5 ▶ $args{location} = \%location if (scalar keys %location);
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %location 1 4 5
64 5 $xml .= XMLout(\%args, RootName => "environment");
65 5 $xml .= "</eeml>\n";
66 5 ▶ my $resp = $self->_request(method => 'POST', url => $self->url.'.xml',
Branch: 1 branch
BranchTrueFalseTotal
my $resp = $self->_request("method", "POST", "url", $self->url . '.xml', "content", $xml) or return 4 1 5
67 content => $xml) or return;
68 4 ▶ my $url = $resp->header('Location') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $url = $resp->header("Location") or return 3 1 4
69 3 ▶ return unless ($url =~ m!/(\d+)\.xml$!);
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[/(\d+)\.xml$] 1 2 3
70 2 $self->feed($1);
71 }
72  
73 14 ▶sub _request {
Subroutine _request: called CC=4 SCAR=13.9
74 14 my $self = shift;
75 14 ▶ my $key = $self->key or
Branch: 1 branch
BranchTrueFalseTotal
my $key = $self->key or croak("No pachube api key defined.\nSet PACHUBE_API_KEY environment variable or pass 'key' parameter to the\nconstructor.\n") 13 1 14
76 croak(q{No pachube api key defined.
77 Set PACHUBE_API_KEY environment variable or pass 'key' parameter to the
78 constructor.
79 });
80 13 my %p = @_;
81 13 my $ua = $self->user_agent;
82 13 $ua->default_header('X-PachubeApiKey' => $key);
83 13 my $request = HTTP::Request->new($p{method} => $p{url});
84 13 ▶ $request->content($p{content}) if (exists $p{content});
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{"content"} 7 6 13
85 13 my $resp = $self->{http_response} = $ua->request($request);
86 13 ▶ $resp->is_success && $resp;
Branch: 1 branch
BranchTrueFalseTotal
$resp->is_success and $resp 11 2 13
87 }
88  
89 2 1;
90  
91  
92 =pod
93  
94 =head1 NAME
95  
96 Net::Pachube - Perl extension for accessing pachube.com
97  
98 =head1 VERSION
99  
100 version 1.102900
101  
102 =head1 SYNOPSIS
103  
104 use Net::Pachube;
105 my $pachube = Net::Pachube->new();
106 my $feed = $pachube->feed($feed_id);
107 print $feed->title, " ", $feed->status, "\n";
108 foreach my $i (0..$feed->number_of_streams-1) {
109 print "Stream ", $i, " value: ", $feed->data_value($i), "\n";
110 foreach my $tag ($feed->data_tags($i)) {
111 print " Tag: ", $tag, "\n";
112 }
113 }
114  
115 # update several streams at once
116 $feed->update(data => [0,1,2,3,4]);
117  
118 # update one stream
119 $feed->update(data => 99);
120  
121 =head1 DESCRIPTION
122  
123 This module provides a simple API to fetch and/or update pachube.com
124 feeds.
125  
126 =head1 ATTRIBUTES
127  
128 =head2 C<key( [$new_key] )>
129  
130 This method is an accessor/setter for the C<key> attribute which is
131 the Pachube API key to use.
132  
133 =head2 C<url( [$new_url] )>
134  
135 This method is an accessor/setter for the C<url> attribute
136 which is the base URL to use for all HTTP requests.
137  
138 =head2 C<user_agent( [$new_user_agent] )>
139  
140 This method is an accessor/setter for the C<user_agent> attribute
141 which is the L<LWP> user agent object to use for all HTTP requests.
142  
143 =head1 METHODS
144  
145 =head2 C<new( %parameters )>
146  
147 The constructor creates a new L<Net:Pachube> object. The constructor
148 takes a parameter hash as arguments. Valid parameters in the hash
149 are:
150  
151 =over
152  
153 =item key
154  
155 The Pachube API key to use. This parameter is optional. If it is
156 not provided then the value of the environment variable
157 C<PACHUBE_API_KEY> is used.
158  
159 =item url
160  
161 The base URL to use for all HTTP requests. The default is
162 C<http://www.pachube.com/api>.
163  
164 =item user_agent
165  
166 The L<LWP> user agent object to use for all HTTP requests. The
167 default is to create a new one for each new L<Net::Pachube> object.
168  
169 =back
170  
171 =head2 C<feed( $feed_id )>
172  
173 This method constructs a new L<Net::Pachube::Feed> object and retrieves
174 the feed data from the server.
175  
176 =head2 C<create( %parameters )>
177  
178 This method makes a C<POST> request to create a new feed. If
179 successful, it returns a L<Net::Pachube::Feed> object for the new feed
180 otherwise it returns undef. The following keys are significant in the
181 hash passed to this method:
182  
183 =over
184  
185 =item title
186  
187 The title of the new feed. This is the only mandatory attribute.
188  
189 =item description
190  
191 A description of the new feed.
192  
193 =item icon
194  
195 The URL of an icon to associate with the new feed.
196  
197 =item website
198  
199 The URL of a website to associate with the new feed.
200  
201 =item email
202  
203 An email to associate with the new feed. B<This email address will
204 be publicly available on the L<www.pachube.com> site, so please
205 don't use any email address you wish to keep private.>
206  
207 =item exposure
208  
209 The 'exposure' of the new feed - either 'outdoor' or 'indoor'.
210  
211 =item disposition
212  
213 The 'disposition' of the new feed - either 'fixed' or 'mobile'.
214  
215 =item domain
216  
217 The 'domain' of the new feed - either 'physical' or 'virtual'.
218  
219 =item location_name
220  
221 The name of the location of the new feed.
222  
223 =item lat
224  
225 The latitude of the new feed.
226  
227 =item lon
228  
229 The longitude of the new feed.
230  
231 =item ele
232  
233 The elevation of the new feed.
234  
235 =back
236  
237 =head1 SEE ALSO
238  
239 Pachube web site: http://www.pachube.com/
240  
241 =head1 AUTHOR
242  
243 Mark Hindess <soft-pachube@temporalanomaly.com>
244  
245 =head1 COPYRIGHT AND LICENSE
246  
247 This software is copyright (c) 2010 by Mark Hindess.
248  
249 This is free software; you can redistribute it and/or modify it under
250 the same terms as the Perl 5 programming language system itself.
251  
252 =cut
253  
254  
255 __END__
↑ Summary Pachube/Feed.pm »