Annotation of HOPE/Net-OpenAMD/lib/Net/OpenAMD.pm, Revision 1.13
1.1 andrew 1: package Net::OpenAMD;
2:
1.13 ! andrew 3: # $AFresh1: OpenAMD.pm,v 1.12 2010/06/28 20:32:32 andrew Exp $
1.12 andrew 4:
1.13 ! andrew 5: use version; our $VERSION = qv('0.0.2');
1.12 andrew 6: my $BASE_URI = 'https://api.hope.net/api/';
1.1 andrew 7:
8: use warnings;
9: use strict;
10: use Carp;
11:
1.8 andrew 12: use Scalar::Util qw( refaddr );
13: *_ident = \&refaddr;
1.1 andrew 14:
1.2 andrew 15: use LWP::UserAgent;
1.6 andrew 16: use URI;
1.2 andrew 17: use Net::OAuth;
1.13 ! andrew 18: use JSON qw/ from_json /;
1.1 andrew 19:
1.2 andrew 20: {
1.1 andrew 21:
1.10 andrew 22: my @attr_refs = \( my %base_uri_of, my %ua_of, my %auth_of, );
1.2 andrew 23:
24: sub new {
25: my ( $class, $options ) = @_;
1.8 andrew 26: my $self = bless do { \my $x }, $class;
27: my $ident = _ident($self);
1.2 andrew 28:
1.12 andrew 29: $options ||= {};
1.6 andrew 30:
31: croak 'Options should be a hashref' if ref $options ne 'HASH';
32:
1.10 andrew 33: $base_uri_of{$ident} = $options->{base_uri} || $BASE_URI;
1.6 andrew 34: $ua_of{$ident} = $options->{ua} || LWP::UserAgent->new();
35:
36: # XXX Authenticate
37:
1.2 andrew 38: return $self;
39: }
40:
1.7 andrew 41: sub get {
1.6 andrew 42: my ( $self, $action, $query ) = @_;
1.8 andrew 43: my $ident = _ident($self);
1.6 andrew 44:
1.10 andrew 45: my $uri = URI->new_abs( $action, $base_uri_of{$ident} );
1.6 andrew 46: $uri->query($query);
47:
48: my $response = $ua_of{$ident}->get($uri);
1.12 andrew 49: croak $response->status_line if !$response->is_success;
1.6 andrew 50:
1.13 ! andrew 51: my @data = map { from_json($_) } split /\r?\n/xms,
! 52: $response->decoded_content;
! 53:
! 54: return @data == 1 ? $data[0] : \@data;
1.6 andrew 55: }
1.2 andrew 56:
1.9 andrew 57: sub location { return shift->get( 'location', @_ ) }
58: sub speakers { return shift->get( 'speakers', @_ ) }
59: sub talks { return shift->get( 'talks', @_ ) }
60: sub interests { return shift->get( 'interests', @_ ) }
61: sub users { return shift->get( 'users', @_ ) }
62: sub stats { croak 'Unused feature' }
1.6 andrew 63:
64: sub DESTROY {
65: my ($self) = @_;
1.8 andrew 66: my $ident = _ident $self;
1.6 andrew 67:
68: foreach my $attr_ref (@attr_refs) {
69: delete $attr_ref->{$ident};
70: }
71:
72: return;
73: }
74:
1.2 andrew 75: }
76:
77: 1; # Magic true value required at end of module
1.1 andrew 78: __END__
1.12 andrew 79:
1.1 andrew 80: =head1 NAME
81:
1.2 andrew 82: Net::OpenAMD - Perl interface to the OpenAMD API
1.1 andrew 83:
84:
85: =head1 VERSION
86:
87: This document describes Net::OpenAMD version 0.0.1
88:
89:
90: =head1 SYNOPSIS
91:
92: use Net::OpenAMD;
93:
1.7 andrew 94: my $amd = Net::OpenAMD->new();
95:
96: my $location = $amd->location({ area => 'Engressia' });
97:
1.1 andrew 98:
99: =head1 DESCRIPTION
100:
1.2 andrew 101: This module is to make it easy to grab information from the OpenAMD project at
102: The Next Hope.
1.4 andrew 103:
104: http://wiki.hope.net/Attendee_Meta-Data
1.1 andrew 105:
1.2 andrew 106: http://amd.hope.net/
1.3 andrew 107:
108: http://amd.hope.net/2010/05/openamd-api-released-v1-1-1/
1.5 andrew 109:
110: http://travisgoodspeed.blogspot.com/2010/06/hacking-next-hope-badge.html
1.1 andrew 111:
112: =head1 INTERFACE
113:
1.7 andrew 114: =head2 new
115:
116: Create a new object for accessing the OpenAMD API.
117:
118: my $amd = Net::OpenAMD->new( $options );
119:
120: $options is a hashref with configuration options.
121:
122: Current options are
123:
124: =over
125:
1.10 andrew 126: =item base_uri
1.7 andrew 127:
1.9 andrew 128: A URL to the API, currently defaults to https://api.hope.net/api/
129:
130: Most likely it should end with a / to make URI happy, so notice that if you
131: having 404 errors you don't expect.
1.7 andrew 132:
133: =item ua
134:
135: Should be a pre-configured LWP::UserAgent or similar that returns a
136: HTTP::Response object when its get method is called with a URI.
137:
138: =back
139:
140: =head2 get
1.1 andrew 141:
1.7 andrew 142: This is the main method, although probably never used. It has better/easier
143: ways to access the different actions of the API.
1.1 andrew 144:
1.7 andrew 145: my $data = $amd->get( $action, $params );
146:
147: $params are anything that are supported by URI->query, they will get passed
148: on the request.
149:
150: Here $data is a the JSON returned by the API converted to Perl reference.
1.1 andrew 151:
1.7 andrew 152: Helper methods you can call as $amd->method($params) are:
1.1 andrew 153:
154: =over
155:
1.7 andrew 156: =item interests
157:
158: =item location
159:
160: =item new
1.1 andrew 161:
1.7 andrew 162: =item speakers
1.1 andrew 163:
1.7 andrew 164: =item stats
1.1 andrew 165:
1.7 andrew 166: =item talks
1.1 andrew 167:
1.7 andrew 168: =item users
1.1 andrew 169:
170: =back
171:
1.7 andrew 172: Unless specified, there is nothing different about any of the helper methods
173: than just calling get($action) instead. Depending on API changes, this may
174: not always be the case.
175:
176: =head1 DIAGNOSTICS
177:
1.11 andrew 178: All methods should croak when an error occurs.
1.7 andrew 179: If the remote API returns a successful response that contains valid JSON, that
180: will be decoded and returned.
1.1 andrew 181:
182: =head1 CONFIGURATION AND ENVIRONMENT
183:
184: Net::OpenAMD requires no configuration files or environment variables.
185:
1.7 andrew 186: Net::OpenAMD uses LWP::UserAgent for requests and environment for that is
187: not cleared.
1.1 andrew 188:
189: =head1 DEPENDENCIES
190:
1.7 andrew 191: =head3 L<LWP::UserAgent>
1.2 andrew 192:
1.7 andrew 193: =head3 L<URI>
1.2 andrew 194:
1.7 andrew 195: =head3 L<Net::OAuth>
1.1 andrew 196:
1.7 andrew 197: =head3 L<JSON::Any>
1.1 andrew 198:
199:
200: =head1 INCOMPATIBILITIES
201:
202: None reported.
203:
204:
205: =head1 BUGS AND LIMITATIONS
206:
1.7 andrew 207: No bugs have been reported.
208:
209: =over
210:
211: =item Currently it does not support the OAuth that is required to log into the
212: API and get information.
1.1 andrew 213:
1.7 andrew 214: =back
1.1 andrew 215:
216: Please report any bugs or feature requests to
217: C<bug-net-openamd@rt.cpan.org>, or through the web interface at
218: L<http://rt.cpan.org>.
219:
220:
221: =head1 AUTHOR
222:
223: Andrew Fresh C<< <andrew@cpan.org> >>
224:
225:
226: =head1 LICENSE AND COPYRIGHT
227:
228: Copyright (c) 2010, Andrew Fresh C<< <andrew@cpan.org> >>. All rights reserved.
229:
230: This module is free software; you can redistribute it and/or
231: modify it under the same terms as Perl itself. See L<perlartistic>.
232:
233:
234: =head1 DISCLAIMER OF WARRANTY
235:
236: BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
237: FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
238: OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
239: PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
240: EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
241: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
242: ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
243: YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
244: NECESSARY SERVICING, REPAIR, OR CORRECTION.
245:
246: IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
247: WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
248: REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
249: LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
250: OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
251: THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
252: RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
253: FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
254: SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
255: SUCH DAMAGES.
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>