Skip to content

Commit

Permalink
Merge pull request #55 from prismicio/sre/fetchlinks
Browse files Browse the repository at this point in the history
Fix fetchLinks with StructuredText
  • Loading branch information
srenault authored Jan 11, 2018
2 parents 1c8fb3f + 22c162a commit 54ef652
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 148 deletions.
16 changes: 8 additions & 8 deletions src/main/scala/io/prismic/HttpClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ object HttpClient {
val ch = chF.channel()

val request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, fullPath)
(headers ++ Map(
HttpHeaders.Names.USER_AGENT -> UserAgent,
HttpHeaders.Names.HOST -> host,
HttpHeaders.Names.CONNECTION -> HttpHeaders.Values.CLOSE,
HttpHeaders.Names.ACCEPT_ENCODING -> HttpHeaders.Values.GZIP
)).map { case (key, value) =>
request.headers().set(key, value)
}
(headers ++ Map(
HttpHeaders.Names.USER_AGENT -> UserAgent,
HttpHeaders.Names.HOST -> host,
HttpHeaders.Names.CONNECTION -> HttpHeaders.Values.CLOSE,
HttpHeaders.Names.ACCEPT_ENCODING -> HttpHeaders.Values.GZIP
)).map { case (key, value) =>
request.headers().set(key, value)
}

