├── .gitignore ├── README ├── build.sbt └── src ├── main └── scala │ └── org │ └── scribe │ └── http │ ├── HttpRequest.scala │ └── Method.scala └── test └── scala └── org └── scribe └── http ├── HttpRequestSpec.scala └── MethodSpec.scala /.gitignore: -------------------------------------------------------------------------------- 1 | reports/ 2 | target/ 3 | project/boot 4 | .idea/ 5 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | 2 | 3 | This is **NOT** the production-ready version of scribe. For that go to https://github.com/fernandezpablo85/scribe-java. 4 | 5 | Do **NOT** use this in your production code unless you know exactly what you're doing. 6 | 7 | 8 | 9 | This is going to be Scribe on functional-steroids. 10 | 11 | I won't take this too seriously (hint: I didn't with scribe-java at first, and I'm not sure wether I do right now) but it will be a good way to actually see what *real* benefits does scala bring to the JVM table. 12 | 13 | Hopefully I'll learn something in the process :) 14 | 15 | Tool Choices: 16 | 17 | * Build Tool: sbt 18 | 19 | * Test Framework: ScalaTest 20 | 21 | * IDE: Idea -------------------------------------------------------------------------------- /build.sbt: -------------------------------------------------------------------------------- 1 | 2 | 3 | name := "scribe" 4 | 5 | organization := "org.scribe" 6 | 7 | // Test Dependencies 8 | libraryDependencies += "org.scalatest" % "scalatest_2.8.1" % "1.5.1" % "test" 9 | -------------------------------------------------------------------------------- /src/main/scala/org/scribe/http/HttpRequest.scala: -------------------------------------------------------------------------------- 1 | package org.scribe.http 2 | 3 | import io.Source 4 | import java.net.{HttpURLConnection, URL} 5 | 6 | /** 7 | * @author: pfernand 8 | */ 9 | class HttpRequest(method : Method, url : String) 10 | { 11 | 12 | lazy val statusCode = response.statusCode 13 | lazy val bodyStream = response.stream 14 | lazy val body = Source.fromInputStream(bodyStream).getLines.mkString 15 | 16 | private val response = 17 | { 18 | val connection = new URL(url).openConnection().asInstanceOf[HttpURLConnection] 19 | connection.setRequestMethod(method.name) 20 | connection.connect() 21 | new HttpResponse(connection) 22 | } 23 | 24 | private class HttpResponse(connection : HttpURLConnection) 25 | { 26 | val statusCode = connection.getResponseCode 27 | val successful = (statusCode >= 200 && statusCode < 400) 28 | val stream = if (successful) connection.getInputStream else connection.getErrorStream 29 | } 30 | } -------------------------------------------------------------------------------- /src/main/scala/org/scribe/http/Method.scala: -------------------------------------------------------------------------------- 1 | package org.scribe.http 2 | 3 | /** 4 | * @author: pfernand 5 | */ 6 | case class Method(name : String) 7 | { 8 | def url (_url : String)(block : HttpRequest => Unit) 9 | { 10 | block(new HttpRequest(this, _url)) 11 | } 12 | } 13 | 14 | object Method 15 | { 16 | val GET = Method("GET") 17 | val POST = Method("POST") 18 | } 19 | -------------------------------------------------------------------------------- /src/test/scala/org/scribe/http/HttpRequestSpec.scala: -------------------------------------------------------------------------------- 1 | package org.scribe.http 2 | 3 | import org.scalatest._ 4 | import org.scalatest.matchers._ 5 | import java.io.InputStream 6 | 7 | /** 8 | * @author: pfernand 9 | */ 10 | class HttpRequestSpec extends Spec 11 | { 12 | 13 | describe ("A valid http get request") 14 | { 15 | val request = new HttpRequest(Method.GET, "http://www.google.com") 16 | 17 | it ("should return an 200 status code") 18 | { 19 | assert(request.statusCode == 200) 20 | } 21 | 22 | it ("should return the response contents as an InputStream") 23 | { 24 | assert(request.bodyStream.available > 0) 25 | assert(request.bodyStream.isInstanceOf[InputStream]) 26 | } 27 | 28 | it ("should return the response contents as String") 29 | { 30 | assert(request.body.length > 0) 31 | assert(request.body.isInstanceOf[String]) 32 | } 33 | } 34 | 35 | describe ("A valid http post request") 36 | { 37 | 38 | } 39 | 40 | describe ("An invalid (not found) http request") 41 | { 42 | 43 | } 44 | } -------------------------------------------------------------------------------- /src/test/scala/org/scribe/http/MethodSpec.scala: -------------------------------------------------------------------------------- 1 | package org.scribe.http 2 | 3 | import org.scalatest._ 4 | 5 | /** 6 | * @author: pfernand 7 | */ 8 | class MethodSpec extends Spec 9 | { 10 | 11 | describe ("An http get method instance") 12 | { 13 | val get = Method.GET 14 | 15 | it ("should execute a simple get request") 16 | { 17 | get.url("http://www.google.com") { 18 | request => { 19 | println("status is: "+request.statusCode) 20 | println("response body is: "+request.body) 21 | } 22 | } 23 | } 24 | } 25 | } --------------------------------------------------------------------------------