Skip to content

Instantly share code, notes, and snippets.

@ThomasAlexandre
Created February 15, 2012 20:23
Show Gist options
  • Save ThomasAlexandre/1838767 to your computer and use it in GitHub Desktop.
Save ThomasAlexandre/1838767 to your computer and use it in GitHub Desktop.
JFokus 2012 v1 - sample from "One liners are your friend"
Http(url("http://api.groupon.com/v2/divisions.xml?client_id=<your_client_id>") >>> System.out)
Http(url("http://api.groupon.com/v2/divisions.xml?client_id=<your_client_id>") as_str)
val grouponCitiesURL = url("http://api.groupon.com/v2/divisions.xml?client_id=<your_client_id>")
Http(grouponCitiesURL as_str)
val citiesAsXML = Http(grouponCitiesURL <> { response => response \\ "division" })
val cityNames = citiesAsXML map ( city => (city \ "name").text )
val cityNames = citiesAsXML map ( city => (city \ "name").text ) filter(_.startsWith("H"))
val cityLocations = citiesAsXML map { city => ((city \ "name").text,(city \ "lat").text,(city \ "lng").text) }
cityLocations find (location => location._1 == "Honolulu")
cityLocations find (_._1 == "Honolulu")
cityLocations find (_._1 == "Honoulu")
val (honolulu,lat,lng) = cityLocations find (_._1 == "Honolulu") getOrElse("Honolulu","21","-157")
val dealsURL = url("http://api.groupon.com/v2/deals.xml?client_id=<your_client_id>")
val deals = Http(dealsURL <<? Map("lat"->lat,"lng"->lng) as_str)
case class Deal(title:String = "", dealUrl:String = "", tag:String = "")
val deals = Http(dealsURL <<? Map("lat"->lat, "lng"->lng) <> { xml => xml \\ "deal" map (deal => Deal((deal \\ "title").text,(deal \\ "dealUrl").text,(deal \\ "tag").text)) })
val deals = Http(dealsURL <<? Map("lat"->lat, "lng"->lng) <> { xml =>
for ( deal <- xml \\ "deal";
title = (deal \\ "title").text;
dealUrl = (deal \\ "dealUrl").text;
tag = (deal \\ "tag" \ "name").text) yield Deal(title,dealUrl,tag) })
deals.size
val sortedDeals = deals groupBy(_.tag)
sortedDeals.keys.size
val nbOfDealsPerTag = sortedDeals mapValues(_.size)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment