Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't use apply method in gift code generator #2643

Merged
merged 2 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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