Skip to content

Commit

Permalink
Fix Instagram class
Browse files Browse the repository at this point in the history
  • Loading branch information
yukihirai0505 committed Sep 26, 2017
1 parent 5512d06 commit bb6566e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 63 deletions.
98 changes: 43 additions & 55 deletions src/main/scala/com/yukihirai0505/sInstagram/Instagram.scala
Expand Up @@ -2,13 +2,11 @@ package com.yukihirai0505.sInstagram

import java.net.URLEncoder

import play.api.libs.json.Reads

import com.netaporter.uri.Uri._
import com.yukihirai0505.com.scala.Request
import com.yukihirai0505.com.scala.constants.Verbs
import com.yukihirai0505.com.scala.model.Response
import com.yukihirai0505.sInstagram.model.{Constants, Methods, OAuthConstants, QueryParam, Relationship}
import com.yukihirai0505.sInstagram.model._
import com.yukihirai0505.sInstagram.responses.auth.{AccessToken, Auth, SignedAccessToken}
import com.yukihirai0505.sInstagram.responses.comments.MediaCommentsFeed
import com.yukihirai0505.sInstagram.responses.common.{NoDataResponse, Pagination}
Expand All @@ -21,6 +19,7 @@ import com.yukihirai0505.sInstagram.responses.users.basicinfo.UserInfo
import com.yukihirai0505.sInstagram.responses.users.feed.UserFeed
import com.yukihirai0505.sInstagram.utils.PaginationHelper
import dispatch._
import play.api.libs.json.Reads

import scala.language.postfixOps

