Skip to content

Commit

Permalink
Add get all follower method
Browse files Browse the repository at this point in the history
  • Loading branch information
yukihirai0505 committed Oct 23, 2017
1 parent c27d57b commit c802ed3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 21 deletions.
73 changes: 61 additions & 12 deletions src/main/scala/com/yukihirai0505/sInstagram/Instagram.scala
Expand Up @@ -21,6 +21,7 @@ import com.yukihirai0505.sInstagram.utils.PaginationHelper
import dispatch._
import play.api.libs.json.Reads

import scala.concurrent.{ExecutionContextExecutor, Future}
import scala.language.postfixOps


Expand Down Expand Up @@ -151,21 +152,11 @@ class Instagram(auth: Auth) {
request(Verbs.GET, apiPath, Some(params))
}

/**
* Get the list of 'users' the authenticated user follows.
*
* @return a UserFeed object.
* if any error occurs.
*/
def getUserFollowList: Future[Response[UserFeed]] = {
getUserFollowListNextPage()
}

/**
* Get the next page for list of 'users' the authenticated user follows.
*
*/
def getUserFollowListNextPage(cursor: Option[String] = None): Future[Response[UserFeed]] = {
def getUserFollowList(cursor: Option[String] = None): Future[Response[UserFeed]] = {
val params: Map[String, Option[String]] = Map(
QueryParam.CURSOR -> cursor
)
Expand All @@ -183,6 +174,35 @@ class Instagram(auth: Auth) {
request(Verbs.GET, page.apiPath, Option(page.queryStringParams))
}

/**
* Get all followers the authenticated
*
* @param executionContextExecutor
* @return
*/
def getUserAllFollowsList(implicit executionContextExecutor: ExecutionContextExecutor): Future[Seq[User]] = {
def getFollowers(followers: Seq[User], pagination: Option[Pagination]): Future[Seq[User]] = {
pagination.flatMap(_.nextUrl) match {
case Some(_) =>
getUserFollowListNextPageByPage(pagination.get).flatMap {
case Response(data, _) =>
val d = data.get
getFollowers(d.data.getOrElse(Seq.empty[User]) ++ followers, d.pagination)
case _ => Future successful followers
}
case None => Future successful followers
}
}

getUserFollowList().flatMap { response =>
response.data match {
case Some(data) =>
getFollowers(data.data.getOrElse(Seq.empty), data.pagination)
case None => Future successful Seq.empty
}
}
}

/**
* Get the authenticated user's list of media they've liked.
*
Expand Down Expand Up @@ -404,6 +424,35 @@ class Instagram(auth: Auth) {
getUserFeedInfoNextPage(pagination)
}

/**
* Get all followers the authenticated
*
* @param executionContextExecutor
* @return
*/
def getUserAllFollowersList(implicit executionContextExecutor: ExecutionContextExecutor): Future[Seq[User]] = {
def getFollowers(followers: Seq[User], pagination: Option[Pagination]): Future[Seq[User]] = {
pagination.flatMap(_.nextUrl) match {
case Some(_) =>
getUserFollowedByListNextPage(pagination.get).flatMap {
case Response(data, _) =>
val d = data.get
getFollowers(d.data.getOrElse(Seq.empty[User]) ++ followers, d.pagination)
case _ => Future successful followers
}
case None => Future successful followers
}
}

getUserFollowedByList().flatMap { response =>
response.data match {
case Some(data) =>
getFollowers(data.data.getOrElse(Seq.empty), data.pagination)
case None => Future successful Seq.empty
}
}
}

/**
* Get information about a media object.
*
Expand Down Expand Up @@ -528,7 +577,7 @@ class Instagram(auth: Auth) {
val params: Map[String, Option[String]] = Map(
QueryParam.LATITUDE -> Some(latitude.toString),
QueryParam.LONGITUDE -> Some(longitude.toString),
QueryParam.DISTANCE -> Option(distance.mkString)
QueryParam.DISTANCE -> distance.map(_.toString)
)
request(Verbs.GET, Methods.MEDIA_SEARCH, Some(params))
}
Expand Down
26 changes: 17 additions & 9 deletions src/test/scala/com/yukihirai0505/sInstagram/InstagramSpec.scala
Expand Up @@ -5,7 +5,7 @@ import com.yukihirai0505.sInstagram.exceptions.OAuthException
import com.yukihirai0505.sInstagram.model.Relationship
import com.yukihirai0505.sInstagram.responses.auth.AccessToken
import com.yukihirai0505.sInstagram.responses.comments.MediaCommentsFeed
import com.yukihirai0505.sInstagram.responses.common.{NoDataResponse, Pagination}
import com.yukihirai0505.sInstagram.responses.common.{NoDataResponse, Pagination, User}
import com.yukihirai0505.sInstagram.responses.likes.LikesFeed
import com.yukihirai0505.sInstagram.responses.locations.{LocationInfo, LocationSearchFeed}
import com.yukihirai0505.sInstagram.responses.media.{MediaFeed, MediaInfoFeed}
Expand All @@ -15,9 +15,9 @@ import com.yukihirai0505.sInstagram.responses.users.basicinfo.UserInfo
import com.yukihirai0505.sInstagram.responses.users.feed.UserFeed
import org.scalatest.matchers.{BePropertyMatchResult, BePropertyMatcher}
import org.scalatest.{FlatSpec, Matchers}
import org.slf4j.Logger
import play.api.libs.iteratee.Execution

import scala.concurrent.Await
import scala.concurrent.{Await, ExecutionContext, ExecutionContextExecutor}
import scala.concurrent.duration._
import scala.io.Source
import scala.language.postfixOps
Expand Down Expand Up @@ -54,6 +54,7 @@ class InstagramSpec extends FlatSpec with Matchers {
val instagram = new Instagram(auth)
val wrongToken = AccessToken("this is a bullshit access token")
val nonePage = new Pagination(None, None, None, None, None, None)
implicit val executionContextExecutor: ExecutionContextExecutor = Execution.trampoline

var userId: Option[String] = None
var mediaId: Option[String] = None
Expand Down Expand Up @@ -82,19 +83,20 @@ class InstagramSpec extends FlatSpec with Matchers {
}

"getUserFollowList" should "return a Some[UserFeed]" in {
val request = Await.result(instagram.getUserFollowList, 10 seconds)
request should be(anInstanceOf[Response[UserFeed]])
}

"getUserFollowListNextPage" should "return a Some[UserFeed]" in {
val request = Await.result(instagram.getUserFollowListNextPage(), 10 seconds)
val request = Await.result(instagram.getUserFollowList(), 10 seconds)
request should be(anInstanceOf[Response[UserFeed]])
}

"getUserFollowListNextPageByPage" should "return a OAuthException by None pagination" in {
an[OAuthException] should be thrownBy Await.result(instagram.getUserFollowListNextPageByPage(nonePage), 10 seconds)
}

"getUserAllFollowsList" should "return Seq[User]" in {
val request = Await.result(instagram.getUserAllFollowsList, 10 seconds)
if (request.isEmpty) request.isEmpty should be(true)
else request should be(anInstanceOf[Seq[User]])
}

"getUserLikedMediaFeed" should "return a Some[MediaFeed]" in {
val request = Await.result(instagram.getUserLikedMediaFeed(), 10 seconds)
request should be(anInstanceOf[Response[MediaFeed]])
Expand Down Expand Up @@ -181,6 +183,12 @@ class InstagramSpec extends FlatSpec with Matchers {
an[OAuthException] should be thrownBy Await.result(instagram.getUserFollowedByListNextPage(nonePage), 10 seconds)
}

"getUserAllFollowersList" should "return Seq[User]]" in {
val request = Await.result(instagram.getUserAllFollowersList, 10 seconds)
if (request.isEmpty) request.isEmpty should be(true)
else request should be(anInstanceOf[Seq[User]])
}

"getMediaInfoByShortCode" should "return a Some[MediaInfoFeed]" in {
val request = Await.result(instagram.getMediaInfoByShortCode("BZfmV4RFklR"), 10 seconds)
request should be(anInstanceOf[Response[MediaInfoFeed]])
Expand Down

0 comments on commit c802ed3

Please sign in to comment.