API-Instagram

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

    Search APIs

VERSION
    version 0.013

SYNOPSIS
            use API::Instagram;

            my $instagram = API::Instagram->new({
                            client_id     => $client_id,
                            client_secret => $client_secret,
                            redirect_uri  => 'http://localhost',
            });

            # Authenticated user feed
            my $my_user = $instagram->user;
            my $feed    = $my_user->feed( count => 5 );

            for my $media ( @$feed ) {

                    printf "Caption: %s\n", $media->caption;
                    printf "Posted by %s at %s (%d likes)\n\n", $media->user->username, $media->created_time, $media->likes;

            }

DESCRIPTION
    This module implements an OO interface to Instagram REST API.

  Authentication
    Instagram API uses the OAuth2 for authentication, requering a
    "client_id" and "client_secret". See
    <http://instagr.am/developer/register/> for details.

   Authorize
    Get the AUTH URL to authenticate.

            use API::Instagram;

            my $instagram = API::Instagram->new({
                            client_id     => 'xxxxxxxxxx',
                            client_secret => 'xxxxxxxxxx',
                            redirect_uri  => 'http://localhost',
                            scope         => 'basic',
                            response_type => 'code',
                            granty_type   => 'authorization_code',
            });

            print $instagram->get_auth_url;

   Authenticate
    After authorization, Instagram will redirected the user to the URL in

README  view on Meta::CPAN

    the authenticated user credentials.

            $instagram->access_token( $access_token );
            my $me = $instagram->user;
            print $me->full_name;

METHODS
  new
            my $instagram = API::Instagram->new({
                            client_id     => $client_id,
                            client_secret => $client_secret,
                            redirect_uri  => 'http://localhost',
                            scope         => 'basic',
                            response_type => 'code',
                            granty_type   => 'authorization_code',
                            no_cache      => 1,
            });

    Returns an API::Instagram object.

    Set "client_id", "client_secret" and "redirect_uri" with the ones
    registered to your application. See
    <http://instagram.com/developer/clients/manage/>.

    "scope" is the scope of access. See
    <http://instagram.com/developer/authentication/#scope>.

    "response_type" and "granty_type" do no vary. See
    <http://instagram.com/developer/authentication/>.

    By default, API::Instagram caches created objects to avoid duplications.

README  view on Meta::CPAN

    parameter.

  instance
            my $instagram = API::Instagram->instance;
            print $instagram->user->full_name;

            or

            my $instagram = API::Instagram->instance({
                            client_id     => $client_id,
                            client_secret => $client_secret,
                            redirect_uri  => 'http://localhost',
            });

    Returns the singleton instance of API::Instagram.

    Note: if no instance was created before, creates a new API::Instagram
    object initialized with arguments provided and then returns it.

  get_auth_url
            my $auth_url = $instagram->get_auth_url;

lib/API/Instagram.pm  view on Meta::CPAN

use Furl;

use API::Instagram::User;
use API::Instagram::Location;
use API::Instagram::Tag;
use API::Instagram::Media;
use API::Instagram::Media::Comment;
use API::Instagram::Search;

has client_id         => ( is => 'ro', required => 1 );
has client_secret     => ( is => 'ro', required => 1 );
has redirect_uri      => ( is => 'ro', required => 1 );
has scope             => ( is => 'ro', default => sub { 'basic' } );
has response_type     => ( is => 'ro', default => sub { 'code'  } );
has grant_type        => ( is => 'ro', default => sub { 'authorization_code' } );
has code              => ( is => 'rw', isa => sub { confess "Code not provided"        unless $_[0] } );
has access_token      => ( is => 'rw', isa => sub { confess "No access token provided" unless $_[0] } );
has no_cache          => ( is => 'rw', default => sub { 0 } );

has _ua               => ( is => 'ro', default => sub { Furl->new() } );
has _obj_cache        => ( is => 'ro', default => sub { { User => {}, Media => {}, Location => {}, Tag => {}, 'Media::Comment' => {} } } );

