Skip to content

Commit 7c95b8b

Browse files
committed
Add ULI check digit API
1 parent 00980be commit 7c95b8b

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package hmda.api.model.public
2+
3+
object ULIModel {
4+
5+
case class Loan(loanId: String)
6+
case class ULI(loanId: String, checkDigit: Int, uli: String)
7+
case class ULICheck(uli: String)
8+
case class ULIValidated(isValid: Boolean)
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package hmda.api.protocol.public
2+
3+
import hmda.api.model.public.ULIModel.{ Loan, ULI, ULICheck, ULIValidated }
4+
import spray.json.DefaultJsonProtocol
5+
6+
object ULIProtocol extends DefaultJsonProtocol {
7+
8+
implicit val loanFormat = jsonFormat1(Loan.apply)
9+
implicit val uliFormat = jsonFormat3(ULI.apply)
10+
implicit val uliCheckFormat = jsonFormat1(ULICheck.apply)
11+
implicit val uliValidated = jsonFormat1(ULIValidated.apply)
12+
13+
}

api/src/main/scala/hmda/api/http/public/PublicHttpApi.scala

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import akka.stream.ActorMaterializer
66
import akka.util.Timeout
77
import hmda.api.http.HmdaCustomDirectives
88
import akka.http.scaladsl.server.Directives._
9+
import hmda.api.model.public.ULIModel.{ Loan, ULI, ULICheck, ULIValidated }
10+
import hmda.api.protocol.public.ULIProtocol._
11+
import hmda.validation.engine.lar.ULI._
12+
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
13+
import akka.http.scaladsl.marshalling.ToResponseMarshallable
914

1015
import scala.concurrent.ExecutionContext
1116

@@ -22,7 +27,29 @@ trait PublicHttpApi extends PublicLarHttpApi with HmdaCustomDirectives {
2227
encodeResponse {
2328
pathPrefix("institutions" / Segment) { instId =>
2429
modifiedLar(instId)
25-
}
30+
} ~
31+
pathPrefix("uli") {
32+
path("check-digit") {
33+
timedPost { _ =>
34+
entity(as[Loan]) { loan =>
35+
val loanId = loan.loanId
36+
val check = checkDigit(loanId)
37+
val uli = ULI(loanId, check.toInt, loanId + check)
38+
complete(ToResponseMarshallable(uli))
39+
}
40+
}
41+
} ~
42+
path("validate") {
43+
timedPost { _ =>
44+
entity(as[ULICheck]) { uc =>
45+
val uli = uc.uli
46+
val isValid = validateULI(uli)
47+
val validated = ULIValidated(isValid)
48+
complete(ToResponseMarshallable(validated))
49+
}
50+
}
51+
}
52+
}
2653
}
2754
}
2855
}

validation/src/main/scala/hmda/validation/engine/lar/ULI.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ object ULI {
5858
uli + checkDigit(uli).toString()
5959
}
6060

61-
def validate(uli: String): Boolean = {
61+
def validateULI(uli: String): Boolean = {
6262
calculateMod(BigInt(convert(uli))) == 1
6363
}
6464

validation/src/test/scala/hmda/validation/engine/lar/ULISpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class ULISpec extends WordSpec with MustMatchers {
1313
ULI.generateULI(validULI) mustBe "10Bx939c5543TqA1144M999143X38"
1414
}
1515
"Validate ULI" in {
16-
ULI.validate(validULI + "38") mustBe true
17-
ULI.validate(invalidULI) mustBe false
16+
ULI.validateULI(validULI + "38") mustBe true
17+
ULI.validateULI(invalidULI) mustBe false
1818
}
1919
}
2020
}

0 commit comments

Comments
 (0)