File Coverage

blib/lib/WebService/Instagram.pm
Criterion Covered Total %
statement 40 78 51.2
branch 1 10 10.0
condition 1 8 12.5
subroutine 13 18 72.2
pod 3 6 50.0
total 58 120 48.3


line stmt bran cond sub pod time code
1             package WebService::Instagram;
2              
3 1     1   12753 use 5.006;
  1         2  
4 1     1   3 use strict;
  1         1  
  1         19  
5 1     1   3 use warnings;
  1         4  
  1         22  
6              
7 1     1   546 use JSON;
  1         9467  
  1         3  
8 1     1   617 use LWP::UserAgent;
  1         28717  
  1         25  
9 1     1   5 use URI;
  1         2  
  1         16  
10 1     1   3 use Carp;
  1         1  
  1         47  
11 1     1   521 use Data::Dumper;
  1         4581  
  1         54  
12 1     1   5 use HTTP::Request;
  1         1  
  1         20  
13 1     1   424 use Safe::Isa;
  1         326  
  1         110  
14              
15             our $VERSION = '0.09';
16              
17 1     1   5 use constant AUTHORIZE_URL => 'https://api.instagram.com/oauth/authorize?';
  1         1  
  1         48  
18 1     1   3 use constant ACCESS_TOKEN_URL => 'https://api.instagram.com/oauth/access_token?';
  1         1  
  1         428  
19              
20             $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
21              
22             sub new {
23 1     1 0 56927 my ($class, $self) = @_;
24 1   33     10 $self->{browser} ||= LWP::UserAgent->new();
25 1 50       1926 unless ( $self->{browser}->$_isa('LWP::UserAgent') ) {
26 0         0 carp 'Browser is not a LWP::UserAgent';
27             }
28 1         23 bless $self, $class;
29 1         3 return $self;
30             }
31              
32             sub get_auth_url {
33 0     0 1   my $self = shift;
34 0 0         carp "User already authorized with code: $self->{code}" if $self->{code};
35 0           my @auth_fields = qw(client_id redirect_uri response_type);
36 0           $self->{response_type} = 'code';
37 0           foreach ( @auth_fields ) {
38 0 0         confess "ERROR: $_ required for generating authorization URL." if (!defined $_);
39             }
40             #print Dumper $self->{client_id};
41              
42 0           my $uri = URI->new( AUTHORIZE_URL );
43             $uri->query_form(
44 0           map { $_ => $self->{$_} } @auth_fields,
  0            
45             );
46              
47 0           return $uri->as_string();
48             }
49              
50             sub set_code {
51 0     0 0   my $self = shift;
52 0   0       $self->{code} = shift || confess "Code not provided";
53 0           return $self;
54             }
55              
56             sub get_access_token {
57 0     0 1   my $self = shift;
58 0           my @access_token_fields = qw(client_id redirect_uri grant_type client_secret code);
59 0           $self->{grant_type} = 'authorization_code';
60 0           foreach ( @access_token_fields ) {
61 0 0         confess "ERROR: $_ required for building access token." if (!defined $_);
62             }
63 0           my $params = {};
64 0           @$params{ @access_token_fields } = @$self{ @access_token_fields };
65            
66 0           my $uri = URI->new( ACCESS_TOKEN_URL );
67 0           my $req = new HTTP::Request POST => $uri->as_string;
68 0           $uri->query_form($params);
69 0           $req->content_type('application/x-www-form-urlencoded');
70 0           $req->content($uri->query);
71 0           my $res = from_json($self->{browser}->request($req)->content);
72             # print Dumper $res;
73             # $self->{access_token} = $res->{access_token};
74 0           return $res->{access_token};
75             }
76              
77             sub set_access_token {
78 0     0 0   my $self = shift;
79 0   0       $self->{access_token} = shift || die "No access token provided";
80             }
81              
82             sub request {
83 0     0 1   my ( $self, $url, $params ) = @_;
84 0 0         croak "access_token not passed" unless defined $self->{access_token} ;
85 0           $params->{access_token} = $self->{access_token};
86 0           my $uri = URI->new( $url );
87 0           $uri->query_form($params);
88 0           my $response = $self->{browser}->get($uri->as_string);
89 0           my $ret = $response->decoded_content;
90 0           return decode_json $ret;
91             }
92              
93             1; # End of WebService::Instagram
94             __END__