EP / me2day-scala
me2day API for scala
$ hg clone http://bitbucket.org/EP/me2day-scala/
| commit 36: | c9e257088350 |
| parent 35: | 5c9d123d00a1 |
| branch: | default |
12 months ago
Changed (Δ2.4 KB):
src/main/scala/net/me2day/scala/Command.scala (13 lines added, 0 lines removed)
src/main/scala/net/me2day/scala/Me2Day.scala (10 lines added, 39 lines removed)
src/main/scala/net/me2day/scala/TrackCommentsCommand.scala (29 lines added, 0 lines removed)
src/test/scala/net/me2day/scala/TrackCommentsCommandSpec.scala (41 lines added, 0 lines removed)
Up to file-list src/main/scala/net/me2day/scala/Command.scala:
1 |
package net.me2day.scala |
|
2 |
||
3 |
import collection.mutable.Map |
|
4 |
||
5 |
abstract class Command[T](val name : String, val method : String) { |
|
6 |
val parameters = Map[String, Any]() |
|
7 |
||
8 |
def add(name : String, value : Any) { |
|
9 |
parameters(name) = value |
|
10 |
} |
|
11 |
||
12 |
def parseResponse(node : xml.Node) : T |
|
13 |
} |
Up to file-list src/main/scala/net/me2day/scala/Me2Day.scala:
| … | … | @@ -32,7 +32,15 @@ object Me2Day { |
32 |
32 |
|
33 |
33 |
class Me2Day(val application : Application, val user : User) { |
34 |
34 |
val baseUrl = "http://me2day.net/api" |
35 |
||
35 |
||
36 |
def execute[T](command : Command[T]) : T = { |
|
37 |
val invocation = prepareInvocation(command.name, command.method) |
|
38 |
command.parameters.foreach { x => |
|
39 |
invocation.addParameter(x._1, x._2.toString) |
|
40 |
} |
|
41 |
command.parseResponse(invocation.execute()) |
|
42 |
} |
|
43 |
||
36 |
44 |
def noop() = { |
37 |
45 |
new NoopCommand(this) |
38 |
46 |
} |
| … | … | @@ -58,11 +66,7 @@ class Me2Day(val application : Applicati |
58 |
66 |
} |
59 |
67 |
|
60 |
68 |
def getComments(postId : String) = { |
61 |
new GetComments(this, postId) |
|
62 |
} |
|
63 |
||
64 |
def trackComments(userId : String) = { |
|
65 |
new |
|
69 |
new GetCommentsCommand(this, postId) |
|
66 |
70 |
} |
67 |
71 |
|
68 |
72 |
def getPerson(userId : String) = { |
| … | … | @@ -107,38 +111,5 @@ class Me2Day(val application : Applicati |
107 |
111 |
|
108 |
112 |
import java.util.Date |
109 |
113 |
|
110 |
class TrackComments(me2day : Me2Day, userId : String) { |
|
111 |
var scope : String = "all" |
|
112 |
var count_ : Option[Int] = None |
|
113 |
||
114 |
def all() = { |
|
115 |
scope = "all" |
|
116 |
} |
|
117 |
||
118 |
def by() = { |
|
119 |
scope = "by_me" |
|
120 |
this |
|
121 |
} |
|
122 |
||
123 |
def to() = { |
|
124 |
scope = "to_me" |
|
125 |
this |
|
126 |
} |
|
127 |
||
128 |
def count(count : Int) = { |
|
129 |
count_ = Some(count) |
|
130 |
this |
|
131 |
} |
|
132 |
||
133 |
def execute() : List[CommentTrace] = { |
|
134 |
val invocation = me2day.prepareInvocation("track_comments/" + userId, "GET") |
|
135 |
invocation.addParameter("scope", scope) |
|
136 |
count_ match { |
|
137 |
case Some(value) => invocation.addParameter("count", value.toString) |
|
138 |
case None => |
|
139 |
} |
|
140 |
invocation.resultParser.commentTraces(invocation.execute()) |
|
141 |
} |
|
142 |
} |
|
143 |
114 |
|
144 |
115 |
case class ErrorResult(code : Int, message : String, description : String) |
Up to file-list src/main/scala/net/me2day/scala/TrackCommentsCommand.scala:
1 |
package net.me2day.scala |
|
2 |
||
3 |
class TrackCommentsCommand(userId : String) |
|
4 |
extends Command[List[CommentTrace]]("track_comments/" + userId, "GET") { |
|
5 |
||
6 |
def all() = { |
|
7 |
add("scope", "all") |
|
8 |
this |
|
9 |
} |
|
10 |
||
11 |
def by() = { |
|
12 |
add("scope", "by_me") |
|
13 |
this |
|
14 |
} |
|
15 |
||
16 |
def to() = { |
|
17 |
add("scope", "to_me") |
|
18 |
this |
|
19 |
} |
|
20 |
||
21 |
def count(count : Int) = { |
|
22 |
add("count", count) |
|
23 |
this |
|
24 |
} |
|
25 |
||
26 |
def parseResponse(node : xml.Node) : List[CommentTrace] = { |
|
27 |
new XmlResultParser().commentTraces(node) |
|
28 |
} |
|
29 |
} |
Up to file-list src/test/scala/net/me2day/scala/TrackCommentsCommandSpec.scala:
1 |
package net.me2day.scala |
|
2 |
||
3 |
import org.specs.Specification |
|
4 |
import org.specs.mock.JMocker |
|
5 |
import org.specs.mock.ClassMocker |
|
6 |
import org.specs.runner.JUnit4 |
|
7 |
||
8 |
class TrackCommentsCommandTest extends JUnit4(TrackCommentsCommandSpec) |
|
9 |
||
10 |
object TrackCommentsCommandSpec extends Specification with JMocker with ClassMocker { |
|
11 |
var dut : TrackCommentsCommand = _ |
|
12 |
||
13 |
doBefore { |
|
14 |
dut = new TrackCommentsCommand("someone") |
|
15 |
dut.name must_== "track_comments/someone" |
|
16 |
} |
|
17 |
||
18 |
"scope all" in { |
|
19 |
dut.all() |
|
20 |
dut.parameters("scope") must_== "all" |
|
21 |
} |
|
22 |
||
23 |
"scope to" in { |
|
24 |
dut.to() |
|
25 |
dut.parameters("scope") must_== "to_me" |
|
26 |
} |
|
27 |
||
28 |
"scope by" in { |
|
29 |
dut.by() |
|
30 |
dut.parameters("scope") must_== "by_me" |
|
31 |
} |
|
32 |
||
33 |
"count" in { |
|
34 |
dut.count(10) |
|
35 |
dut.parameters("count") must_== 10 |
|
36 |
} |
|
37 |
||
38 |
"response" in { |
|
39 |
dut.parseResponse(<commentLogs></commentLogs>).isInstanceOf[List[CommentTrace]] must_== true |
|
40 |
} |
|
41 |
} |