Expand Down Expand Up @@ -116,7 +115,7 @@ class Instagram(auth: Auth) {
*/
def getUserInfo(userId: String): Future[Response[UserInfo]] = {
val apiPath: String = Methods.USERS_WITH_ID format userId
request[UserInfo](Verbs.GET, apiPath)
request(Verbs.GET, apiPath)
}

/**
Expand All @@ -126,7 +125,7 @@ class Instagram(auth: Auth) {
* if any error occurs.
*/
def getCurrentUserInfo: Future[Response[UserInfo]] = {
request[UserInfo](Verbs.GET, Methods.USERS_SELF)
request(Verbs.GET, Methods.USERS_SELF)
}

/**
Expand All @@ -141,15 +140,15 @@ class Instagram(auth: Auth) {
*/
def getRecentMediaFeed(userId: Option[String] = None, count: Option[Int] = None, minId: Option[String] = None, maxId: Option[String] = None): Future[Response[MediaFeed]] = {
val params: Map[String, Option[String]] = Map(
QueryParam.COUNT -> Option(count.mkString),
QueryParam.MIN_ID -> Option(minId.mkString),
QueryParam.MAX_ID -> Option(maxId.mkString)
QueryParam.COUNT -> count.map(_.toString),
QueryParam.MIN_ID -> minId,
QueryParam.MAX_ID -> maxId
)
val apiPath: String = userId match {
case Some(id) => Methods.USERS_RECENT_MEDIA format id
case None => Methods.USERS_SELF_RECENT_MEDIA
}
request[MediaFeed](Verbs.GET, apiPath, Some(params))
request(Verbs.GET, apiPath, Some(params))
}

/**
Expand All @@ -170,10 +169,10 @@ class Instagram(auth: Auth) {
*/
def getUserFollowListNextPage(userId: String, cursor: Option[String] = None): Future[Response[UserFeed]] = {
val params: Map[String, Option[String]] = Map(
QueryParam.CURSOR -> Option(cursor.mkString)
QueryParam.CURSOR -> cursor
)
val apiPath: String = Methods.USERS_ID_FOLLOWS format userId
request[UserFeed](Verbs.GET, apiPath, Some(params))
request(Verbs.GET, apiPath, Some(params))
}

/**
Expand All @@ -183,7 +182,7 @@ class Instagram(auth: Auth) {
*/
def getUserFollowListNextPageByPage(pagination: Pagination): Future[Response[UserFeed]] = {
val page: PaginationHelper.Page = PaginationHelper.parseNextUrl(pagination, Constants.API_URL)
request[UserFeed](Verbs.GET, page.apiPath, Option(page.queryStringParams))
request(Verbs.GET, page.apiPath, Option(page.queryStringParams))
}

/**
Expand All @@ -194,10 +193,10 @@ class Instagram(auth: Auth) {
*/
def getUserLikedMediaFeed(maxLikeId: Option[Long] = None, count: Option[Int] = None): Future[Response[MediaFeed]] = {
val params: Map[String, Option[String]] = Map(
QueryParam.MAX_LIKE_ID -> Option(maxLikeId.mkString),
QueryParam.COUNT -> Option(count.mkString)
QueryParam.MAX_LIKE_ID -> maxLikeId.map(_.toString),
QueryParam.COUNT -> count.map(_.toString)
)
request[MediaFeed](Verbs.GET, Methods.USERS_SELF_LIKED_MEDIA, Some(params))
request(Verbs.GET, Methods.USERS_SELF_LIKED_MEDIA, Some(params))
}

/**
Expand All @@ -211,9 +210,9 @@ class Instagram(auth: Auth) {
def searchUser(query: String, count: Option[Int] = None): Future[Response[UserFeed]] = {
val params: Map[String, Option[String]] = Map(
QueryParam.SEARCH_QUERY -> Some(query),
QueryParam.COUNT -> Option(count.mkString)
QueryParam.COUNT -> count.map(_.toString)
)
request[UserFeed](Verbs.GET, Methods.USERS_SEARCH, Some(params))
request(Verbs.GET, Methods.USERS_SEARCH, Some(params))
}

/**
Expand All @@ -226,7 +225,7 @@ class Instagram(auth: Auth) {
*/
def getLocationInfo(locationId: String): Future[Response[LocationInfo]] = {
val apiPath: String = Methods.LOCATIONS_BY_ID format locationId
request[LocationInfo](Verbs.GET, apiPath)
request(Verbs.GET, apiPath)
}

/**
Expand All @@ -239,7 +238,7 @@ class Instagram(auth: Auth) {
*/
def getTagInfo(tagName: String): Future[Response[TagInfoFeed]] = {
val apiPath: String = Methods.TAGS_BY_NAME format URLEncoder.encode(tagName, "UTF-8")
request[TagInfoFeed](Verbs.GET, apiPath)
request(Verbs.GET, apiPath)
}

/**
Expand All @@ -260,7 +259,7 @@ class Instagram(auth: Auth) {
QueryParam.LONGITUDE -> Some(longitude.toString),
QueryParam.DISTANCE -> Some(distance.getOrElse(Constants.LOCATION_DEFAULT_DISTANCE).toString)
)
request[LocationSearchFeed](Verbs.GET, Methods.LOCATIONS_SEARCH, Some(params))
request(Verbs.GET, Methods.LOCATIONS_SEARCH, Some(params))
}

/**
Expand All @@ -277,11 +276,11 @@ class Instagram(auth: Auth) {
*/
def getRecentMediaByLocation(locationId: String, minId: Option[String] = None, maxId: Option[String] = None): Future[Response[MediaFeed]] = {
val params: Map[String, Option[String]] = Map(
QueryParam.MIN_ID -> Option(minId.mkString),
QueryParam.MAX_ID -> Option(maxId.mkString)
QueryParam.MIN_ID -> minId,
QueryParam.MAX_ID -> maxId
)
val apiMethod: String = Methods.LOCATIONS_RECENT_MEDIA_BY_ID format locationId
request[MediaFeed](Verbs.GET, apiMethod, Some(params))
request(Verbs.GET, apiMethod, Some(params))
}

/**
Expand All @@ -293,8 +292,7 @@ class Instagram(auth: Auth) {
* if any error occurs.
*/
def setUserLike(mediaId: String): Future[Response[NoDataResponse]] = {
val apiMethod: String = Methods.LIKES_BY_MEDIA_ID format mediaId
request[NoDataResponse](Verbs.POST, apiMethod)
request(Verbs.POST, Methods.LIKES_BY_MEDIA_ID format mediaId)
}

/**
Expand All @@ -306,8 +304,7 @@ class Instagram(auth: Auth) {
* if any error occurs.
*/
def getMediaInfo(mediaId: String): Future[Response[MediaInfoFeed]] = {
val apiPath = Methods.MEDIA_BY_ID format mediaId
request[MediaInfoFeed](Verbs.GET, apiPath)
request(Verbs.GET, Methods.MEDIA_BY_ID format mediaId)
}

/**
Expand All @@ -318,7 +315,7 @@ class Instagram(auth: Auth) {
*/
def getRecentMediaNextPage(pagination: Pagination): Future[Response[MediaFeed]] = {
val page: PaginationHelper.Page = PaginationHelper.parseNextUrl(pagination, Constants.API_URL)
request[MediaFeed](Verbs.GET, page.apiPath, Some(page.queryStringParams))
request(Verbs.GET, page.apiPath, Some(page.queryStringParams))
}

/**
Expand All @@ -328,7 +325,7 @@ class Instagram(auth: Auth) {
*/
def getUserFeedInfoNextPage(pagination: Pagination): Future[Response[UserFeed]] = {
val page: PaginationHelper.Page = PaginationHelper.parseNextUrl(pagination, Constants.API_URL)
request[UserFeed](Verbs.GET, page.apiPath, Option(page.queryStringParams))
request(Verbs.GET, page.apiPath, Option(page.queryStringParams))
}

/**
Expand All @@ -340,8 +337,7 @@ class Instagram(auth: Auth) {
* if any error occurs.
*/
def deleteUserLike(mediaId: String): Future[Response[NoDataResponse]] = {
val apiPath: String = Methods.LIKES_BY_MEDIA_ID format mediaId
request[NoDataResponse](Verbs.DELETE, apiPath)
request(Verbs.DELETE, Methods.LIKES_BY_MEDIA_ID format mediaId)
}

/**
Expand All @@ -353,8 +349,7 @@ class Instagram(auth: Auth) {
* if any error occurs.
*/
def getMediaComments(mediaId: String): Future[Response[MediaCommentsFeed]] = {
val apiPath: String = Methods.MEDIA_COMMENTS format mediaId
request[MediaCommentsFeed](Verbs.GET, apiPath)
request(Verbs.GET, Methods.MEDIA_COMMENTS format mediaId)
}

/**
Expand All @@ -372,8 +367,7 @@ class Instagram(auth: Auth) {
val params: Map[String, Option[String]] = Map(
QueryParam.TEXT -> Some(text)
)
val apiPath: String = Methods.MEDIA_COMMENTS format mediaId
request[NoDataResponse](Verbs.POST, apiPath, Some(params))
request(Verbs.POST, Methods.MEDIA_COMMENTS format mediaId, Some(params))
}

/**
Expand All @@ -388,8 +382,7 @@ class Instagram(auth: Auth) {
* if any error occurs.
*/
def deleteMediaCommentById(mediaId: String, commentId: String): Future[Response[NoDataResponse]] = {
val apiPath: String = Methods.DELETE_MEDIA_COMMENTS format(mediaId, commentId)
request[NoDataResponse](Verbs.DELETE, apiPath)
request(Verbs.DELETE, Methods.DELETE_MEDIA_COMMENTS format(mediaId, commentId))
}

/**
Expand All @@ -402,8 +395,7 @@ class Instagram(auth: Auth) {
val params: Map[String, Option[String]] = Map(
QueryParam.CURSOR -> Option(cursor.mkString)
)
val apiPath: String = Methods.USERS_ID_FOLLOWED_BY format userId
request[UserFeed](Verbs.GET, apiPath, Some(params))
request(Verbs.GET, Methods.USERS_ID_FOLLOWED_BY format userId, Some(params))
}

/**
Expand All @@ -424,8 +416,7 @@ class Instagram(auth: Auth) {
* if any error occurs.
*/
def getMediaInfoByShortCode(shortCode: String): Future[Response[MediaInfoFeed]] = {
val apiPath: String = Methods.MEDIA_BY_SHORT_CODE format shortCode
request[MediaInfoFeed](Verbs.GET, apiPath)
request(Verbs.GET, Methods.MEDIA_BY_SHORT_CODE format shortCode)
}

/**
Expand All @@ -441,7 +432,7 @@ class Instagram(auth: Auth) {
val params: Map[String, Option[String]] = Map(
QueryParam.SEARCH_QUERY -> Some(tagName)
)
request[TagSearchFeed](Verbs.GET, Methods.TAGS_SEARCH, Some(params))
request(Verbs.GET, Methods.TAGS_SEARCH, Some(params))
}

/**
Expand All @@ -462,11 +453,11 @@ class Instagram(auth: Auth) {
def getRecentMediaFeedTags(tagName: String, minTagId: Option[String] = None, maxTagId: Option[String] = None, count: Option[Long] = None): Future[Response[MediaFeed]] = {
val apiPath: String = Methods.TAGS_RECENT_MEDIA format tagName
val params: Map[String, Option[String]] = Map(
QueryParam.MIN_TAG_ID -> Option(minTagId.mkString),
QueryParam.MAX_TAG_ID -> Option(maxTagId.mkString),
QueryParam.COUNT -> Option(count.mkString)
QueryParam.MIN_TAG_ID -> minTagId,
QueryParam.MAX_TAG_ID -> maxTagId,
QueryParam.COUNT -> count.map(_.toString)
)
request[MediaFeed](Verbs.GET, apiPath, Some(params))
request(Verbs.GET, apiPath, Some(params))
}

/**
Expand All @@ -483,8 +474,7 @@ class Instagram(auth: Auth) {
val params: Map[String, Option[String]] = Map(
QueryParam.ACTION -> Some(relationship.value)
)
val apiPath: String = Methods.USERS_ID_RELATIONSHIP format userId
request[RelationshipFeed](Verbs.POST, apiPath, Some(params))
request(Verbs.POST, Methods.USERS_ID_RELATIONSHIP format userId, Some(params))
}

/**
Expand All @@ -494,7 +484,7 @@ class Instagram(auth: Auth) {
* if any error occurs.
*/
def getUserRequestedBy: Future[Response[UserFeed]] = {
request[UserFeed](Verbs.GET, Methods.USERS_SELF_REQUESTED_BY)
request(Verbs.GET, Methods.USERS_SELF_REQUESTED_BY)
}

/**
Expand All @@ -507,8 +497,7 @@ class Instagram(auth: Auth) {
* if any error occurs.
*/
def getUserRelationship(userId: String): Future[Response[RelationshipFeed]] = {
val apiPath: String = Methods.USERS_ID_RELATIONSHIP format userId
request[RelationshipFeed](Verbs.GET, apiPath)
request(Verbs.GET, Methods.USERS_ID_RELATIONSHIP format userId)
}

/**
Expand All @@ -523,7 +512,7 @@ class Instagram(auth: Auth) {
val params: Map[String, Option[String]] = Map(
QueryParam.FACEBOOK_PLACES_ID -> Some(facebookPlacesId)
)
request[LocationSearchFeed](Verbs.GET, Methods.LOCATIONS_SEARCH, Some(params))
request(Verbs.GET, Methods.LOCATIONS_SEARCH, Some(params))
}

/**
Expand All @@ -544,7 +533,7 @@ class Instagram(auth: Auth) {
QueryParam.LONGITUDE -> Some(longitude.toString),
QueryParam.DISTANCE -> Option(distance.mkString)
)
request[MediaFeed](Verbs.GET, Methods.MEDIA_SEARCH, Some(params))
request(Verbs.GET, Methods.MEDIA_SEARCH, Some(params))
}

/**
Expand All @@ -556,7 +545,6 @@ class Instagram(auth: Auth) {
* if any error occurs.
*/
def getUserLikes(mediaId: String): Future[Response[LikesFeed]] = {
val apiPath: String = Methods.LIKES_BY_MEDIA_ID format mediaId
request[LikesFeed](Verbs.GET, apiPath)
request(Verbs.GET, Methods.LIKES_BY_MEDIA_ID format mediaId)
}
}
@@ -1,10 +1,10 @@
package com.yukihirai0505.sInstagram.responses.common

