Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bump to sbt 0.12.0 and clean up code a bit

  • Loading branch information...
commit 42ec2f801fc80bf0cb24219e6964f0a30ed01a90 1 parent d193138
@daggerrz authored
View
4 core/build.sbt
@@ -1,7 +1,9 @@
name := "Scwilio Core"
+scalaVersion := "2.9.2"
+
libraryDependencies ++= Seq(
- "net.databinder" %% "dispatch-http" % "0.8.5",
+ "net.databinder" %% "dispatch-http" % "0.8.8",
"org.slf4j" % "slf4j-api" % "1.6.1",
"ch.qos.logback" % "logback-classic" % "0.9.26",
"org.scala-tools.testing" %% "specs" % "1.6.9" % "test",
View
33 core/src/main/scala/scwilio/devices.scala → core/src/main/scala/scwilio/Phone.scala
@@ -13,27 +13,23 @@ trait Phone { self: CallbackManager with Logging =>
/**
* Set this to handle incoming calls.
*/
- var incomingCallHandler: Option[(ActiveCall) => VoiceResponse] = None
+ def incomingCallHandler: (ActiveCall) => VoiceResponse
/**
* Called when an incoming call arrives.
*/
def handleIncomingCall(call: ActiveCall) : VoiceResponse = {
log.debug("Incoming call: " + call)
- incomingCallHandler match {
- case Some(f) => f.apply(call)
- case _ => Say("Hello, thanks for calling, but incoming calls are not supported by this server.")
- }
+ incomingCallHandler(call)
}
/**
* Called when a no-param callback is invoked. An example is waitUrl for conference calls.
*/
def handleNoParam(fid: String) : VoiceResponse = {
- getAndRemove[ActiveCall, VoiceResponse](fid) match {
- case Some(callback) => callback.apply(null)
- case _ => Say("Sorry, an error has occured. Do not know how to handle this callback.")
- }
+ getAndRemove[ActiveCall, VoiceResponse](fid).map(_.apply(null)).getOrElse(
+ Say("Sorry, an error has occured. Do not know how to handle this callback.")
+ )
}
/**
@@ -41,10 +37,9 @@ trait Phone { self: CallbackManager with Logging =>
*/
def handleCallStatus(fid: String, call: ActiveCall) : VoiceResponse = {
log.debug("Call connected: " + call)
- getAndRemove[ActiveCall, VoiceResponse](fid) match {
- case Some(callback) => callback.apply(call)
- case _ => Say("Sorry, an error has occured. Do not know how to handle this call.")
- }
+ getAndRemove[ActiveCall, VoiceResponse](fid).map(_.apply(call)).getOrElse(
+ Say("Sorry, an error has occured. Do not know how to handle this call.")
+ )
}
/**
@@ -52,10 +47,7 @@ trait Phone { self: CallbackManager with Logging =>
*/
def handleCallEnded(fid: String, outcome: CompletedCall) : Unit = {
log.debug("Call ended: " + outcome)
- getAndRemove[CompletedCall, VoiceResponse](fid) match {
- case Some(callback) => callback.apply(outcome)
- case _ => log.warn("No handler for call end " + fid)
- }
+ getAndRemove[CompletedCall, VoiceResponse](fid).foreach(_.apply(outcome))
}
/**
@@ -63,9 +55,8 @@ trait Phone { self: CallbackManager with Logging =>
*/
def handleOutgoingDialEnded(fid: String, outcome: CompletedOutgoingDial) : VoiceResponse = {
log.debug("Outgoing dial ended: " + outcome)
- getAndRemove[CompletedOutgoingDial, VoiceResponse](fid) match {
- case Some(callback) => callback.apply(outcome)
- case _ => Say("Sorry, an error has occured. Do not know how to handle this call.")
- }
+ getAndRemove[CompletedOutgoingDial, VoiceResponse](fid).map(_.apply(outcome)).getOrElse(
+ Say("Sorry, an error has occured. Do not know how to handle this call.")
+ )
}
}
View
2  ...ala/scwilio/callback/callbackevents.scala → ...cala/scwilio/callback/CallbackEvent.scala
@@ -50,7 +50,7 @@ object ActiveCall {
case Some(s) => Unknown(s)
case None => Unknown("no status")
},
- Phonenumber(p.get("ForwardedFrom")),
+ p.get("ForwardedFrom").map(Phonenumber.parse),
p.get("AnsweredBy") match {
case Some("human") => Some(Human)
case Some("machine") => Some(Machine)
View
59 ...rc/main/scala/scwilio/op/operations.scala → ...in/scala/scwilio/op/TwilioOperation.scala
@@ -1,5 +1,5 @@
-package scwilio {
-package op {
+package scwilio.op
+import scwilio._
import xml._
@@ -52,9 +52,7 @@ protected object XmlPredef {
case class ListAvailableNumbers(countryCode: String) extends TwilioOperation[Seq[Phonenumber]] {
def request(conf: HttpConfig) = conf.API_BASE / "AvailablePhoneNumbers" / countryCode / "Local"
- def parser = parse _
-
- def parse(nodes: NodeSeq) = {
+ val parser = { nodes: NodeSeq =>
for (num <- nodes \\ "AvailablePhoneNumber" \ "PhoneNumber")
yield {
Phonenumber(num.text)
@@ -83,19 +81,18 @@ case class DialOperation(
) extends TwilioOperation[CallInfo] {
def request(conf: HttpConfig) = {
- var params = Map(
+ val params = Map(
"From" -> from.toStandardFormat,
"To" -> to.toStandardFormat
- )
- callbackUrl.foreach(params += "Url" -> _)
- statusCallbackUrl.foreach(params += "StatusCallback" -> _)
- if (machineDetection) params += "IfMachine" -> "Continue"
-
+ ) ++
+ callbackUrl.map("Url" -> _) ++
+ statusCallbackUrl.map("StatusCallback" -> _) ++
+ (if (machineDetection) Some("IfMachine" -> "Continue") else None)
conf.API_BASE / "Calls" << params
}
- def parser = DialOperation.parse
+ def parser = DialOperation.parse _
}
object DialOperation {
@@ -122,7 +119,7 @@ case class SendSms(from: Phonenumber, to: Phonenumber, body: String) extends Twi
conf.API_BASE / "SMS" / "Messages" << params
}
- def parser = SendSms.parse
+ def parser = SendSms.parse _
}
object SendSms {
@@ -146,14 +143,7 @@ object SendSms {
*/
case class UpdateIncomingNumberConfig(sid: String, config: IncomingNumberConfig) extends TwilioOperation[IncomingNumber] {
def request(conf: HttpConfig) = {
- var params = Map(
- "ApiVersion" -> Twilio.API_VERSION,
- "VoiceMethod" -> "POST",
- "VoiceFallbackMethod" -> "POST",
- "StatusCallbackMethod" -> "POST",
- "SmsMethod" -> "POST",
- "SmsFallbackMethod" -> "POST"
- )
+
val options = List(
(config.friendlyName -> "FriendlyName"),
(config.voiceUrl ->"VoiceUrl"),
@@ -161,10 +151,16 @@ case class UpdateIncomingNumberConfig(sid: String, config: IncomingNumberConfig)
(config.smsUrl -> "SmsUrl"),
(config.smsFallbackUrl -> "SmsFallbackUrl")
)
- params ++= options.flatMap {
- case (Some(opt), setting) => List(setting -> opt)
- case _ => Nil
- }
+
+ val params = Map(
+ "ApiVersion" -> Twilio.API_VERSION,
+ "VoiceMethod" -> "POST",
+ "VoiceFallbackMethod" -> "POST",
+ "StatusCallbackMethod" -> "POST",
+ "SmsMethod" -> "POST",
+ "SmsFallbackMethod" -> "POST"
+ ) ++ options.flatMap{ case (value, optionName) => value.map(optionName -> _) }
+
conf.API_BASE / "IncomingPhoneNumbers" / sid << params
}
@@ -196,21 +192,20 @@ object IncomingNumbersParser {
*/
case object ListIncomingNumbers extends TwilioOperation[Seq[IncomingNumber]] {
def request(conf: HttpConfig) = conf.API_BASE / "IncomingPhoneNumbers"
- def parser = IncomingNumbersParser.parse
+ def parser = IncomingNumbersParser.parse _
}
/**
* Get the URIs for the participants resources in a conference.
*/
-case class GetConferenceParticipantURIs(cid: String) extends TwilioOperation[Tuple2[String, Seq[String]]] {
+case class GetConferenceParticipantURIs(cid: String) extends TwilioOperation[(String, Seq[String])] {
def request(conf: HttpConfig) = conf.API_BASE / "Conferences" / cid
- def parser = parse
-
- def parse(res: NodeSeq) = {
+ def parser = { res: NodeSeq =>
val conf = res \ "Conference"
(conf \ "Status").text -> (conf \ "SubresourceUris" \ "Participants").map{ _.text }
}
+
}
/**
@@ -219,10 +214,8 @@ case class GetConferenceParticipantURIs(cid: String) extends TwilioOperation[Tup
case class GetConferenceParticipantInfo(uri: String) extends TwilioOperation[ConferenceParticipant] {
def request(conf: HttpConfig) = conf.TWILIO_BASE / uri
- def parser = parse
- def parse(res: NodeSeq) = {
+ def parser = { res: NodeSeq =>
val part = res \ "Participant"
ConferenceParticipant((part \ "CallSid").text, if ("true" == (part \ "Muted").text) true else false)
}
}
-}}
View
19 core/src/main/scala/scwilio/phonenumber.scala
@@ -19,7 +19,7 @@ case class Phonenumber(countryCode: String, number: String) {
*
* @return
*/
- def toStandardFormat = "+" + countryCode + number
+ lazy val toStandardFormat = "+" + countryCode + number
override def toString = toStandardFormat
@@ -27,10 +27,6 @@ case class Phonenumber(countryCode: String, number: String) {
object Phonenumber {
def apply(number: String) : Phonenumber = parse(number)
- def apply(number: Option[String]) : Option[Phonenumber] = number match {
- case Some(num) => Some(parse(num))
- case None => None
- }
def parse(string: String) = PhonenumberParser.parse(string)
implicit def string2Phonenumber(number: String) : Phonenumber = Phonenumber.apply(number)
@@ -63,17 +59,6 @@ object PhonenumberParser {
parseAndDetermineCountryCodeIfPresent(trimmed)
}
- /**
- * Parse a phonenumber if it is paresable.
- */
- def parseOption(number: String) : Option[Phonenumber] = {
- try {
- Some(parse(number))
- } catch {
- case e: IllegalArgumentException => None
- }
- }
-
private def parseAndDetermineCountryCodeIfPresent(number: String) : Phonenumber = {
if (number.startsWith("+")) {
val numberWithoutPlus = number.substring(1)
@@ -114,7 +99,7 @@ object PhonenumberParser {
new Phonenumber(countryCode, number)
}
- private val countryCodes = Map(
+ private final val countryCodes = Map(
"355" -> "Albania",
"213" -> "Algeria",
"376" -> "Andorra",
View
0  .../src/main/scala/scwilio/twiml/verbs.scala → core/src/main/scala/scwilio/twiml/Verb.scala
File renamed without changes
View
6 core/src/test/scala/scwilio/op/OperationsSpec.scala
@@ -43,7 +43,7 @@ object OperationsSpec extends Specification {
</AvailablePhoneNumber>
</AvailablePhoneNumbers>
</TwilioResponse>
- val numbers = new ListAvailableNumbers(null).parse(res)
+ val numbers = new ListAvailableNumbers(null).parser(res)
numbers.size must_== 2
}
}
@@ -171,7 +171,7 @@ object OperationsSpec extends Specification {
</Conference>
</TwilioResponse>
- val (state, uris) = new GetConferenceParticipantURIs(null).parse(res)
+ val (state, uris) = new GetConferenceParticipantURIs(null).parser(res)
state must_== "completed"
uris.size must_== 1
uris.head must_== "/2010-04-01/Accounts/AC5ef872f6da5a21de157d80997a64bd33/Conferences/CFbbe46ff1274e283f7e3ac1df0072ab39/Participants"
@@ -193,7 +193,7 @@ object OperationsSpec extends Specification {
</Participant>
</TwilioResponse>
- val p = new GetConferenceParticipantInfo(null).parse(res)
+ val p = new GetConferenceParticipantInfo(null).parser(res)
p must_== ConferenceParticipant("CA386025c9bf5d6052a1d1ea42b4d16662", true)
}
}
View
2  project/build.properties
@@ -1 +1 @@
-sbt.version=0.10.1
+sbt.version=0.12.0
Please sign in to comment.
Something went wrong with that request. Please try again.