Commits

e...@cocktail  committed 9e3bc01

v0.18.12
* ResponseBody has new iterator method that returns the lines of the body via an iterator
* sbt now uses v0.12.1

  • Participants
  • Parent commits d064959

Comments (0)

Files changed (5)

 
 organization := "uk.co.bigbeeconsultants"
 
-version := "0.18.11"
+version := "0.18.12"
 
 crossScalaVersions := Seq("2.9.0", "2.9.1", "2.9.2")
 
 #!/bin/bash -e
-V=0.12.0
+V=0.12.1
 
 cd $(dirname $0)
 if [ ! -f xsbt/bin/sbt-launch.jar ]; then

File src/main/scala/uk/co/bigbeeconsultants/http/response/ResponseBody.scala

 import uk.co.bigbeeconsultants.http.header.MediaType
 import uk.co.bigbeeconsultants.http.HttpClient
 import java.nio.ByteBuffer
+import uk.co.bigbeeconsultants.http.util.Splitter
 
 /**
  * Defines the outline of a response body. This may or may not be buffered in memory or streamed directly
   def asBytes: Array[Byte] = new Array[Byte](0)
 
   /**
-   * Gets the body as a string if available. By default, this merely returns an empty string, which may not be
-   * much use.
+   * Gets the body as a string, if available. If the data is binary, this method always returns a blank string.
    */
   def asString: String = ""
 
   /**
+   * Gets the body as a string split into lines, if available. If the data is binary, this method always returns
+   * an empty iterator.
+   */
+  def iterator: Iterator[String] = if (isTextual && asString.length > 0) new Splitter(asString, '\n') else Nil.iterator
+
+  /**
    * Tests whether this response body can be represented as text, or whether the data is binary.
    */
   def isTextual = true

File src/test/scala/uk/co/bigbeeconsultants/http/response/ByteBufferResponseBodyTest.scala

     body.asBytes should be(bytes)
     body.isTextual should be(true)
     body.toString should be(s)
+    val it = body.iterator
+    it.hasNext should be(true)
+    it.next should be(s)
+    it.hasNext should be(false)
   }
 
   test("ByteBufferResponseBody with text but without a body") {
     body.asBytes should be(new Array[Byte](0))
     body.isTextual should be(true)
     body.toString should be("")
+    body.iterator.hasNext should be(false)
   }
 
   test("ByteBufferResponseBody with binary but without a body") {
     body.asBytes should be(new Array[Byte](0))
     body.isTextual should be(false)
     body.toString should be("")
+    body.iterator.hasNext should be(false)
   }
 
   test("ByteBufferResponseBody with binary and a body") {
     body.asBytes should be(bytes)
     body.isTextual should be(false)
     body.toString should be("")
+    body.iterator.hasNext should be(false)
   }
 
   test("ByteBufferResponseBody with plain text but no media type") {

File src/test/scala/uk/co/bigbeeconsultants/http/response/ResponseBodyTest.scala

     body.asBytes should be(new Array[Byte](0))
     body.isTextual should be(true)
     body.toString should be("")
+    body.iterator.hasNext should be(false)
   }
 
   test("StringResponseBody with json body") {
     val s = """[ "Some json message text" ]"""
     val bytes = s.getBytes(HttpClient.UTF8)
     val mt = MediaType.APPLICATION_JSON
-    val body = new StringResponseBody(mt, s)
+    val body = new StringResponseBody(s, mt)
 
     body.contentType should be(mt)
     body.contentLength should be(bytes.length)
     body.asBytes should be(bytes)
     body.isTextual should be(true)
     body.toString should be(s)
+    val it = body.iterator
+    it.hasNext should be(true)
+    it.next should be(s)
+    it.hasNext should be(false)
   }
 }