Skip to content

Commit

Permalink
Merge pull request #2643 from guardian/jd-giftcode-tidyup
Browse files Browse the repository at this point in the history
don't use apply method in gift code generator
  • Loading branch information
johnduffell committed Aug 4, 2020
2 parents b28d95e + e56bd86 commit af0d87e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package com.gu.support.redemption.generator

import java.security.SecureRandom

import com.gu.support.redemption.generator.ConstructCode.GenerateGiftCode
import com.gu.support.redemption.generator.CodeBuilder.GenerateGiftCode

object GiftCodeGenerator {

lazy val randomGiftCodes: Iterator[GenerateGiftCode] = {
val gen = new SecureRandom()
val ints = Iterator.continually(gen.nextInt())
apply(ints)
fromInts(ints)
}

def apply(random: Iterator[Int]): Iterator[GenerateGiftCode] =
CodeSuffixGenerator(random).map(ConstructCode.apply)
def fromInts(random: Iterator[Int]): Iterator[GenerateGiftCode] =
CodeSuffixGenerator.generate(random).map(CodeBuilder.build)

}

Expand All @@ -29,7 +29,7 @@ object GiftDuration {

}

object ConstructCode {
object CodeBuilder {

private val prefix = "gd"

Expand All @@ -46,7 +46,7 @@ object ConstructCode {
def withDuration(duration: GiftDuration): GiftCode
}

def apply(code: CodeSuffixGenerator.CodeSuffix): GenerateGiftCode =
def build(code: CodeSuffixGenerator.CodeSuffix): GenerateGiftCode =
(duration: GiftDuration) => {
val init = prefix + duration.code + "-"
GiftCode(init + code.value).get
Expand All @@ -64,10 +64,10 @@ object CodeSuffixGenerator {
.map(new CodeSuffix(_))
}

def apply(random: Iterator[Int]): Iterator[CodeSuffix] =
def generate(random: Iterator[Int]): Iterator[CodeSuffix] =
random.grouped(6).map(codeFromGroup).map(CodeSuffix.apply).map(_.get)

def codeFromGroup(groupedInts: Seq[Int]): String = {
private[generator] def codeFromGroup(groupedInts: Seq[Int]): String = {
val chars = groupedInts
.map(int => java.lang.Integer.toString(int, 34).last)
.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.scalatest.matchers.should.Matchers
class GiftCodeGeneratorSpec extends AnyFlatSpec with Matchers {

it should "work in the basic case" in {
val giftCode = GiftCodeGenerator(Iterator.continually(0)).next.withDuration(GiftDuration.Gift3Month)
val giftCode = GiftCodeGenerator.fromInts(Iterator.continually(0)).next.withDuration(GiftDuration.Gift3Month)
giftCode.value should be("gd03-000000")
}

Expand All @@ -22,16 +22,16 @@ class GiftCodeGeneratorSpec extends AnyFlatSpec with Matchers {

}

class ConstructCodeSpec extends AnyFlatSpec with Matchers {
class CodeBuilderSpec extends AnyFlatSpec with Matchers {

it should "get the durations right for the codes" in {
ConstructCode(CodeSuffixGenerator.CodeSuffix("000000").get).withDuration(GiftDuration.Gift3Month).value should be("gd03-000000")
ConstructCode(CodeSuffixGenerator.CodeSuffix("000000").get).withDuration(GiftDuration.Gift6Month).value should be("gd06-000000")
ConstructCode(CodeSuffixGenerator.CodeSuffix("000000").get).withDuration(GiftDuration.Gift12Month).value should be("gd12-000000")
CodeBuilder.build(CodeSuffixGenerator.CodeSuffix("000000").get).withDuration(GiftDuration.Gift3Month).value should be("gd03-000000")
CodeBuilder.build(CodeSuffixGenerator.CodeSuffix("000000").get).withDuration(GiftDuration.Gift6Month).value should be("gd06-000000")
CodeBuilder.build(CodeSuffixGenerator.CodeSuffix("000000").get).withDuration(GiftDuration.Gift12Month).value should be("gd12-000000")
}

it should "not allow invalid codes to be constructed" in {
ConstructCode.GiftCode("invalid") should be(None)
CodeBuilder.GiftCode("invalid") should be(None)
}

}
Expand All @@ -48,7 +48,7 @@ class CodeSuffixGeneratorSpec extends AnyFlatSpec with Matchers {

it should "cycle through all the possiblities" in {
val seq = Stream.from(0).iterator
CodeSuffixGenerator(seq).take(7).toList.map(_.value) should be(
CodeSuffixGenerator.generate(seq).take(7).toList.map(_.value) should be(
List("0y2345", "6789ab", "cdefgh", "ijkzmn", "opqrst", "uvwx0y", "234567")
)
}
Expand Down

0 comments on commit af0d87e

Please sign in to comment.