lib/API/Instagram.pm  view on Meta::CPAN


	my $uri = URI->new( $self->_authorize_url );
	$uri->query_form( map { $_ => $self->$_ } @auth_fields );
	$uri->as_string();
}


sub get_access_token {
	my $self = shift;

	my @access_token_fields = qw(client_id redirect_uri grant_type client_secret code);
	for ( @access_token_fields ) {
		carp "ERROR: $_ required for generating access token." and return unless defined $self->$_;
	}

	my $data = { map { $_ => $self->$_ } @access_token_fields };
	my $json = $self->_request( 'post', $self->_access_token_url, $data, { token_not_required => 1 } );

	wantarray ? ( $json->{access_token}, $self->user( $json->{user} ) ) : $json->{access_token};
}

lib/API/Instagram.pm  view on Meta::CPAN

__END__

=pod

=encoding UTF-8

=head1 NAME

API::Instagram - Object Oriented Interface for the Instagram REST and Search APIs

=for Pod::Coverage client_id client_secret grant_type no_cache redirect_uri response_type scope BUILD

=for HTML <a href="https://travis-ci.org/gabrielmad/API-Instagram"><img src="https://travis-ci.org/gabrielmad/API-Instagram.svg?branch=build%2Fmaster"></a>

=for HTML <a href='https://coveralls.io/r/gabrielmad/API-Instagram?branch=build%2Fmaster'><img src='https://coveralls.io/repos/gabrielmad/API-Instagram/badge.png?branch=build%2Fmaster' alt='Coverage Status' /></a>

=head1 VERSION

version 0.013

=head1 SYNOPSIS

	use API::Instagram;

	my $instagram = API::Instagram->new({
			client_id     => $client_id,
			client_secret => $client_secret,
			redirect_uri  => 'http://localhost',
	});

	# Authenticated user feed
	my $my_user = $instagram->user;
	my $feed    = $my_user->feed( count => 5 );

	for my $media ( @$feed ) {

		printf "Caption: %s\n", $media->caption;

lib/API/Instagram.pm  view on Meta::CPAN


	}

=head1 DESCRIPTION

This module implements an OO interface to Instagram REST API.

=head2 Authentication

Instagram API uses the OAuth2 for authentication, requering a C<client_id> and
C<client_secret>. See L<http://instagr.am/developer/register/> for details.

=head3 Authorize

Get the AUTH URL to authenticate.

	use API::Instagram;

	my $instagram = API::Instagram->new({
			client_id     => 'xxxxxxxxxx',
			client_secret => 'xxxxxxxxxx',
			redirect_uri  => 'http://localhost',
			scope         => 'basic',
			response_type => 'code',
			granty_type   => 'authorization_code',
	});

	print $instagram->get_auth_url;

=head3 Authenticate

lib/API/Instagram.pm  view on Meta::CPAN

	$instagram->access_token( $access_token );
	my $me = $instagram->user;
	print $me->full_name;

=head1 METHODS

=head2 new

	my $instagram = API::Instagram->new({
			client_id     => $client_id,
			client_secret => $client_secret,
			redirect_uri  => 'http://localhost',
			scope         => 'basic',
			response_type => 'code',
			granty_type   => 'authorization_code',
			no_cache      => 1,
	});

Returns an L<API::Instagram> object.

Set C<client_id>, C<client_secret> and C<redirect_uri> with the ones registered
to your application. See L<http://instagram.com/developer/clients/manage/>.

C<scope> is the scope of access. See L<http://instagram.com/developer/authentication/#scope>.

C<response_type> and C<granty_type> do no vary. See L<http://instagram.com/developer/authentication/>.

By default, L<API::Instagram> caches created objects to avoid duplications. You can disable
this feature setting a true value to C<no_chace> parameter.

=head2 instance

	my $instagram = API::Instagram->instance;
	print $instagram->user->full_name;

	or

	my $instagram = API::Instagram->instance({
			client_id     => $client_id,
			client_secret => $client_secret,
			redirect_uri  => 'http://localhost',
	});

