Skip to content

Instantly share code, notes, and snippets.

@sassunt
Created February 19, 2012 13:10
Show Gist options
  • Save sassunt/1863751 to your computer and use it in GitHub Desktop.
Save sassunt/1863751 to your computer and use it in GitHub Desktop.
dispatchの基本的な使い方
scala> import dispatch._
import dispatch._
scala> val req = :/("gist.github.com") / "1614046"
req: dispatch.Request = dispatch.Request@4e04622f
scala> req.host
res0: org.apache.http.HttpHost = http://gist.github.com
scala> req.path
res1: String = /1614046
scala> req.method
res3: String = GET
scala> req.secure
res6: dispatch.Request = dispatch.Request@43bbaad3
scala> val req2 = req.secure
req2: dispatch.Request = dispatch.Request@660d402e
scala> req2.host
res7: org.apache.http.HttpHost = https://gist.github.com
scala> val req3 = url("https://raw.github.com/gist/1614046/30d74d258442c7c65512eafab474568dd706c430/gist-test")
req8: dispatch.Request = dispatch.Request@6bedc67e
scala> http(req3 as_str)
INF: [console logger] dispatch: raw.github.com GET /gist/1614046/30d74d258442c7c65512eafab474568dd706c430/gist-test HTTP/1.1
res9: http.HttpPackage[String] = test
scala> import dispatch.json._
import dispatch.json._
scala> import dispatch.json.JsHttp._
import dispatch.json.JsHttp._
scala> val http = new Http
http: dispatch.Http = dispatch.Http@412429c
scala> val req = url("https://api.github.com/gists/1614046")
req: dispatch.Request = dispatch.Request@682f594d
scala> val JsObject(gist) = http(req ># obj)
INF: [console logger] dispatch: api.github.com GET /gists/1614046 HTTP/1.1
gist: Map[dispatch.json.JsString,dispatch.json.JsValue] = Map("updated_at" -> "2012-01-15T02:48:30Z", "url" -> "https://api.github.com/gists/1614046", "git_pull_url" -> "git://gist.github.com/1614046.git", "history" -> [{"change_status" : {"additions" : 1, "deletions" : 0, "total" : 1}, "url" : "https://api.github.com/gists/1614046/2c5f13af1386eedce8159bac7a373d22cc7bcfa8", "user" : {"login" : "sassunt", "gravatar_id" : "01c2f43a6e1a9a1a26fdedef7efcb339", "url" : "https://api.github.com/users/sassunt", "avatar_url" : "https://secure.gravatar.com/avatar/01c2f43a6e1a9a1a26fdedef7efcb339?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png", "id" : 1014191}, "committed_at" : "2012-01-15T02:48:30Z", "version" : "2c5f13af1386eedce8159bac7a373d22cc7bcfa8"}], "...
scala>
scala> :paste
// Entering paste mode (ctrl-D to finish)
for { (JsString("description"), JsString(description)) <- gist
(JsString("user"), JsObject(userObj)) <- gist
(JsString("login"), JsString(userID)) <- userObj
} yield Map("description" -> description, "user" -> userID)
// Exiting paste mode, now interpreting.
res3: scala.collection.immutable.Iterable[scala.collection.immutable.Map[java.lang.String,String]] = List(Map(description -> gist test, user -> sassunt))
scala>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment