Skip to content

Instantly share code, notes, and snippets.

@trbngr
Created October 17, 2016 21:16
Show Gist options
  • Select an option

  • Save trbngr/420e273a07fe770a368e92336a62546a to your computer and use it in GitHub Desktop.

Select an option

Save trbngr/420e273a07fe770a368e92336a62546a to your computer and use it in GitHub Desktop.
package com.company
import akka.actor.ActorSystem
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.model.Uri
import akka.util.Timeout
import com.company.Crawler.Destination
import com.company.model.AliasRequest
import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}
class HttpMediator(repository: Repository)(implicit system: ActorSystem, ex: ExecutionContext) {
import akka.pattern.ask
val crawler = system.actorOf(Crawler.props)
def getAliases(userId: UserId): Future[(Success, Set[AliasEntity])] = {
repository.getAliases(userId) map { aliases ⇒
OK → aliases
}
}
def createLink(pixelId: PixelId, request: AliasRequest): Future[(Success, AliasEntity)] = {
implicit val timeout = Timeout(5 seconds)
val future = crawler ? Crawler.Crawl(Uri(request.url))
future flatMap {
case d: Destination ⇒
val screenshot = d.screenShot
val entity = AliasEntity(pixelId, AliasId.generate, request.url, d.resolvedUrl.toString(), d.title, ScreenShots(screenshot.thumbnail, screenshot.fullPage), Tags.normalize(request.tags))
repository saveAlias entity map (_ ⇒ OK → entity)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment