File Coverage

blib/lib/Net/OpenSocial/Client/HTTPRequestBuilder/OAuth.pm
Criterion Covered Total %
statement 24 43 55.8
branch 0 10 0.0
condition 0 5 0.0
subroutine 8 9 88.8
pod 1 1 100.0
total 33 68 48.5


line stmt bran cond sub pod time code
1             package Net::OpenSocial::Client::HTTPRequestBuilder::OAuth;
2              
3 1     1   4 use Any::Moose;
  1         3  
  1         41  
4             with 'Net::OpenSocial::Client::HTTPRequestBuilder';
5              
6 1     1   1625 use OAuth::Lite::Consumer;
  1         65784  
  1         10  
7 1     1   43 use OAuth::Lite::AuthMethod qw(URL_QUERY);
  1         3  
  1         57  
8 1     1   5 use OAuth::Lite::Token;
  1         2  
  1         7  
9 1     1   24 use HTTP::Headers;
  1         2  
  1         11  
10 1     1   22 use HTTP::Request;
  1         2  
  1         9  
11 1     1   26 use bytes ();
  1         2  
  1         318  
12              
13             has 'consumer_secret' => (
14             is => 'ro',
15             isa => 'Str',
16             required => 1,
17             );
18              
19             has 'consumer_key' => (
20             is => 'ro',
21             isa => 'Str',
22             required => 1,
23             );
24              
25             has 'token' => (
26             is => 'ro',
27             isa => 'OAuth::Lite::Token',
28             );
29              
30             has 'requestor' => (
31             is => 'ro',
32             isa => 'Str',
33             );
34              
35             sub build_request {
36 0     0 1   my ( $self, %args ) = @_;
37              
38 0           my $method = $args{method};
39 0           my $url = $args{url};
40 0           my $params = $args{params};
41 0           my $content_type = $args{content_type};
42 0           my $content = $args{content};
43 0           my $container = $args{container};
44 0           my $token = $self->token;
45              
46 0   0       $params ||= {};
47 0 0         $params->{xoauth_requestor_id} = $self->requestor
48             if $self->requestor;
49              
50 0 0         my $use_request_body_hash = $container->use_request_body_hash ? 1 : 0;
51              
52             # now OAuth::Lite 1.16 supports Request Body Hash
53 0           my $consumer = OAuth::Lite::Consumer->new(
54             consumer_key => $self->consumer_key,
55             consumer_secret => $self->consumer_secret,
56             auth_method => URL_QUERY,
57             use_request_body_hash => $use_request_body_hash,
58             );
59              
60 0           my %oauth_args = (
61             method => $method,
62             url => $url,
63             params => $params,
64             );
65 0 0         $oauth_args{token} = $token if $token;
66 0 0 0       if ( $method eq 'POST' || $method eq 'PUT' ) {
67 0 0         $oauth_args{content} = $content if $content;
68 0           $oauth_args{headers} = [ 'Content-Type' => $content_type ];
69             }
70 0           my $http_req = $consumer->gen_oauth_request(%oauth_args);
71 0           return $http_req;
72             }
73              
74 1     1   5 no Any::Moose;
  1         7  
  1         82  
75             __PACKAGE__->meta->make_immutable;
76             1;
77              
78             =head1 NAME
79              
80             Net::OpenSocial::Client::HTTPRequestBuilder::OAuth - OAuth request builder
81              
82             =head1 SYNOPSIS
83              
84             # 3legged
85             # get access token beforehand.
86             my $builder = Net::OpenSocial::Client::HTTPRequestBuilder::OAuth->new(
87             consumer_key => $consumer_key,
88             consumer_secret => $consumer_secret,
89             token => $access_token,
90             );
91              
92             # or 2legged
93             my $builder = Net::OpenSocial::Client::HTTPRequestBuilder::OAuth->new(
94             consumer_key => $consumer_key,
95             consumer_secret => $consumer_secret,
96             requestor => $xoauth_requestor_id,
97             );
98              
99             my $http_request = $builder->build_request();
100              
101             =head1 DESCRIPTION
102              
103             =head1 METHODS
104              
105             =head2 build_request
106              
107             =head1 AUTHOR
108              
109             Lyo Kato, Elyo.kato@gmail.comE
110              
111             =head1 COPYRIGHT AND LICENSE
112              
113             Copyright (C) 2009 by Lyo Kato
114              
115             This library is free software; you can redistribute it and/or modify
116             it under the same terms as Perl itself, either Perl version 5.8.8 or,
117             at your option, any later version of Perl 5 you may have available.
118              
119             =cut
120