Skip to content

Commit

Permalink
Merge pull request #58 from prismicio/alw/links
Browse files Browse the repository at this point in the history
Alw/links
  • Loading branch information
srenault committed Apr 24, 2018
2 parents b2cc720 + 62b802e commit 6ea8ae5
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ You can find this library in our own Maven repository (hosted on Github).
Then add, the library dependency:

```
"io.prismic" %% "scala-kit" % "2.0.0"
"io.prismic" %% "scala-kit" % "2.4.1"
```

* *the kit requires **JDK 7** or above*
Expand Down
20 changes: 10 additions & 10 deletions src/main/scala/io/prismic/PrismicJsonProtocol.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ object PrismicJsonProtocol extends DefaultJsonProtocol with NullOptions {

implicit object FileLinkFormat extends RootJsonFormat[FileLink] {
override def read(json: JsValue): FileLink = FileLink(
(json \ "url").convertTo[String],
(json \ "kind").convertTo[String],
(json \ "size").convertTo[String].toLong,
(json \ "name").convertTo[String],
(json \ "target").toOpt[String]
(json \ "file" \ "url").convertTo[String],
(json \ "file" \ "kind").convertTo[String],
(json \ "file" \ "size").convertTo[String].toLong,
(json \ "file" \ "name").convertTo[String],
(json \ "file" \ "target").toOpt[String]
)
override def write(obj: FileLink): JsValue = throw new SerializationException("Not implemented")
}

implicit object ImageLinkFormat extends RootJsonFormat[ImageLink] {
override def read(json: JsValue): ImageLink = ImageLink(
(json \ "url").convertTo[String],
(json \ "kind").convertTo[String],
(json \ "size").convertTo[String].toLong,
(json \ "name").convertTo[String],
(json \ "target").toOpt[String]
(json \ "image" \ "url").convertTo[String],
(json \ "image" \ "kind").convertTo[String],
(json \ "image" \ "size").convertTo[String].toLong,
(json \ "image" \ "name").convertTo[String],
(json \ "image" \ "target").toOpt[String]
)

override def write(obj: ImageLink): JsValue = throw new SerializationException("Not implemented")
Expand Down
70 changes: 70 additions & 0 deletions src/test/scala/DocumentSpec.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.prismic

import io.prismic.fragments._
import org.joda.time.{DateTime, DateTimeZone}
import org.specs2.mutable._
import spray.json._
import io.prismic.PrismicJsonProtocol._
import io.prismic.fragments.AlternateLanguage

Expand Down Expand Up @@ -43,5 +45,73 @@ class DocumentSpec extends Specification {
have size 1 and
contain(AlternateLanguage("WmWkEyQAABvqGKrj", None, "store", "de-de"))
}

"should parse fetch links" in {
println("json")

val json = JsonParser(
"""
| {
| "id": "WRmlvyMAANV2plTU",
| "uid": null,
| "type": "home",
| "href": "https://prismicio-docs.cdn.prismic.io/api/v1/documents/search?ref=Wt4EwCcAAB1hFCNZ&q=%5B%5B%3Ad+%3D+at%28document.id%2C+%22WRmlvyMAANV2plTU%22%29+%5D%5D",
| "tags": [],
| "first_publication_date": "2017-05-15T13:52:06+0000",
| "last_publication_date": "2018-03-13T14:11:05+0000",
| "slugs": [
| "prismic.io-documentation",
| "api-clients"
| ],
| "linked_documents": null,
| "lang": "en-us",
| "alternate_languages": [],
| "data": {
| "home": {
| "link": {
| "type": "Link.document",
| "value": {
| "document": {
| "id": "WTquXikAACkA4Hux",
| "type": "page-javascript",
| "tags": [],
| "lang": "en-us",
| "slug": "page---javascript---integrating-with-an-existing-project",
| "uid": "integrating-with-an-existing-javascript-project",
| "data": {
| "page-javascript": {
| "category": {
| "type": "Link.document",
| "value": {
| "document": {
| "id": "WTl0SikAACYA2xXL",
| "lang": "en-us",
| "type": "category-javascript",
| "tags": [],
| "slug": "category---javascript---getting-started",
| "uid": "getting-started"
| }
| },
| "isBroken": false
| }
| }
| }
| },
| "isBroken": false
| }
| }
| }
| }
|}
""".stripMargin)

val doc = json.convertTo[Document]

val link = doc.fragments.get("home.link").get
val computed = link.asInstanceOf[DocumentLink].fragments.size


computed must_== 1
}
}
}
70 changes: 70 additions & 0 deletions src/test/scala/FragmentSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,74 @@ class FragmentSpec extends Specification {
json.convertTo[WebLink].asHtml() mustEqual """<a href="https://google.fr" target="_blank" rel="noopener">https://google.fr</a>"""
}
}

"FileLink" should {
"deserialize to fileLink without target" in {
val json = JsonParser(
"""
|{
| "file": {
| "url": "https://mondoc.com",
| "kind": "media",
| "size": "10",
| "name": "mondoc"
| }
|}
""".stripMargin)

json.convertTo[FileLink].asHtml() mustEqual """<a href="https://mondoc.com">mondoc</a>"""
}

"deserialize to fileLink with target" in {
val json = JsonParser(
"""
|{
| "file": {
| "url": "https://mondoc.com",
| "kind": "media",
| "size": "10",
| "name": "mondoc",
| "target": "_blank"
| }
|}
""".stripMargin)

json.convertTo[FileLink].asHtml() mustEqual """<a href="https://mondoc.com" target="_blank" rel="noopener">mondoc</a>"""
}
}

"ImageLink" should {
"deserialize to ImageLink without target" in {
val json = JsonParser(
"""
|{
| "image": {
| "url": "https://monmedia.com",
| "kind": "media",
| "size": "10",
| "name": "monmedia"
| }
|}
""".stripMargin)

json.convertTo[ImageLink].asHtml() mustEqual """<img src="https://monmedia.com" alt="monmedia"/>"""
}

"deserialize to ImageLink with target" in {
val json = JsonParser(
"""
|{
| "image": {
| "url": "https://monmedia.com",
| "kind": "media",
| "size": "10",
| "name": "monmedia",
| "target": "_blank"
| }
|}
""".stripMargin)

json.convertTo[ImageLink].asHtml() mustEqual """<a href="https://monmedia.com" target="_blank" rel="noopener"><img src="https://monmedia.com" alt="monmedia"/></a>"""
}
}
}

0 comments on commit 6ea8ae5

Please sign in to comment.