case class Location(
id: String,
latitude: Double,
longitude: Double,
name: String
id: Option[String],
latitude: Option[Double],
longitude: Option[Double],
name: Option[String]
)

import play.api.libs.json.Json
Expand Down
Expand Up @@ -106,8 +106,8 @@ class InstagramSpec extends FlatSpec with Matchers {

"getLocationInfo" should "return a Some[LocationInfo]" in {
val request = Await.result(instagram.getLocationInfo(locationId.getOrElse("")), 10 seconds)
latitude = request.data.flatMap(x => Some(x.data.latitude))
longitude = request.data.flatMap(x => Some(x.data.longitude))
latitude = request.data.flatMap(x => x.data.latitude)
longitude = request.data.flatMap(x => x.data.longitude)
request should be(anInstanceOf[Response[LocationInfo]])
}

Expand Down Expand Up @@ -175,7 +175,7 @@ class InstagramSpec extends FlatSpec with Matchers {
}

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

Expand Down Expand Up @@ -205,7 +205,7 @@ class InstagramSpec extends FlatSpec with Matchers {
}

"searchFacebookPlace" should "return a Some[LocationSearchFeed]" in {
val request = Await.result(instagram.searchFacebookPlace("273471170716"), 10 seconds)
val request = Await.result(instagram.searchFacebookPlace(locationId.getOrElse("")), 10 seconds)
request should be(anInstanceOf[Response[LocationSearchFeed]])
}

Expand Down

0 comments on commit bb6566e

Please sign in to comment.