ch.writeAndFlush(request)
}
Expand Down
20 changes: 14 additions & 6 deletions src/main/scala/io/prismic/PrismicJsonProtocol.scala
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ object PrismicJsonProtocol extends DefaultJsonProtocol with NullOptions {
case Seq(JsString("Link.file"), value) => value.convertTo[FileLink]
case Seq(JsString("Link.image"), value) => value.convertTo[ImageLink]
case Seq(JsString("StructuredText"), value) => value.convertTo[StructuredText]
case Seq(JsString("Group"), value) => value.convertTo[Group]
case Seq(JsString("SliceZone"), value) => value.convertTo[SliceZone]
case Seq(JsString("Group"), value) => value.convertTo[Group](GroupFormat)
case Seq(JsString("SliceZone"), value) => value.convertTo[SliceZone](SliceZoneFormat)
case Seq(JsString("Color"), value) => value.convertTo[Color]
case Seq(JsString("Separator")) => Separator
case Seq(JsString(t), _) => throw new DeserializationException(s"Unkown fragment type: $t")
Expand Down Expand Up @@ -327,10 +327,18 @@ object PrismicJsonProtocol extends DefaultJsonProtocol with NullOptions {
def parseFragments(json: JsObject, typ: String): Map[String, Fragment] = {
val fields = json.fields.map {
case (key, jsobj: JsObject) => jsobj.toOpt[Fragment].toList.map(fragment => (s"$typ.$key", fragment))
case (key, jsons: JsArray) => jsons.elements.zipWithIndex.collect {
case (json: JsObject, i) => json.toOpt[Fragment].toList.map(fragment => (s"$typ.$key[$i]", fragment))
case (jsval, i) => Nil
}.flatten
case (key, jsons: JsArray) =>
jsons.toOpt[StructuredText] match {
case Some(structuredText) =>
Seq(s"$typ.$key" -> structuredText)

case None =>
jsons.elements.zipWithIndex.collect {
case (json: JsObject, i) => json.toOpt[Fragment].toList.map(fragment => (s"$typ.$key[$i]", fragment))
case (jsval, i) => Nil
}.flatten
}

case (key, jsval) => Nil
}.flatten.toSeq
collection.immutable.ListMap(fields:_*)
Expand Down
8 changes: 1 addition & 7 deletions src/test/scala/DocumentSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@ package io.prismic

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

class DocumentSpec extends Specification {

def fixture(file: String): JsValue = {
val content = scala.io.Source.fromFile(s"src/test/scala/fixtures/$file").mkString
JsonParser(content)
}

lazy val document = fixture("document_store.json").convertTo[Document]
lazy val document = Fixtures.document

"Document" should {
"have first publication" in {
Expand Down
14 changes: 14 additions & 0 deletions src/test/scala/Fixtures.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.prismic

import PrismicJsonProtocol._
import spray.json._

object Fixtures {

lazy val document: Document = Fixtures.load("document_store.json").convertTo[Document]

def load(file: String): JsValue = {
val content = scala.io.Source.fromFile(s"src/test/scala/fixtures/$file").mkString
JsonParser(content)
}
}
15 changes: 7 additions & 8 deletions src/test/scala/FragmentSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,13 @@ class FragmentSpec extends Specification {
}
}
"Link" should {
val api = await(Api.get("https://test-public.prismic.io/api"))
def query(q: String) = await(api.forms("everything").ref(api.master).query(q).submit())
val doc = query("""[[:d = at(document.id, "VFfjTSgAACYA86Zn")]]""").results.head
println(doc.getLink("product.gallery"))
"support image media" in {
doc getLink "product.link" must beSome.like {
case l: ImageLink => l.filename must_== "20130209_152532.jpg"
}
lazy val document = Fixtures.document
document.getLink("store.link") must beSome.like {
case d: DocumentLink =>
d.fragments.get("timestamp.text") must beSome.like {
case s: StructuredText =>
s.asHtml(resolver) must_== "<p>This is text</p>"
}
}
}
"Timestamp" should {
Expand Down
273 changes: 154 additions & 119 deletions src/test/scala/fixtures/document_store.json
Original file line number Diff line number Diff line change
@@ -1,124 +1,159 @@
{
"id": "UlfoxUnM0wkXYXbb", "type": "store", "href": "https://lesbonneschoses.prismic.io/api/documents/search?ref=UlfoxUnM08QWYXdl&q=%5B%5B%3Ad+%3D+at%28document.id%2C+%22UlfoxUnM0wkXYXbb%22%29+%5D%5D", "tags": [], "slugs": ["paris-saint-lazare"], "lang": "fr-fr", "alternate_languages":[], "first_publication_date": "2017-01-13T11:45:21.000Z", "last_publication_date": "2017-02-21T16:05:19.000Z", "linked_documents": [], "data": {
"id": "UlfoxUnM0wkXYXbb",
"type": "store",
"href": "https://lesbonneschoses.prismic.io/api/documents/search?ref=UlfoxUnM08QWYXdl&q=%5B%5B%3Ad+%3D+at%28document.id%2C+%22UlfoxUnM0wkXYXbb%22%29+%5D%5D",
"tags": [],
"slugs": [
"paris-saint-lazare"
],
"lang": "fr-fr",
"alternate_languages": [],
"first_publication_date": "2017-01-13T11:45:21.000Z",
"last_publication_date": "2017-02-21T16:05:19.000Z",
"linked_documents": [],
"data": {
"store": {
"name": {
"type": "StructuredText",
"value": [
{
"type": "heading1",
"text": "Paris Saint-Lazare",
"spans": []
}
]
},
"address": {
"type": "Text",
"value": "56 rue Saint-Lazare"
},
"city": {
"type": "Text",
"value": "PARIS"
},
"zipcode": {
"type": "Text",
"value": "75009"
},
"country": {
"type": "Select",
"value": "France"
},
"coordinates": {
"type": "GeoPoint",
"value": {
"latitude": 48.877108,
"longitude": 2.3338790
}
},
"description": {
"type": "StructuredText",
"value": [
{
"type": "paragraph",
"text": "Our headquarters are also the setting of our oldest shop, where our pastries are highlighted by a traditional Parisian-bakery style.",
"spans": []
}
]
},
"image": {
"type": "Image",
"value": {
"main": {
"url": "https://prismic-io.s3.amazonaws.com/lesbonneschoses/becaf8a6343465b04bcfb0fe1807d6607231ec7a.jpg",
"alt": "",
"copyright": "",
"dimensions": {
"width": 1500,
"height": 500
}
},
"views": {
"medium": {
"url": "https://prismic-io.s3.amazonaws.com/lesbonneschoses/ed23b65bf3ace612924487d09718806df74bc48f.jpg",
"alt": "",
"copyright": "",
"dimensions": {
"width": 800,
"height": 250
}
},
"icon": {
"url": "https://prismic-io.s3.amazonaws.com/lesbonneschoses/dab1e5fc3e85f71dd6f2d3c01cc6a0c08fcf12f6.jpg",
"alt": "",
"copyright": "",
"dimensions": {
"width": 250,
"height": 250
}
}
}
}
},
"monday": [
{
"type": "Text",
"value": "2pm - 10pm"
}
],
"tuesday": [
{
"type": "Text",
"value": "2pm - 10pm"
}
],
"wednesday": [
{
"type": "Text",
"value": "2pm - 10pm"
}
],
"thursday": [
{
"type": "Text",
"value": "2pm - 10pm"
}
],
"friday": [
{
"type": "Text",
"value": "2pm - 10pm"
}
],
"saturday": [
{
"type": "Text",
"value": "2pm - 10pm"
"name": {
"type": "StructuredText",
"value": [
{
"type": "heading1",
"text": "Paris Saint-Lazare",
"spans": []
}
]
},
"address": {
"type": "Text",
"value": "56 rue Saint-Lazare"
},
"city": {
"type": "Text",
"value": "PARIS"
},
"zipcode": {
"type": "Text",
"value": "75009"
},
"country": {
"type": "Select",
"value": "France"
},
"coordinates": {
"type": "GeoPoint",
"value": {
"latitude": 48.877108,
"longitude": 2.333879
}
},
"description": {
"type": "StructuredText",
"value": [
{
"type": "paragraph",
"text": "Our headquarters are also the setting of our oldest shop, where our pastries are highlighted by a traditional Parisian-bakery style.",
"spans": []
}
]
},
"link": {
"type": "Link.document",
"value": {
"document": {
"id": "WjfSNSIAAH_Uc90o",
"type": "timestamp",
"tags": [],
"lang": "es-es",
"slug": "qsmdkqlsdk",
"data": {
"timestamp": {
"text": [{
"type": "paragraph",
"text": "This is text",
"spans": []
}]
}
}
},
"isBroken": false
}
},
"image": {
"type": "Image",
"value": {
"main": {
"url": "https://prismic-io.s3.amazonaws.com/lesbonneschoses/becaf8a6343465b04bcfb0fe1807d6607231ec7a.jpg",
"alt": "",
"copyright": "",
"dimensions": {
"width": 1500,
"height": 500
}
],
"sunday": [
{
"type": "Text",
"value": "Closed"
},
"views": {
"medium": {
"url": "https://prismic-io.s3.amazonaws.com/lesbonneschoses/ed23b65bf3ace612924487d09718806df74bc48f.jpg",
"alt": "",
"copyright": "",
"dimensions": {
"width": 800,
"height": 250
}
},
"icon": {
"url": "https://prismic-io.s3.amazonaws.com/lesbonneschoses/dab1e5fc3e85f71dd6f2d3c01cc6a0c08fcf12f6.jpg",
"alt": "",
"copyright": "",
"dimensions": {
"width": 250,
"height": 250
}
}
]
}
}
},
"monday": [
{
"type": "Text",
"value": "2pm - 10pm"
}
],
"tuesday": [
{
"type": "Text",
"value": "2pm - 10pm"
}
],
"wednesday": [
{
"type": "Text",
"value": "2pm - 10pm"
}
],
"thursday": [
{
"type": "Text",
"value": "2pm - 10pm"
}
],
"friday": [
{
"type": "Text",
"value": "2pm - 10pm"
}
],
"saturday": [
{
"type": "Text",
"value": "2pm - 10pm"
}
],
"sunday": [
{
"type": "Text",
"value": "Closed"
}
]
}
}}
}
}

0 comments on commit 54ef652

Please sign in to comment.