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

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

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