File Coverage

blib/lib/Net/Pachube.pm
Criterion Covered Total %
statement 23 25 92.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod n/a
total 32 34 94.1


line stmt bran cond sub pod time code
1 2     2   35006 use strict;
  2         4  
  2         85  
2 2     2   10 use warnings;
  2         3  
  2         231  
3             package Net::Pachube;
4             BEGIN {
5 2     2   41 $Net::Pachube::VERSION = '1.102900';
6             }
7              
8             # ABSTRACT: Perl extension for accessing pachube.com
9              
10              
11 2     2   58 use 5.006;
  2         7  
  2         101  
12 2     2   9 use base qw/Class::Accessor::Fast/;
  2         2  
  2         2400  
13 2     2   8487 use Carp;
  2         5  
  2         159  
14 2     2   4631 use LWP::UserAgent;
  2         164258  
  2         77  
15 2     2   21 use HTTP::Request;
  2         4  
  2         48  
16 2     2   886 use XML::Simple;
  0            
  0            
17             use Net::Pachube::Feed;
18              
19             __PACKAGE__->mk_accessors(qw/key url user_agent/);
20             __PACKAGE__->mk_ro_accessors(qw/http_response/);
21              
22              
23             sub new {
24             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             @_ });
29             }
30              
31              
32             sub feed {
33             my ($self, $feed_id, $fetch) = @_;
34             $fetch = 1 unless (defined $fetch);
35             Net::Pachube::Feed->new(id => $feed_id, pachube => $self, fetch => $fetch);
36             }
37              
38              
39             sub create {
40             my $self = shift;
41             my %p = @_;
42             exists $p{title} or croak "New feed should have a 'title' attribute.\n";
43             my $xml = q{
44            
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             title => [ $p{title} ],
51             );
52             foreach (qw/description icon website email/) {
53             $args{$_} = [$p{$_}] if (exists $p{$_});
54             }
55             my %location = ();
56             foreach (qw/exposure domain disposition/) {
57             $location{$_} = $p{$_} if (exists $p{$_});
58             }
59             foreach (qw/lat lon ele/) {
60             $location{$_} = [$p{$_}] if (exists $p{$_});
61             }
62             $location{name} = [$p{location_name}] if (exists $p{location_name});
63             $args{location} = \%location if (scalar keys %location);
64             $xml .= XMLout(\%args, RootName => "environment");
65             $xml .= "\n";
66             my $resp = $self->_request(method => 'POST', url => $self->url.'.xml',
67             content => $xml) or return;
68             my $url = $resp->header('Location') or return;
69             return unless ($url =~ m!/(\d+)\.xml$!);
70             $self->feed($1);
71             }
72              
73             sub _request {
74             my $self = shift;
75             my $key = $self->key or
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             my %p = @_;
81             my $ua = $self->user_agent;
82             $ua->default_header('X-PachubeApiKey' => $key);
83             my $request = HTTP::Request->new($p{method} => $p{url});
84             $request->content($p{content}) if (exists $p{content});
85             my $resp = $self->{http_response} = $ua->request($request);
86             $resp->is_success && $resp;
87             }
88              
89             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
129              
130             This method is an accessor/setter for the C attribute which is
131             the Pachube API key to use.
132              
133             =head2 C
134              
135             This method is an accessor/setter for the C attribute
136             which is the base URL to use for all HTTP requests.
137              
138             =head2 C
139              
140             This method is an accessor/setter for the C attribute
141             which is the L user agent object to use for all HTTP requests.
142              
143             =head1 METHODS
144              
145             =head2 C
146              
147             The constructor creates a new L 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 is used.
158              
159             =item url
160              
161             The base URL to use for all HTTP requests. The default is
162             C.
163              
164             =item user_agent
165              
166             The L user agent object to use for all HTTP requests. The
167             default is to create a new one for each new L object.
168              
169             =back
170              
171             =head2 C
172              
173             This method constructs a new L object and retrieves
174             the feed data from the server.
175              
176             =head2 C
177              
178             This method makes a C request to create a new feed. If
179             successful, it returns a L 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
204             be publicly available on the L 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
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__