Skip to content

Commit

Permalink
Merge pull request #47 from prismicio/alw/slice
Browse files Browse the repository at this point in the history
add Slice type support
  • Loading branch information
srenault authored Mar 9, 2017
2 parents bd44ed8 + 7e73cdd commit 9f8b47c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
11 changes: 9 additions & 2 deletions src/main/scala/io/prismic/PrismicJsonProtocol.scala
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,18 @@ object PrismicJsonProtocol extends DefaultJsonProtocol with NullOptions {
implicit object SliceFormat extends RootJsonFormat[Slice] {
override def read(jsValue: JsValue): Slice = {
val json = jsValue.asJsObject
json.getFields("slice_type", "value") match {
json.getFields("slice_type", "value", "non-repeat", "repeat") match {
case Seq(JsString(sliceType), data: JsObject) =>
val sliceLabel = (json \ "slice_label").toOpt[String]
val fragment = data.convertTo[Fragment]
Slice(sliceType, sliceLabel, fragment)
SimpleSlice(sliceType, sliceLabel, fragment)

case Seq(JsString(sliceType), nonRepeat: JsObject, repeat: JsArray) =>
val sliceLabel = (json \ "slice_label").toOpt[String]
val nr = JsArray(nonRepeat).convertTo[Group].docs.headOption.getOrElse(Group.Doc(Map()))
val r = repeat.convertTo[Group]
CompositeSlice(sliceType, sliceLabel, nr, r)

case _ => throw new DeserializationException("Expected slice_type and value")
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/scala/io/prismic/ProxyServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ case class ProxyServer(
/** The port of the proxy server. */
port: Int,

@deprecated("Ignored")
@deprecated("Ignored", "1.3.4")
protocol: Option[String] = None,

/** The principal (aka username) of the credentials for the proxy server. */
Expand All @@ -19,13 +19,13 @@ case class ProxyServer(
/** The password for the credentials for the proxy server. */
password: Option[String] = None,

@deprecated("Ignored")
@deprecated("Ignored", "1.3.4")
ntlmDomain: Option[String] = None,

@deprecated("Ignored")
@deprecated("Ignored", "1.3.4")
encoding: Option[String] = None,

@deprecated("Ignored")
@deprecated("Ignored", "1.3.4")
nonProxyHosts: Option[Seq[String]] = None) {

}
21 changes: 20 additions & 1 deletion src/main/scala/io/prismic/fragments/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,26 @@ case class DocumentLink(id: String,

}

case class Slice(sliceType: String, sliceLabel: Option[String], value: Fragment) {
trait Slice {
def sliceType: String
def sliceLabel: Option[String]
def asHtml(linkResolver: DocumentLinkResolver): String
}

case class CompositeSlice(sliceType: String, sliceLabel: Option[String], nonRepeat: Group.Doc, repeat: Group) extends Slice {

def asHtml(linkResolver: DocumentLinkResolver): String = {
var className = (Seq("slice") ++ sliceLabel.toSeq).mkString(" ")
s"""<div data-slicetype="$sliceType" class="$className">
<div class="non-repeat">
${nonRepeat.fragments.toSeq.map{case (key, value) => Fragment.getHtml(value, linkResolver)}}
</div>
${repeat.asHtml(linkResolver)}
</div>"""
}
}

case class SimpleSlice(sliceType: String, sliceLabel: Option[String], value: Fragment) extends Slice {

def asHtml(linkResolver: DocumentLinkResolver): String = {
var className = (Seq("slice") ++ sliceLabel.toSeq).mkString(" ")
Expand Down
18 changes: 9 additions & 9 deletions src/test/scala/PaginationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@ class PaginationSpec extends Specification {
"first page" in {
val res = query(1)
res.page must_== 1
res.results.head.id must_== "V_OplCUAACQAE0lA"
res.results.head.id must_== "WHx-gSYAAMkyXYX_"
res.results.size must_== 20
res.resultsPerPage must_== 20
res.resultsSize must_== 20
res.totalPages must_== 1
res.nextPage must_== None
res.totalPages must_== 2
res.nextPage must_== Some("https://micro.prismic.io/api/documents/search?ref=WH8MzyoAAGoSGJwT&page=2&pageSize=20")
res.prevPage must_== None
}
"second page" in {
val res = query(2)
res.page must_== 2
res.results.size must_== 0
res.results.size must_== 4
res.resultsPerPage must_== 20
res.resultsSize must_== 0
res.totalPages must_== 1
res.resultsSize must_== 4
res.totalPages must_== 2
res.nextPage must_== None
res.prevPage must_== Some(s"https://micro.prismic.io/api/documents/search?ref=V_OpmSUAACcAE0lS&page=1&pageSize=20")
res.prevPage must_== Some(s"https://micro.prismic.io/api/documents/search?ref=WH8MzyoAAGoSGJwT&page=1&pageSize=20")
}
"setting page size" in {
val res = query(1, 7)
res.page must_== 1
res.results.size must_== 7
res.resultsPerPage must_== 7
res.resultsSize must_== 7
res.totalPages must_== 3
res.nextPage must_== Some(s"https://micro.prismic.io/api/documents/search?ref=V_OpmSUAACcAE0lS&page=2&pageSize=7")
res.totalPages must_== 4
res.nextPage must_== Some(s"https://micro.prismic.io/api/documents/search?ref=WH8MzyoAAGoSGJwT&page=2&pageSize=7")
res.prevPage must_== None
}
}
Expand Down

0 comments on commit 9f8b47c

Please sign in to comment.