Skip to content

Commit

Permalink
change TwitterEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
y-yu committed Jul 20, 2015
1 parent 8a0834b commit 6599cc9
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 93 deletions.
8 changes: 1 addition & 7 deletions src/main/scala/Main.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import models.DefaultEnvironment
import services.UpdateStatusService._

import scala.concurrent.Future

// import repositories.TwitterRepository._
// import repositories.TwitterRepositoryDI._
import scala.concurrent.ExecutionContext.Implicits.global

object Main {
def main(args: Array[String]): Unit = {
updateStatusIfExist("_yyu_", "ok").run(DefaultEnvironment.defaultEnvironment)
updateStatusIfExist("_yyu_", "good").run(DefaultEnvironment.defaultEnvironment)
}
}
10 changes: 5 additions & 5 deletions src/main/scala/models/DefaultEnvironment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import play.api.libs.ws.ning.NingWSClient
object DefaultEnvironment {
val config = new NingAsyncHttpClientConfigBuilder(DefaultWSClientConfig()).build()
val builder = new AsyncHttpClientConfig.Builder(config)
val client = new NingWSClient(builder.build)
val c = new NingWSClient(builder.build)

val defaultEnvironment = TwitterEnv(
client,
OAuthCalculator(
val defaultEnvironment = new UseWSClient with UseOAuthCred {
val client = c
val cred = OAuthCalculator(
ConsumerKey(
"key",
"secret"
Expand All @@ -25,5 +25,5 @@ object DefaultEnvironment {
"secret"
)
)
)
}
}
6 changes: 0 additions & 6 deletions src/main/scala/models/TwitterEnv.scala

This file was deleted.

11 changes: 3 additions & 8 deletions src/main/scala/models/TwitterSYM.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package models

import play.api.libs.ws.WSResponse
import utils.Reader

import scala.concurrent.Future

trait TwitterSYM[R[_]] {
def existUserWithScreenName(screenName: R[String]): R[Future[Boolean]]
def updateStatus(status: R[String]): R[Future[String]]
def condition[A](cond: R[Future[Boolean]], t: R[A], e: R[A]): R[Future[A]]
def existUserWithScreenName(screenName: R[String]): R[Boolean]
def updateStatus(status: R[String]): R[String]
def condition[A](cond: R[Boolean], t: R[A], e: R[A]): R[A]
}
10 changes: 10 additions & 0 deletions src/main/scala/models/UseOAuthCred.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

import play.api.libs.oauth.OAuthCalculator

/**
* Created by hikaru_yoshimura on 2015/07/20.
*/
trait UseOAuthCred {
val cred: OAuthCalculator
}
10 changes: 10 additions & 0 deletions src/main/scala/models/UseWSClient.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

import play.api.libs.ws.WSClient

/**
* Created by hikaru_yoshimura on 2015/07/20.
*/
trait UseWSClient {
val client: WSClient
}
68 changes: 36 additions & 32 deletions src/main/scala/repositories/DefaultInterpreter.scala
Original file line number Diff line number Diff line change
@@ -1,63 +1,67 @@
package repositories

import models.TwitterEnv
import models.TwitterSYM
import play.api.libs.ws.WSResponse
import models.UseOAuthCred
import models.UseWSClient
import utils.Reader
import utils.Reader._

import scala.concurrent.Await
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration

object DefaultInterpreter {
type Twitter[A] = Reader[TwitterEnv, A]
type Twitter[A] = Reader[UseWSClient with UseOAuthCred, A]

implicit val defaultInterpreter = new TwitterSYM[Twitter] {
def existUserWithScreenName(screenName: Twitter[String]): Twitter[Future[Boolean]] =
def existUserWithScreenName(screenName: Twitter[String]): Twitter[Boolean] =
for {
sn <- screenName
env <- ask
} yield for {
res <- env.client.url("https://api.twitter.com/1.1/users/show.json")
.withQueryString("screen_name" -> sn)
.sign(env.cred)
.get()
} yield
if (res.status == 200) true
else throw new RuntimeException("not found")
} yield {
val res = Await.result(
env.client.url("https://api.twitter.com/1.1/users/show.json")
.withQueryString("screen_name" -> sn)
.sign(env.cred)
.get(),
Duration.Inf
)

res.status == 200
}

def updateStatus(status: Twitter[String]): Twitter[Future[String]] =
def updateStatus(status: Twitter[String]): Twitter[String] =
for {
s <- status
env <- ask
} yield for {
res <- env.client.url("https://api.twitter.com/1.1/statuses/update.json")
.sign(env.cred)
.post(Map("status" -> Seq(s)))
id <- Future((res.json \ "id_str").as[String])
} yield id
} yield {
val res = Await.result(
env.client.url("https://api.twitter.com/1.1/statuses/update.json")
.sign(env.cred)
.post(Map("status" -> Seq(s))),
Duration.Inf
)

(res.json \ "id_str").as[String]
}

def condition[A](cond: Twitter[Future[Boolean]], t: Twitter[A], e: Twitter[A]): Twitter[Future[A]] =
def condition[A](cond: Twitter[Boolean], t: Twitter[A], e: Twitter[A]): Twitter[A] = {
for {
f <- cond
env <- ask
} yield for {
c <- f
} yield {
if (c) t(env) else e(env)
}
bool <- cond
env <- ask
} yield
if (bool) t(env) else e(env)
}
}

def existUserWithScreenName(screenName: Twitter[String])(implicit i: TwitterSYM[Twitter]): Twitter[Future[Boolean]] =
def pure[A](a: A): Twitter[A] = Reader.pure[UseWSClient with UseOAuthCred, A](a)

def existUserWithScreenName(screenName: Twitter[String])(implicit i: TwitterSYM[Twitter]): Twitter[Boolean] =
i.existUserWithScreenName(screenName)

def updateStatus(status: Twitter[String])(implicit i: TwitterSYM[Twitter]): Twitter[Future[String]] =
def updateStatus(status: Twitter[String])(implicit i: TwitterSYM[Twitter]): Twitter[String] =
i.updateStatus(status)

def condition[A](c: Twitter[Future[Boolean]], t: Twitter[A], e: Twitter[A])(implicit i: TwitterSYM[Twitter]): Twitter[Future[A]] =
def condition[A](c: Twitter[Boolean], t: Twitter[A], e: Twitter[A])(implicit i: TwitterSYM[Twitter]): Twitter[A] =
i.condition(c, t, e)
}

Expand Down
24 changes: 0 additions & 24 deletions src/main/scala/repositories/TwitterRepositoryDI.scala

This file was deleted.

11 changes: 2 additions & 9 deletions src/main/scala/services/UpdateStatusService.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package services

import models.TwitterEnv
import utils.Reader._
import repositories.DefaultInterpreter._

import scala.concurrent.Future

/**
* Created by hikaru_yoshimura on 2015/07/20.
*/
object UpdateStatusService {
def updateStatusIfExist(screenName: String, status: String): Twitter[Future[String]] =
def updateStatusIfExist(screenName: String, status: String): Twitter[String] =
condition(
existUserWithScreenName(pure(screenName)),
updateStatus(pure(status)),
pure[TwitterEnv, Twitter[Future[String]]]({println("ng"); Future.successful("ng")})
pure("ng")
)
}
2 changes: 0 additions & 2 deletions src/main/scala/utils/Reader.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package utils



case class Reader[E, A](g: E => A) {
def apply(e: E) = g(e)
def run: E => A = apply
Expand Down

0 comments on commit 6599cc9

Please sign in to comment.