File Coverage

blib/lib/WWW/TypePad/CmdLine.pm
Criterion Covered Total %
statement 21 57 36.8
branch 0 16 0.0
condition 0 9 0.0
subroutine 7 11 63.6
pod 1 4 25.0
total 29 97 29.9


line stmt bran cond sub pod time code
1             package WWW::TypePad::CmdLine;
2              
3 1     1   38725 use strict;
  1         3  
  1         39  
4 1     1   51 use 5.008_001;
  1         4  
  1         209  
5             our $VERSION = '0.02';
6              
7 1     1   1274 use Config::Tiny;
  1         1673  
  1         239  
8 1     1   1038 use File::HomeDir;
  1         9159  
  1         142  
9 1     1   18 use File::Spec;
  1         3  
  1         35  
10 1     1   1996 use Getopt::Long qw( :config pass_through );
  1         43433  
  1         9  
11 1     1   1838 use WWW::TypePad;
  1         325739  
  1         765  
12              
13             our $Config = File::Spec->catfile(
14             File::HomeDir->my_data, '.www-typepad-info'
15             );
16             our $ConfigObj;
17              
18             GetOptions( 'config=s' => \$Config );
19              
20             sub config_file {
21 0     0 0   my $class = shift;
22 0           return $Config;
23             }
24              
25             sub config {
26 0     0 0   my $class = shift;
27 0   0       return $ConfigObj ||= Config::Tiny->read( $class->config_file );
28             }
29              
30             sub initialize {
31 0     0 1   my $class = shift;
32 0           my( %p ) = @_;
33              
34 0           my $tp;
35              
36 0           my $config = $class->config;
37 0 0 0       if ( !defined $config && $p{requires_auth} ) {
    0          
38 0 0         $tp = WWW::TypePad->new(
39             ( $ENV{TP_HOST} ? ( host => $ENV{TP_HOST} ) : () ),
40             consumer_key => $ENV{TP_CONSUMER_KEY},
41             consumer_secret => $ENV{TP_CONSUMER_SECRET},
42             );
43              
44 0 0 0       die "TP_CONSUMER_KEY and TP_CONSUMER_SECRET required in ENV"
45             unless $tp->consumer_key && $tp->consumer_secret;
46              
47 0           $class->new_config( $tp );
48             } elsif ( $p{requires_auth} ) {
49 0 0         $tp = WWW::TypePad->new(
50             ( $config->{app}{host} ? ( host => $config->{app}{host} ) : () ),
51             consumer_key => $config->{app}{consumer_key},
52             consumer_secret => $config->{app}{consumer_secret},
53             access_token => $config->{auth}{access_token},
54             access_token_secret => $config->{auth}{access_token_secret},
55             );
56             } else {
57 0           $tp = WWW::TypePad->new;
58             }
59              
60 0           return $tp;
61             }
62              
63             sub new_config {
64 0     0 0   my $class = shift;
65 0           my( $tp ) = @_;
66              
67 0           $ConfigObj = Config::Tiny->new;
68              
69 0           print <
70              
71             Welcome to WWW::TypePad! Before we get started, we'll need to link our
72             local configuration to your TypePad account.
73              
74             First, you'll need to authorize this application in your web browser.
75              
76             MSG
77              
78 0           my $auth_uri = $tp->oauth->get_authorization_url(
79             callback => 'oob',
80             );
81              
82 0 0         if ( -x '/usr/bin/open' ) {
83 0           print <
84             We're opening this URL in your web browser:
85              
86             $auth_uri
87             MSG
88 0           system 'open', $auth_uri;
89             } else {
90 0           print <
91             Open this URL in your web browser:
92              
93             $auth_uri
94             MSG
95             }
96              
97 0           print <
98              
99             When you've allowed access to this application, you'll get a verifier
100             code that we'll need to complete the handshake.
101              
102             MSG
103              
104 0           print "Enter the verifier code: ";
105 0           chomp( my $verifier = );
106            
107 0           my( $access_token, $access_token_secret ) =
108             $tp->oauth->request_access_token( verifier => $verifier );
109 0           $tp->access_token( $access_token );
110 0           $tp->access_token_secret( $access_token_secret );
111            
112 0           my $obj = $tp->users->get( '@self' );
113 0 0         die 'Request for @self gave us empty result'
114             unless $obj;
115              
116 0           print <
117              
118             Great! We've identified you as "$obj->{displayName}".
119              
120             We're going to save your configuration in the following file, in case you'd
121             like to access or change it in the future:
122              
123             $Config
124              
125             MSG
126              
127 0 0         $ConfigObj->{app} = {
128             ( $tp->host ne 'api.typepad.com' ? ( host => $tp->host ) : () ),
129             consumer_key => $tp->consumer_key,
130             consumer_secret => $tp->consumer_secret,
131             };
132              
133 0           $ConfigObj->{auth} = {
134             access_token => $access_token,
135             access_token_secret => $access_token_secret,
136             };
137              
138 0           $ConfigObj->write( $class->config_file );
139             }
140              
141             1;
142             __END__