Skip to content

Instantly share code, notes, and snippets.

@sumerman sumerman/WunderlistJsonIteratee.sc Secret
Last active Aug 29, 2015

Embed
What would you like to do?
import java.util.Date
import net.fortuna.ical4j.model.component.VEvent
import net.fortuna.ical4j.model.property.Summary
import play.api.libs.iteratee._
import play.api.libs.json.JsString
import play.extras.iteratees.{CharString, Combinators, Encoding}
import play.extras.iteratees.JsonIteratees._
import play.extras.iteratees.JsonEnumeratees._
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import net.fortuna.ical4j.model._
val json = """
|{
| "id": 1051180611,
| "created_at": "2015-03-18T17:38:35.583Z",
| "recurrence_type": "day",
| "recurrence_count": 1,
| "due_date": "2015-03-19",
| "completed": false,
| "starred": false,
| "list_id": 121063920,
| "revision": 1,
| "title": "Заправить диван",
| "type": "task"
|}
|""".stripMargin
val json2 = """
|{
| "id": 1051180611,
| "created_at": "2015-03-18T17:38:35.583Z",
| "title": "Жепь",
| "type": "task"
|}
|""".stripMargin
val json3 = s"[$json,$json2,$json]"
val timeFormat = new java.text.SimpleDateFormat("yyyy-MM-dd")
val ignoreObject = new property.Tel()
val taskSchema = jsObject { field:String =>
field match {
case "id" =>
jsNumber map { idjs =>
new property.Uid(s"wunderlist-task-${idjs.value.toString()}")
}
case "due_date" =>
jsNullOr(jsString) map {
case None => ignoreObject
case Some(str) =>
val javaDate = timeFormat.parse(str.value)
val calDate = new net.fortuna.ical4j.model.Date(javaDate)
new property.DtStart(calDate)
}
case "title" =>
jsNullOr(jsString) map { topt =>
new property.Summary(topt.getOrElse(JsString("")).value)
}
case _ => jsValue.map(_ => ignoreObject)
}
}
val ignore = Enumeratee.filterNot[Property](_ == ignoreObject)
val buildEvent = Iteratee.fold[Property, Option[VEvent]](None) {
(maybeEvent, prop) =>
val event = maybeEvent.getOrElse(new VEvent())
event.getProperties.add(prop)
Some(event)
}.map(_.getOrElse(new VEvent()))
val task = taskSchema ><> ignore &>> buildEvent
val ignoreNoDate = Enumeratee.filterNot[VEvent](_.getStartDate == null)
val buildCalendar = Iteratee.fold[VEvent, Option[Calendar]](None) {
(maybeCal, event) =>
val cal = maybeCal.getOrElse(new Calendar())
cal.getComponents.add(event)
Some(cal)
}.map(_.getOrElse(new Calendar()))
val taskList = jsArray(_ => task) ><> ignoreNoDate &>> buildCalendar
// Encoding.decode() ><> Combinators.errorReporter ><>
Await.result(
Enumerator(CharString.fromString(json3)) |>>> taskList,
1 second
)
@sumerman

This comment has been minimized.

Copy link
Owner Author

commented Mar 19, 2015

Output is:

BEGIN:VCALENDAR
BEGIN:VEVENT
DTSTAMP:20150319T125117Z
UID:wunderlist-task-1051180611
DTSTART;VALUE=DATE:20150318
SUMMARY:Заправить диван
END:VEVENT
BEGIN:VEVENT
DTSTAMP:20150319T125117Z
UID:wunderlist-task-1051180611
DTSTART;VALUE=DATE:20150318
SUMMARY:Заправить диван
END:VEVENT
END:VCALENDAR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.