Skip to content

Instantly share code, notes, and snippets.

@n8han
Last active December 12, 2015 08:39
Show Gist options
  • Save n8han/4745816 to your computer and use it in GitHub Desktop.
Save n8han/4745816 to your computer and use it in GitHub Desktop.
dispatch 0.9 meetup api example

!SLIDE

Demo

import dispatch._

!SLIDE

def api = host("api.meetup.com").secure
val key = Map("key" -> "yourapikey")

!SLIDE

def profiles(group: String) = (
    api / "2" / "profiles.xml" <<? 
      key ++ Map("group_urlname" -> group,
                 "order" -> "interesting") >
        as.xml.Elem
)

val profs = Http(profiles("Scala-Berlin-Brandenburg"))

!SLIDE

val ps = profs()

val names = for {
  name <- ps \\ "item" \ "name"
} yield name.text

!SLIDE

val bios = for {
  item <- ps \\ "item"
} yield {
  (item \ "bio").headOption.map {
    _.text
  }.getOrElse("")
}

!SLIDE

for {
  (name, bio) <- names.zip(bios).take(5)
} {
  println("%s\n%s\n".format(name,bio))
}

!SLIDE

Moar concurrency

val idsPromise =
  for (p <- profs)
    yield for (id <- (p \\ "member_id").take(10))
      yield id.text

!SLIDE

def groups(memberId: String) = (
  api / "2" / "groups.xml" <<? 
    key ++ Map("member_id" -> memberId) >
      as.xml.Elem
)

!SLIDE

val memberGroups = for {
  id <- idsPromise.values
  gs <- Http(groups(id))
} yield gs \\ "urlname"

!SLIDE

val flattened =
  for (members <- memberGroups)
    yield for {
      member <- members
      group <- member
    } yield group.text

!SLIDE

flattened().toSet.size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment