File Coverage

blib/lib/WebService/Instagram.pm
Criterion Covered Total %
statement 33 76 43.4
branch 0 10 0.0
condition 0 5 0.0
subroutine 11 17 64.7
pod 3 6 50.0
total 47 114 41.2


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