|
| 1 | +@file:Suppress("PackageDirectoryMismatch") |
| 2 | + |
| 3 | +package playground.barber |
| 4 | + |
| 5 | +import app.cash.barber.Barber |
| 6 | +import app.cash.barber.Barbershop |
| 7 | +import app.cash.barber.BarbershopBuilder |
| 8 | +import app.cash.barber.getBarber |
| 9 | +import app.cash.barber.locale.Locale.Companion.EN_US |
| 10 | +import app.cash.barber.models.Document |
| 11 | +import app.cash.barber.models.DocumentData |
| 12 | +import app.cash.barber.models.DocumentTemplate |
| 13 | +import playground.shouldBe |
| 14 | +import playground.shouldThrow |
| 15 | +import java.time.Instant |
| 16 | +import java.util.* |
| 17 | + |
| 18 | +/*** |
| 19 | + * cashapp/barber |
| 20 | + * A type safe Kotlin JVM library for building up localized, fillable, themed documents using Mustache templating. |
| 21 | + * |
| 22 | + * - [GitHub](https://github.com/cashapp/barber) |
| 23 | + * - [Documentation](https://cashapp.github.io/barber/) |
| 24 | + */ |
| 25 | +fun main() { |
| 26 | + println() |
| 27 | + println("# cashapp/barber") |
| 28 | + val recipientReceiptSmsDocumentTemplateEN_US = DocumentTemplate( |
| 29 | + fields = mapOf( |
| 30 | + "subject" to "{{sender}} sent you {{amount}}", |
| 31 | + "headline" to "New transfer", |
| 32 | + "short_description" to "{{sender}} sent {{amount}}", |
| 33 | + "secondary_button_url" to "{{cancelUrl}}" |
| 34 | + ), |
| 35 | + source = RecipientReceipt::class, |
| 36 | + targets = setOf(TransactionalSmsDocument::class), |
| 37 | + locale = EN_US |
| 38 | + ) |
| 39 | + |
| 40 | + val barbershop = BarbershopBuilder() |
| 41 | + .installDocumentTemplate<RecipientReceipt>(recipientReceiptSmsDocumentTemplateEN_US) |
| 42 | + .installDocument<TransactionalSmsDocument>() |
| 43 | + .build() |
| 44 | + |
| 45 | + // Get a Barber who knows how to render RecipientReceipt data into a TransactionalSmsDocument |
| 46 | + val recipientReceiptSms = barbershop.getBarber<RecipientReceipt, TransactionalSmsDocument>() |
| 47 | + val recept = RecipientReceipt( |
| 48 | + "Patrick", "14€", "http://example.com", Instant.now() |
| 49 | + ) |
| 50 | + val transactionalSmsDocument: TransactionalSmsDocument = recipientReceiptSms.render(recept, EN_US) |
| 51 | + transactionalSmsDocument shouldBe TransactionalSmsDocument( |
| 52 | + "Patrick sent you 14€","New transfer", "Patrick sent 14€", null, null, null, "http://example.com" |
| 53 | + ) |
| 54 | + |
| 55 | + shouldThrow("DocumentData [unregistered] and corresponding DocumentTemplate(s) are not installed in Barbershop") { |
| 56 | + barbershop.getBarber<UnregisteredDocumentData, TransactionalSmsDocument>() |
| 57 | + } |
| 58 | + |
| 59 | +} |
| 60 | + |
| 61 | +// Define DocumentData |
| 62 | +data class RecipientReceipt( |
| 63 | + val sender: String, |
| 64 | + val amount: String, |
| 65 | + val cancelUrl: String, |
| 66 | + val deposit_expected_at: Instant |
| 67 | +) : DocumentData |
| 68 | + |
| 69 | +data class TransactionalSmsDocument( |
| 70 | + val subject: String, |
| 71 | + val headline: String, |
| 72 | + val short_description: String, |
| 73 | + val primary_button: String?, |
| 74 | + val primary_button_url: String?, |
| 75 | + val secondary_button: String?, |
| 76 | + val secondary_button_url: String? |
| 77 | +) : Document |
| 78 | + |
| 79 | +data class UnregisteredDocumentData( |
| 80 | + val id: Long |
| 81 | +): DocumentData |
0 commit comments