Skip to content

Instantly share code, notes, and snippets.

@alternegro
Created January 18, 2013 20:43
Show Gist options
  • Save alternegro/4568362 to your computer and use it in GitHub Desktop.
Save alternegro/4568362 to your computer and use it in GitHub Desktop.
play scala soap client plus json transformation
def interday(symbol:String, range:String) = Action {request =>
Async {
WS.url("http://api.sproutwerks.com/api/TimeSeries/TimeSeries.svc").withHeaders(CONTENT_TYPE -> "application/soap+xml").post(ChartRequests.interdayRequest(symbol, range)).map { response =>
Ok(timeSeriesToJson(range.toUpperCase, response.xml, noFilter, timeLabeler(range))).as(JSON)
}
}
}
def intraday(symbol:String) = Action {request =>
Async {
WS.url("http://api.sproutwerks.com/api/TimeSeries/TimeSeries.svc").withHeaders(CONTENT_TYPE -> "application/soap+xml").post(ChartRequests.intradayRequest(symbol)).map { response =>
Ok(timeSeriesToJson("DAY", response.xml, every15Minutes, {x:String => x.slice(11, 16)})).as(JSON)
}
}
}
val every5Minutes = {x:Map[String, Any] => x("timeLabel").asInstanceOf[String].last == '0'}
val every15Minutes = {x:Map[String, Any] => Set("00", "15", "30", "45").contains(x("timeLabel").asInstanceOf[String].slice(3,5))}
val noFilter = {x:Map[String, Any] => true}
def timeSeriesToJson(range:String, payload:scala.xml.Elem, timeFilter:Map[String,Any] => Boolean, timeLabeler:String => String): String = {
val series = (payload \\ "Row").map(row=> Map("open" -> nodeToDouble(row, "OPEN"),
"high" -> nodeToDouble(row, "HIGH"),
"low" -> nodeToDouble(row, "LOW"),
"close" -> nodeToDouble(row, "CLOSE"),
"volume" -> nodeToDouble(row, "VOLUME"),
"timestamp" -> ParseHelper.parseDate((row \\ "TIMESTAMP").text),
"timeLabel" -> timeLabeler((row \\ "TIMESTAMP").text)))
generate(Map(
"grid" -> range,
"labels" -> timeSeriesLabels(range, series),
"points" -> series.filter(timeFilter)
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment