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

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

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