Permalink
Browse files

Initial commit.

  • Loading branch information...
tommorris committed Jun 18, 2010
0 parents commit ee561739cfe6471c41546b36b9f0bf18ae3ce76f
@@ -0,0 +1,4 @@
+lib_managed/
+target/
+src_managed/
+project/boot/
@@ -0,0 +1,8 @@
+#Project properties
+#Tue Jun 15 00:29:58 BST 2010
+project.organization=org.tommorris
+project.name=fenghuang
+sbt.version=0.7.4
+project.version=1.0
+build.scala.versions=2.7.7
+project.initialize=false
@@ -0,0 +1,24 @@
+import sbt._
+import growl._
+
+class Blogtext(info: ProjectInfo) extends DefaultProject(info) with growl.GrowlingTests {
+ override def parallelExecution = true
+
+ val scalaToolsRepository = "Scala Tools Repository" at "http://scala-tools.org/repo-releases/"
+ val mockitoCoreRepository = "Mockito Core Repository" at "http://repo2.maven.org/maven2/org/mockito/mockito-core"
+ val mockitoAllRepository = "Mockito All Repository" at "http://repo2.maven.org/maven2/org/mockito/mockito-all"
+
+ override def libraryDependencies = Set(
+ "org.scala-tools.testing" % "specs" % "1.6.2.1" % "test",
+ "net.databinder" %% "dispatch" % "0.7.4",
+ "org.mockito" % "mockito-core" % "1.8.5",
+ "org.mockito" % "mockito-all" % "1.8.5",
+ "joda-time" % "joda-time" % "1.6"
+ ) ++ super.libraryDependencies
+
+ override val growlTestImages = GrowlTestImages(
+ Some("/Users/tom/code/sbt-growltest/images/scalalogo-green.png"),
+ Some("/Users/tom/code/sbt-growltest/images/scalalogo-red.png"),
+ Some("/Users/tom/code/sbt-growltest/images/scalalogo-red.png")
+ )
+}
@@ -0,0 +1,7 @@
+import sbt._
+class Plugins(info: ProjectInfo) extends PluginDefinition(info) {
+ val lessRepo = "lessis repo" at "http://repo.lessis.me"
+ val growl = "me.lessis" % "sbt-growl-plugin" % "0.0.5"
+}
+
+// vim: set ts=4 sw=4 et:
@@ -0,0 +1,45 @@
+import dispatch._
+import oauth._
+import OAuth._
+import scala.xml._
+import java.net.URI
+
+/**
+ * Main FireEagle consumer class. Used to connect to the API and to get back user and location objects.
+ *
+ * @author Tom Morris
+ */
+class FireEagle(val consumer: Consumer) {
+ def this(consumerToken: String, consumerSecret: String) = {
+ this(Consumer(consumerToken, consumerSecret))
+ }
+
+ def this(consumer: Consumer, accessToken: Token) = {
+ this(consumer)
+ this.accessToken = Some[Token](accessToken)
+ }
+
+ def this(consumerToken: String, consumerSecret: String, accessToken: String, accessSecret: String) {
+ this(Consumer(consumerToken, consumerSecret))
+ this.accessToken = Some[Token](Token(accessToken, accessSecret))
+ }
+
+ private val h = new Http()
+ private val fe_oauth = (:/("fireeagle.yahooapis.com") / "oauth").secure
+ private val fe = (:/("fireeagle.yahooapis.com") / "api" / "0.1").secure
+ var callbackUri = "oob"
+
+ var generalToken: Option[Token] = None
+ var requestToken: Option[Token] = None
+ var accessToken: Option[Token] = None
+
+ def getRequestToken() {
+ this.requestToken = Some[Token](this.h(this.fe_oauth / "request_token" << Map("oauth_callback" -> this.callbackUri) <@ this.consumer as_token))
+ }
+
+ def convertToAccessToken(verifier: String) {
+ this.accessToken = Some[Token](this.h(this.fe_oauth / "access_token" << Map("oauth_verifier" -> verifier) <@ (this.consumer, this.requestToken.get) as_token))
+ }
+
+ def getUserLocation() = XML.loadString(this.h(this.fe / "user" <@ (this.consumer, this.accessToken.get) as_str))
+}
@@ -0,0 +1,11 @@
+abstract class Level {
+ val identifier: Int
+}
+
+object Exact extends Level { val identifier = 0 }
+object Postal extends Level { val identifier = 1 }
+object Neighbourhood extends Level { val identifier = 2 }
+object City extends Level { val identifier = 3 }
+object Region extends Level { val identifier = 4 }
+object State extends Level { val identifier = 5 }
+object Country extends Level { val identifier = 6 }
@@ -0,0 +1,59 @@
+import scala.xml.Node
+import org.joda.time.DateTime
+import org.joda.time.format.ISODateTimeFormat
+
+/*
+<location best-guess="true">
+ ! <georss:point>51.5083 -0.125969</georss:point>
+ ! <label></label>
+ <level>0</level>
+ <level-name>exact</level-name>
+ ! <located-at>2010-06-17T11:37:13-07:00</located-at>
+ ! <name>Strand, London, WC2N 5</name>
+ ! <normal-name>Strand</normal-name>
+ ! <place-id exact-match="false">IR1c0qSbB5tPh9kZoQ</place-id>
+ ! <woeid exact-match="false">26788881</woeid>
+ ! <query>q=Strand%2C%20London%2C%20England</query>
+</location>
+*/
+
+/* tests todo:
+ - empty label */
+
+class Location {
+ var name: Option[String] = None
+ var normalName: Option[String] = None
+ var latitude: Option[Float] = None
+ var longitude: Option[Float] = None
+ var label: Option[String] = None
+ var dateTime: Option[DateTime] = None
+ var query: Option[String] = None
+ var woeId: Option[WoeId] = None
+ var placeId: Option[PlaceId] = None
+ var level: Option[Level] = None
+}
+object Location {
+ def fromXml(xml: Node): Location = new Location {
+ name = Some[String]((xml \ "name").text)
+ normalName = Some[String]((xml \ "normal-name").text)
+ latitude = Some[Float]((xml \ "georss:point").text.split(" ")(0).toFloat)
+ longitude = Some[Float]((xml \ "georss:point").text.split(" ")(1).toFloat)
+ label = if ((xml \ "label")(0).text.isEmpty) {
+ Some[String]((xml \ "label")(0).text)
+ } else None
+ dateTime = Some(ISODateTimeFormat.dateTimeParser().parseDateTime(
+ (xml \ "located-at").text))
+ query = Some[String]((xml \ "query").text)
+ woeId = Some[WoeId](WoeId.fromXml((xml \ "woeid")(0)))
+ placeId = Some[PlaceId](PlaceId.fromXml((xml \ "place-id")(0)))
+ level = (xml \ "level").text.toInt match {
+ case 0 => Some[Level](Exact)
+ case 1 => Some[Level](Postal)
+ case 2 => Some[Level](Neighbourhood)
+ case 3 => Some[Level](City)
+ case 4 => Some[Level](Region)
+ case 5 => Some[Level](State)
+ case 6 => Some[Level](Country)
+ }
+ }
+}
@@ -0,0 +1,6 @@
+import scala.xml.Node
+
+case class PlaceId(val identifier: String, val bestGuess: Boolean)
+object PlaceId {
+ def fromXml(xml: Node) = PlaceId(xml.text, xml.attributes("exact-match")(0).text.toBoolean )
+}
@@ -0,0 +1,8 @@
+import dispatch.oauth.Token
+import scala.xml._
+
+class User(private var fe: FireEagle) {
+/* def getLocationHierarchy() {
+*//* XML.loadString(this.fe.h(this.fe.fe / "user" <@ (this.fe.consumer, this.fe.accessToken.get) as_str))*/
+/* }*/
+}
@@ -0,0 +1,6 @@
+import scala.xml.Node
+
+case class WoeId(val identifier: Int, val bestGuess: Boolean)
+object WoeId {
+ def fromXml(xml: Node) = WoeId(xml.text.toInt, xml.attributes("exact-match")(0).text.toBoolean )
+}
@@ -0,0 +1,35 @@
+import org.specs._
+import org.specs.util._
+import dispatch._
+import oauth._
+
+trait FireEagleMocker extends FireEagle {
+ override def getRequestToken() {
+ this.requestToken = Some[Token](Token("0000000000","0000000000000000000000000000000"))
+ }
+
+ override def convertToAccessToken(verifier: String) {
+ this.accessToken = Some[Token](Token("1111111111","1111111111111111111111111111111"))
+ }
+}
+
+object FireEagleTest extends Specification {
+ "FireEagle" should {
+ "get request token" in {
+ val fe = new FireEagle("000000000000", "00000000000000000000000000000000") with FireEagleMocker
+ fe.requestToken.isDefined mustBe false
+ fe.getRequestToken()
+ fe.requestToken.isDefined mustBe true
+ fe.requestToken.get.value mustBe "0000000000"
+ fe.requestToken.get.secret mustBe "0000000000000000000000000000000"
+ }
+
+ "convert request token to access token" in {
+ val fe = new FireEagle("000000000000", "00000000000000000000000000000000") with FireEagleMocker
+ fe.getRequestToken()
+ fe.convertToAccessToken("abcdef")
+ fe.accessToken.get.value mustBe "1111111111"
+ fe.accessToken.get.secret mustBe "1111111111111111111111111111111"
+ }
+ }
+}
No changes.

0 comments on commit ee56173

Please sign in to comment.