Created
August 1, 2015 20:37
-
-
Save seamusv/31912eb0eef055f0b63a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.ActorSystem | |
import akka.stream.ActorMaterializer | |
import akka.stream.scaladsl._ | |
import org.apache.http.client.fluent.{Form, Request} | |
import scala.concurrent.duration.Duration | |
import scala.concurrent.{Await, Future} | |
import scala.util.matching.Regex | |
object Streaming extends App { | |
implicit val system = ActorSystem("Sys") | |
implicit val materializer = ActorMaterializer() | |
def updateCustomer(name: String): Future[String] = { | |
Source.single(name) | |
.mapAsyncUnordered(1)(reverseString) | |
.map(_.toLowerCase) | |
.runFold("")((m, s) => m + s) | |
} | |
def reverseString(s: String): Future[String] = Future.successful { | |
val r = Request.Post("http://www.miniwebtool.com/reverse-text/") | |
.bodyForm(Form.form().add("text", s).add("wordbyword", "0").build()) | |
.execute() | |
.returnContent() | |
.asString() | |
val pattern: Regex = """<div><textarea rows=5 style="width:100%">(.*?)</textarea></div>""".r | |
pattern.findFirstMatchIn(r).get.group(1) | |
} | |
println(Await.result(updateCustomer("Welcome to the Jungle!"), Duration.Inf)) | |
system.shutdown() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment