[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.11

1.1       andrew      1: package Net::OpenAMD;
                      2:
1.11    ! andrew      3: # $AFresh1: OpenAMD.pm,v 1.10 2010/06/27 03:59:32 andrew Exp $
1.1       andrew      4:
                      5: use warnings;
                      6: use strict;
                      7: use Carp;
                      8:
                      9: use version; our $VERSION = qv('0.0.1');
                     10:
1.10      andrew     11: my $BASE_URI = 'https://api.hope.net/api/';
1.1       andrew     12:
1.8       andrew     13: use Scalar::Util qw( refaddr );
                     14: *_ident = \&refaddr;
1.1       andrew     15:
1.2       andrew     16: use LWP::UserAgent;
1.6       andrew     17: use URI;
1.2       andrew     18: use Net::OAuth;
                     19: use JSON::Any;
1.1       andrew     20:
1.2       andrew     21: {
1.1       andrew     22:
1.10      andrew     23:     my @attr_refs = \( my %base_uri_of, my %ua_of, my %auth_of, );
1.2       andrew     24:
                     25:     sub new {
                     26:         my ( $class, $options ) = @_;
1.8       andrew     27:         my $self = bless do { \my $x }, $class;
                     28:         my $ident = _ident($self);
1.2       andrew     29:
1.6       andrew     30:         $options //= {};
                     31:
                     32:         croak 'Options should be a hashref' if ref $options ne 'HASH';
                     33:
1.10      andrew     34:         $base_uri_of{$ident} = $options->{base_uri} || $BASE_URI;
1.6       andrew     35:         $ua_of{$ident}       = $options->{ua}       || LWP::UserAgent->new();
                     36:
                     37:         # XXX Authenticate
                     38:
1.2       andrew     39:         return $self;
                     40:     }
                     41:
1.7       andrew     42:     sub get {
1.6       andrew     43:         my ( $self, $action, $query ) = @_;
1.8       andrew     44:         my $ident = _ident($self);
1.6       andrew     45:
1.10      andrew     46:         my $uri = URI->new_abs( $action, $base_uri_of{$ident} );
1.6       andrew     47:         $uri->query($query);
                     48:
                     49:         my $response = $ua_of{$ident}->get($uri);
                     50:
                     51:         if ( !$response->is_success ) {
                     52:             croak $response->status_line;
                     53:         }
                     54:
                     55:         return JSON::Any->jsonToObj( $response->decoded_content );
                     56:     }
1.2       andrew     57:
1.9       andrew     58:     sub location  { return shift->get( 'location',  @_ ) }
                     59:     sub speakers  { return shift->get( 'speakers',  @_ ) }
                     60:     sub talks     { return shift->get( 'talks',     @_ ) }
                     61:     sub interests { return shift->get( 'interests', @_ ) }
                     62:     sub users     { return shift->get( 'users',     @_ ) }
                     63:     sub stats     { croak 'Unused feature' }
1.6       andrew     64:
                     65:     sub DESTROY {
                     66:         my ($self) = @_;
1.8       andrew     67:         my $ident = _ident $self;
1.6       andrew     68:
                     69:         foreach my $attr_ref (@attr_refs) {
                     70:             delete $attr_ref->{$ident};
                     71:         }
                     72:
                     73:         return;
                     74:     }
                     75:
1.2       andrew     76: }
                     77:
                     78: 1;    # Magic true value required at end of module
1.1       andrew     79: __END__
                     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>