Returns the singleton instance of L<API::Instagram>.

Note: if no instance was created before, creates a new L<API::Instagram> object initialized with arguments provided and then returns it.

=head2 get_auth_url

	my $auth_url = $instagram->get_auth_url;

t/00-comment.t  view on Meta::CPAN

use warnings;
use Test::MockObject::Extends; 

use JSON;
use API::Instagram;
use Test::More tests => 11;

my $api = Test::MockObject::Extends->new(
	API::Instagram->new({
			client_id     => '123',
			client_secret => '456',
			redirect_uri  => 'http://localhost',
            no_cache      => 1,
	})
);

my $data = join '', <DATA>;
my $json = decode_json $data;
$api->mock('_request', sub { $json });

my $media = $api->media(123);

t/00-instagram.t  view on Meta::CPAN


my $data = join '', <DATA>;
my $ua   = Test::MockObject::Extends->new( Furl->new() );
my $res  = Test::MockObject::Extends->new( Furl::Response->new( 1, 200, 'OK', {}, $data) );

$ua->mock('get',  sub { $res });
$ua->mock('post', sub { $res });

my $api = API::Instagram->new({
			client_id     => '123',
			client_secret => '456',
			redirect_uri  => 'http://localhost',
            no_cache      => 1,
            _ua           => $ua,
});


isa_ok( $api, 'API::Instagram');
ok $api->get_auth_url;
is $api->get_access_token, undef;

t/00-location.t  view on Meta::CPAN

use warnings;
use Test::MockObject::Extends; 

use JSON;
use API::Instagram;
use Test::More tests => 6;

my $api = Test::MockObject::Extends->new(
	API::Instagram->new({
			client_id     => '123',
			client_secret => '456',
			redirect_uri  => 'http://localhost',
	})
);

my $data = join '', <DATA>;
my $json = decode_json $data;
$api->mock('_request', sub { $json });
$api->mock('_get_list', sub { [] });

my $location = $api->location('1');

t/00-media.t  view on Meta::CPAN

use strict;
use warnings;
use Test::MockObject::Extends; 

use JSON;
use API::Instagram;
use Test::More tests => 27;

my $api = Test::MockObject::Extends->new(
	API::Instagram->new({
			client_id     => '123', client_secret => '456', redirect_uri  => 'http://localhost', no_cache      => 1, })
);

my $data = join '', <DATA>;
my $json = decode_json $data;
$api->mock('_request', sub { $json });
$api->mock('_get_list', sub { {} });

# First Object
my $media = $api->media(3);
isa_ok( $media, 'API::Instagram::Media' );

t/00-search.t  view on Meta::CPAN

use warnings;
use Test::MockObject::Extends; 

use JSON;
use API::Instagram;
use Test::More tests => 4;

my $api = Test::MockObject::Extends->new(
	API::Instagram->new({
			client_id     => '123',
			client_secret => '456',
			redirect_uri  => 'http://localhost',
	})
);

my $data = join '', <DATA>;
my $json = decode_json $data;
$api->mock('_request', sub { $json });

my $search = $api->search('tag');
isa_ok( $search, 'API::Instagram::Search' );

t/00-tag.t  view on Meta::CPAN

use warnings;
use Test::MockObject::Extends; 

use JSON;
use API::Instagram;
use Test::More tests => 5;

my $api = Test::MockObject::Extends->new(
	API::Instagram->new({
			client_id     => '123',
			client_secret => '456',
			redirect_uri  => 'http://localhost',
	})
);

my $data = join '', <DATA>;
my $json = decode_json $data;
$api->mock('_request', sub { $json });
$api->mock('_get_list', sub { [] });

my $tag = $api->tag('nofilter');

t/00-user.t  view on Meta::CPAN

use warnings;
use Test::MockObject::Extends; 

use JSON;
use API::Instagram;
use Test::More tests => 19;

my $api = Test::MockObject::Extends->new(
	API::Instagram->new({
			client_id     => '123',
			client_secret => '456',
			redirect_uri  => 'http://localhost',
            no_cache      => 1
	})
);

