Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial commit.

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

0 notes on commit ee56173

Please sign in to comment.
Something went wrong with that request. Please try again.