[BACK]Return to OpenAMD.pm CVS log [TXT][DIR] Up to [local] / HOPE / Net-OpenAMD / lib / Net

Annotation of HOPE/Net-OpenAMD/lib/Net/OpenAMD.pm, Revision 1.12

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>