my $data = join '', <DATA>;
my $json = decode_json $data;
$api->mock('_request', sub { $json });
$api->mock('_get_list', sub { [] });

t/01-location.t  view on Meta::CPAN

use warnings;
use Test::MockObject::Extends; 

use JSON;
use API::Instagram;
use Test::More tests => 4;

my $api = Test::MockObject::Extends->new(
	API::Instagram->new({
			client_id     => '123',
			client_secret => '456',
			redirect_uri  => 'http://localhost',
	})
);

my $data = join '', <DATA>;
my $json = decode_json $data;
$api->mock('_request', sub { $json });
$api->mock('_get_list', sub { [] });

my $location = $api->location( $json->{data} );

t/01-media.t  view on Meta::CPAN

use warnings;
use Test::MockObject::Extends; 

use JSON;
use API::Instagram;
use Test::More tests => 13;

my $api = Test::MockObject::Extends->new(
	API::Instagram->new({
			client_id     => '123',
			client_secret => '456',
			redirect_uri  => 'http://localhost',
            no_cache      => 1,
	})
);

my $data = join '', <DATA>;
my $json = decode_json $data;
$api->mock('_request', sub { $json });
$api->mock('_get_list', sub { [] });

t/01-search.t  view on Meta::CPAN

use warnings;
use Test::MockObject::Extends; 

use JSON;
use API::Instagram;
use Test::More tests => 1;

my $api = Test::MockObject::Extends->new(
	API::Instagram->new({
			client_id     => '123',
			client_secret => '456',
			redirect_uri  => 'http://localhost',
	})
);

my $data = join '', <DATA>;
my $json = decode_json $data;
$api->mock('_request', sub { $json });

my $search = eval { $api->search('car') };
is $search, undef;

t/01-user.t  view on Meta::CPAN

use warnings;
use Test::MockObject::Extends; 

use JSON;
use API::Instagram;
use Test::More tests => 6;

my $api = Test::MockObject::Extends->new(
	API::Instagram->new({
			client_id     => '123',
			client_secret => '456',
			redirect_uri  => 'http://localhost',
            no_cache      => 1
	})
);

my $data = join '', <DATA>;
my $json = decode_json $data;
$api->mock('_request', sub { $json });
$api->mock('_get_list', sub { [] });

t/02-user.t  view on Meta::CPAN

use warnings;
use Test::MockObject::Extends; 

use JSON;
use API::Instagram;
use Test::More tests => 6;

my $api = Test::MockObject::Extends->new(
	API::Instagram->new({
			client_id     => '123',
			client_secret => '456',
			redirect_uri  => 'http://localhost',
            no_cache      => 1
	})
);

my $data = join '', <DATA>;
my $json = decode_json $data;
$api->mock('_request', sub { $json });
$api->mock('_get_list', sub { [] });

t/03-user.t  view on Meta::CPAN

use warnings;
use Test::MockObject::Extends; 

use JSON;
use API::Instagram;
use Test::More tests => 2;

my $api = Test::MockObject::Extends->new(
	API::Instagram->new({
			client_id     => '123',
			client_secret => '456',
			redirect_uri  => 'http://localhost',
            no_cache      => 1
	})
);

my $data = join '', <DATA>;
my $json = decode_json $data;
$api->mock('_request', sub { $json });
$api->mock('_get_list', sub { [] });

t/04-user.t  view on Meta::CPAN

use warnings;
use Test::MockObject::Extends; 

use JSON;
use API::Instagram;
use Test::More tests => 5;

my $api = Test::MockObject::Extends->new(
	API::Instagram->new({
			client_id     => '123',
			client_secret => '456',
			redirect_uri  => 'http://localhost',
            no_cache      => 1
	})
);

my $data = join '', <DATA>;
my $json = decode_json $data;
$api->mock('_request', sub { $json });
$api->mock('_post',    sub { $json });



( run in 1.732 second using v1.01-cache-2.11-cpan-39bf76dae61 )