File Coverage

blib/lib/WebService/TypePad.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3              
4             WebService::TypePad - Interface to the Six Apart TypePad API
5              
6             =cut
7              
8             package WebService::TypePad;
9              
10 1     1   736 use 5.006;
  1         3  
  1         38  
11 1     1   5 use vars qw($VERSION);
  1         2  
  1         49  
12             $VERSION = '0.01_01';
13              
14 1     1   14 use strict;
  1         1  
  1         30  
15 1     1   4 use warnings;
  1         2  
  1         30  
16 1     1   5 use Carp;
  1         2  
  1         75  
17 1     1   559 use WebService::TypePad::Request;
  0            
  0            
18             use WebService::TypePad::Noun;
19             #use WebService::TypePad::List;
20              
21             =head1 SYNOPSIS
22              
23             my $typepad = WebService::TypePad->new();
24             my $user = $typepad->users->get_user(user_id => '6p1234123412341234');
25             my $user_memberships = $typepad->users->get_user_memberships(user => $user);
26              
27             =head1 METHODS
28              
29             =head2 WebService::TypePad->new(%opts)
30              
31             Create a new TypePad API client instance.
32              
33             By default, with no arguments, the returned object will be configured to use
34             the API endpoints for the main TypePad service. However, the argument
35             C can be used to override this and have the
36             client connect to a different URL. For example:
37              
38             my $typepad = WebService::TypePad->new(
39             backend_url => 'http://127.0.0.1/',
40             );
41              
42             If no arguments are supplied, the client will do unauthenticated requests to
43             the unauthenticated TypePad endpoints. To do authenticated requests, provide the
44             necessary OAuth parameters. For example:
45              
46             my $typepad = WebService::TypePad->new(
47             consumer_key => '...',
48             consumer_secret => '...',
49             access_token => '...',
50             access_token_secret => '...',
51             );
52              
53             If you need to obtain an access_token and access_token_secret, you can use
54             the methods provided by L.
55              
56             =cut
57              
58             sub new {
59             my ($class, %opts) = @_;
60              
61             my $self = bless {}, $class;
62              
63             foreach my $k (qw(backend_url)) {
64             $self->{$k} = delete $opts{$k};
65             }
66              
67             my $auth_args_count = 0;
68             foreach my $k (qw(consumer_key consumer_secret access_token access_token_secret)) {
69             $self->{$k} = delete $opts{$k};
70             $auth_args_count++ if defined($self->{$k});
71             }
72              
73             if ($auth_args_count == 4) {
74             $self->{authenticated} = 1;
75             }
76             elsif ($auth_args_count == 0) {
77             $self->{authenticated} = 0;
78             }
79             else {
80             croak "Must provide all four OAuth arguments in order to use authenticated requests";
81             }
82              
83             croak "Unsupported argument(s): ".join(', ', keys %opts) if %opts;
84              
85             unless ($self->{backend_url}) {
86             if ($self->authenticated) {
87             $self->{backend_url} = 'https://api.typepad.com/';
88             }
89             else {
90             $self->{backend_url} = 'http://api.typepad.com/';
91             }
92             }
93              
94             return $self;
95             }
96              
97             sub backend_url {
98             return $_[0]->{backend_url};
99             }
100              
101             sub authenticated {
102             return $_[0]->{authenticated};
103             }
104              
105             sub new_request {
106             my ($self) = @_;
107              
108             return WebService::TypePad::Request->new_for_api($self);
109             }
110              
111             sub consumer_key {
112             return $_[0]->{consumer_key};
113             }
114              
115             sub consumer_secret {
116             return $_[0]->{consumer_secret};
117             }
118              
119             sub access_token {
120             return $_[0]->{access_token};
121             }
122              
123             sub access_token_secret {
124             return $_[0]->{access_token_secret};
125             }
126              
127             sub oauth_parameters {
128             my ($self) = @_;
129              
130             return (
131             token => $self->access_token,
132             token_secret => $self->access_token_secret,
133             consumer_key => $self->consumer_key,
134             consumer_secret => $self->consumer_secret,
135             );
136             }
137              
138             =pod
139              
140             =head2 Noun Accessors
141              
142             Each noun in the TypePad API is represented in this library as a class.
143             An instance of a noun class can be obtained by calling the method
144             named after it on the typepad instance.
145              
146             For example, to get the "users" noun, call C<< $typepad->users >>.
147             Dashes in the names are replaced with underscores to create valid Perl
148             method names.
149              
150             A full list of nouns known to this version of the library is in
151             L.
152              
153             =cut
154              
155             # Generate an accessor for each of the known nouns.
156             {
157              
158             foreach my $noun_accessor (keys %WebService::TypePad::Noun::Nouns) {
159             my $class_name = $WebService::TypePad::Noun::Nouns{$noun_accessor};
160             my $full_accessor_name = __PACKAGE__."::".$noun_accessor;
161              
162             my $accessor_func = sub {
163             eval "use $class_name;";
164             return $class_name->_new_for_client($_[0]);
165             };
166              
167             {
168             no strict 'refs';
169             *{$full_accessor_name} = $accessor_func;
170             }
171             }
172              
173             }
174              
175             1;
176              
177             =head1 AUTHOR
178              
179             Copyright 2009 Six Apart Ltd. All rights reserved.
180              
181             =head1 LICENCE
182              
183             This package may be distributed under the same terms as Perl itself.
184