File Coverage

blib/lib/API/Twitter.pm
Criterion Covered Total %
statement 12 41 29.2
branch 0 2 0.0
condition n/a
subroutine 4 6 66.6
pod 0 2 0.0
total 16 51 31.3


line stmt bran cond sub pod time code
1             # ABSTRACT: Twitter.com API Client
2             package API::Twitter;
3              
4 1     1   1450 use Data::Object::Class;
  1         24529  
  1         9  
5 1     1   2268 use Data::Object::Signatures;
  1         254222  
  1         12  
6              
7 1         5 use Data::Object::Library qw(
8             Str
9 1     1   9177 );
  1         2  
10              
11 1     1   3114 use Net::OAuth ();
  1         1259  
  1         1058  
12              
13             extends 'API::Client';
14              
15             our $VERSION = '0.05'; # VERSION
16              
17             our $DEFAULT_URL = "https://api.twitter.com";
18              
19             $Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A;
20              
21             our $VERSION = '0.05'; # VERSION
22              
23             # ATTRIBUTES
24              
25             has consumer_key => (
26             is => 'ro',
27             isa => Str,
28             required => 1,
29             );
30              
31             has consumer_secret => (
32             is => 'rw',
33             isa => Str,
34             required => 1,
35             );
36              
37             has access_token => (
38             is => 'rw',
39             isa => Str,
40             required => 0,
41             );
42              
43             has access_token_secret => (
44             is => 'rw',
45             isa => Str,
46             required => 0,
47             );
48              
49             has oauth_type => (
50             is => 'rw',
51             isa => Str,
52             default => 'protected resource',
53             required => 0,
54             );
55              
56             # DEFAULTS
57              
58             has '+identifier' => (
59             default => 'API::Twitter (Perl)',
60             required => 0,
61             );
62              
63             has '+url' => (
64             default => $DEFAULT_URL,
65             required => 0,
66             );
67              
68             has '+version' => (
69             default => 1.1,
70             required => 0,
71             );
72              
73             # CONSTRUCTION
74              
75             after BUILD => method {
76              
77             my $identifier = $self->identifier;
78             my $version = $self->version;
79             my $agent = $self->user_agent;
80             my $url = $self->url;
81              
82             $agent->transactor->name($identifier);
83              
84             $url->path("/$version");
85              
86             return $self;
87              
88             };
89              
90             # METHODS
91              
92 0     0 0   method PREPARE ($ua, $tx, %args) {
  0            
  0            
93              
94 0           my $req = $tx->req;
95 0           my $headers = $req->headers;
96 0           my $params = $req->params->to_hash;
97 0           my $url = $req->url;
98              
99             # default headers
100 0           $headers->header('Content-Type' => 'application/json');
101              
102             # append path suffix
103 0 0         $url->path("@{[$url->path]}.json") if $url->path !~ /\.json$/;
  0            
104              
105             # oauth data
106 0           my $consumer_key = $self->consumer_key;
107 0           my $consumer_secret = $self->consumer_secret;
108 0           my $access_token = $self->access_token;
109 0           my $access_token_secret = $self->access_token_secret;
110              
111             # oauth variables
112 0           my $oauth_consumer_key = $consumer_key;
113 0           my $oauth_nonce = Digest::SHA::sha1_base64(time . $$ . rand);
114 0           my $oauth_signature_method = 'HMAC-SHA1',
115             my $oauth_timestamp = time,
116             my $oauth_token = $access_token,
117             my $oauth_version = '1.0';
118              
119             # oauth object
120 0           my $base = $url->clone; $base->query(undef);
  0            
121 0           my $oauth = Net::OAuth->request($self->oauth_type)->new(%$params,
122             version => '1.0',
123             consumer_key => $consumer_key,
124             consumer_secret => $consumer_secret,
125             request_method => uc($req->method),
126             request_url => $base,
127             signature_method => 'HMAC-SHA1',
128             timestamp => time,
129             token => $access_token,
130             token_secret => $access_token_secret,
131             nonce => Digest::SHA::sha1_base64(time . $$ . rand),
132             );
133              
134             # oauth signature
135 0           $oauth->sign;
136              
137             # authorization header
138 0           $headers->header('Authorization' => $oauth->to_authorization_header);
139              
140             }
141              
142 0     0 0   method resource (@segments) {
  0            
  0            
143              
144             # build new resource instance
145 0           my $instance = __PACKAGE__->new(
146             debug => $self->debug,
147             fatal => $self->fatal,
148             retries => $self->retries,
149             timeout => $self->timeout,
150             user_agent => $self->user_agent,
151             identifier => $self->identifier,
152             version => $self->version,
153             access_token => $self->access_token,
154             access_token_secret => $self->access_token_secret,
155             consumer_key => $self->consumer_key,
156             consumer_secret => $self->consumer_secret,
157             );
158              
159             # resource locator
160 0           my $url = $instance->url;
161              
162             # modify resource locator if possible
163 0           $url->path(join '/', $self->url->path, @segments);
164              
165             # return resource instance
166 0           return $instance;
167              
168             }
169              
170             1;
171              
172             __END__