bitcoin-atm
bitcoin atm for pyc inc.
git clone https://9o.is/git/bitcoin-atm.git
PhoneNumber.scala
(1290B)
1 package inc.pyc.chimera
2 package minions
3
4 import ddb._
5 import akka.actor._
6 import akka.pattern._
7 import concurrent.Await
8
9 /**
10 * Minion that has access to the DDB PhoneNumber Table.
11 */
12 class PhoneNumberMinion
13 extends Actor
14 with ActorLogging
15 with Minion
16 with MinionWorker
17 with DDBMinion {
18
19 def props: Props = Props[PhoneNumber]
20 def name: String = "PhoneNumber_"+util.Random.nextInt
21
22 val tasks: Receive = {
23 case address: BitcoinAddress => logIn(address)
24 case (address: String, phone: Phone) => saveNumber(address, phone)
25 }
26
27 /**
28 * Logs in user with phone number.
29 */
30 def logIn(address: BitcoinAddress) = workout {
31 worker =>
32 val f = ask(worker, address).mapTo[Option[awscala.dynamodbv2.Item]]
33 val item = Await.result(f, timeout.duration)
34 if(item.isDefined) {
35 (item.map { i =>
36 val number = i.attributes.
37 find(_.name == "phone").get.value.getS()
38 sender ! Phone(number, true, true)
39 })
40 } else {
41 sender ! LoggedOut
42 }
43 }
44
45 /**
46 * Saves phone number with bitcoin address to Dynamo DB's PhoneNumber table.
47 * @param adress bitcoin address
48 * @param phone phone number
49 */
50 def saveNumber(address: String, phone: Phone) = workout(_ ! (address, phone))
51 }