API-Instagram

 view release on metacpan or  search on metacpan

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

	my $opts   = shift;

	my $url      = delete $params->{url} || return [];
	my $count    = $params->{count} // 999_999_999;
	$count       = 999_999_999 if $count < 0;
	$params->{count} = $count;

	my $request = $self->_request( 'get', $url, $params, $opts );
	my $data    = $request->{data};

	# Keeps requesting if total items is less than requested
	# and still there is pagination
	while ( my $pagination = $request->{pagination} ){

		last if     @$data >= $count;
		last unless $pagination->{next_url};

		$opts->{prepared_url} = 1;
		$request = $self->_request( 'get', $pagination->{next_url}, $params, $opts );
		push @$data, @{ $request->{data} };
	}

	return @$data;
}

##############################################################
# Requests the data from the given URL with QUERY parameters #
##############################################################
sub _request {
	my ( $self, $method, $url, $params, $opts ) = @_;

	# Verifies access requirements
	unless ( defined $self->access_token ) {
		if ( !$opts->{token_not_required} or !defined $self->client_id ) {
			carp "A valid access_token is required";
			return {}
		}
	}

	# If URL is not prepared, prepares it
	unless ( $opts->{prepared_url} ){

		$url =~ s|^/||;
		$params->{access_token} = $self->access_token;

		# Prepares the URL
		my $uri = URI->new( $self->_endpoint_url );
		$uri->path_segments( $uri->path_segments, split '/', $url );
		$uri->query_form($params);
		$url = $uri->as_string;
	}

	# For debugging purposes
	print "Requesting: $url$/" if $self->_debug;

	# Treats response content
	my $res = decode_json $self->_ua->$method( $url, [], $params )->decoded_content;

	# Verifies meta node
	my $meta = $res->{meta};
	carp "$meta->{error_type}: $meta->{error_message}" if $meta->{code} ne '200';

use Data::Dumper;
# die Dumper $res;
	$res;
}

sub _request_data { shift->_request(@_)->{data} || {} }

sub _del  { shift->_request_data( 'delete', @_ ) }
sub _get  { shift->_request_data( 'get',    @_ ) }
sub _post { shift->_request_data( 'post',   @_ ) }

################################
# Returns requested cache hash #
################################
sub _cache { shift->_obj_cache->{ shift() } }


1;

__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;
		printf "Posted by %s at %s (%d likes)\n\n", $media->user->username, $media->created_time, $media->likes;

	}



( run in 2.451 seconds using v1.01-cache-2.11-cpan-f0fbb3f571b )