From 3f5fb97e8963472a474ef77c2ac4445222dd3e43 Mon Sep 17 00:00:00 2001 From: Eugene Yokota Date: Tue, 8 May 2018 00:33:07 -0400 Subject: [PATCH] we can't use snip here because of backslash --- .../00-Getting-Started/02-sbt-by-example.md | 36 ++++++++++++++++++- .../00-Getting-Started/02-scala-by-example.md | 36 ++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/reference/00-Getting-Started/02-sbt-by-example.md b/src/reference/00-Getting-Started/02-sbt-by-example.md index 795c12c06..2a6eb7f94 100644 --- a/src/reference/00-Getting-Started/02-sbt-by-example.md +++ b/src/reference/00-Getting-Started/02-sbt-by-example.md @@ -348,7 +348,41 @@ Let's add Play JSON to `helloCore`. After `reload`, add `core/src/main/scala/example/core/Weather.scala`: -@@snip [example-weather]($root$/src/sbt-test/ref/example-weather/core/src/main/scala/example/core/Weather.scala) {} +```scala +package example.core + +import gigahorse._, support.okhttp.Gigahorse +import scala.concurrent._ +import play.api.libs.json._ + +object Weather { + lazy val http = Gigahorse.http(Gigahorse.config) + def weather: Future[String] = { + val r = Gigahorse.url("https://query.yahooapis.com/v1/public/yql").get. + addQueryString( + "q" -> """select item.condition + |from weather.forecast where woeid in (select woeid from geo.places(1) where text='New York, NY') + |and u='c'""".stripMargin, + "format" -> "json" + ) + + import ExecutionContext.Implicits._ + for { + f <- http.run(r, Gigahorse.asString) + x <- parse(f) + } yield x + } + + def parse(rawJson: String): Future[String] = { + val js = Json.parse(rawJson) + (js \\\\ "text").headOption match { + case Some(JsString(x)) => Future.successful(x.toLowerCase) + case _ => Future.failed(sys.error(rawJson)) + } + } +} + +``` Next, change `src/main/scala/example/Hello.scala` as follows: diff --git a/src/reference/ja/00-Getting-Started/02-scala-by-example.md b/src/reference/ja/00-Getting-Started/02-scala-by-example.md index 66a68cbe9..ebaca7d01 100644 --- a/src/reference/ja/00-Getting-Started/02-scala-by-example.md +++ b/src/reference/ja/00-Getting-Started/02-scala-by-example.md @@ -342,7 +342,41 @@ sbt:Hello> ~testQuick `reload` 後、`core/src/main/scala/example/core/Weather.scala` を追加する: -@@snip [example-weather]($root$/src/sbt-test/ref/example-weather/core/src/main/scala/example/core/Weather.scala) {} +```scala +package example.core + +import gigahorse._, support.okhttp.Gigahorse +import scala.concurrent._ +import play.api.libs.json._ + +object Weather { + lazy val http = Gigahorse.http(Gigahorse.config) + def weather: Future[String] = { + val r = Gigahorse.url("https://query.yahooapis.com/v1/public/yql").get. + addQueryString( + "q" -> """select item.condition + |from weather.forecast where woeid in (select woeid from geo.places(1) where text='New York, NY') + |and u='c'""".stripMargin, + "format" -> "json" + ) + + import ExecutionContext.Implicits._ + for { + f <- http.run(r, Gigahorse.asString) + x <- parse(f) + } yield x + } + + def parse(rawJson: String): Future[String] = { + val js = Json.parse(rawJson) + (js \\\\ "text").headOption match { + case Some(JsString(x)) => Future.successful(x.toLowerCase) + case _ => Future.failed(sys.error(rawJson)) + } + } +} + +``` 次に `src/main/scala/example/Hello.scala` を以下のように変更する: