Skip to content

Commit

Permalink
Fix creating strip-based PKPasses
Browse files Browse the repository at this point in the history
  • Loading branch information
Arclite committed Dec 23, 2024
1 parent 7b29f7d commit fb9bc62
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ func buildRouter(files: StaticFiles) -> Router<AppRequestContext> {

router.post("/generate-pass") { request, context in
let passRequest = try await request.decode(as: PassRequest.self, context: context)
return try Pass(passRequest)
return Pass(passRequest)
}

router.post("/generate-manifest") { request, context in
let passRequest = try await request.decode(as: PassRequest.self, context: context)
let pass = try Pass(passRequest)
let pass = Pass(passRequest)
let manifest = Manifest(pass: pass, files: files, stripImages: [])
return manifest
}

router.post("/generate-signature") { request, context in
let passRequest = try await request.decode(as: PassRequest.self, context: context)
let pass = try Pass(passRequest)
let pass = Pass(passRequest)
let manifest = Manifest(pass: pass, files: files, stripImages: [])
let manifestData = try Manifest.encoder.encode(manifest)
let signature = try await Signer().sign(manifestData)
Expand Down
6 changes: 4 additions & 2 deletions Sources/App/Pass/Pass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ struct Pass: ResponseEncodable {
let locations: [Pass.Location]
let relevantDate: String?

init(_ request: PassRequest) throws {
init(_ request: PassRequest) {
self.description = request.title
self.logoText = request.title
self.barcodes = try [request.barcode].compactMap { try Pass.Barcode($0) }
self.locations = request.locations.map(Pass.Location.init)

// only include barcodes that can be represented "natively" in PKPass
self.barcodes = [request.barcode].compactMap { try? Pass.Barcode($0) }

self.relevantDate = try? request.dates
.compactMap(Self.dateFormatStyle.parse(_:))
.sorted()
Expand Down
2 changes: 1 addition & 1 deletion Sources/App/PassGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct PassGenerator {
private let bundler = Bundler()
private let stripImageGenerator = StripImageGenerator()
func generatePass(request: PassRequest, files: StaticFiles) async throws -> Response {
let pass = try Pass(request)
let pass = Pass(request)
let stripImages = try await stripImageGenerator.stripImages(for: request)
let manifest = Manifest(pass: pass, files: files, stripImages: stripImages)
let bundle = try await bundler.bundle(pass: pass, manifest: manifest, files: files, stripImages: stripImages)
Expand Down
16 changes: 16 additions & 0 deletions Tests/AppTests/Pass/PassTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Testing

@testable import BarcPass

struct PassTests {
@Test("Creating an EAN-13 Pass should not throw")
func createEAN13Pass() {
let request = PassRequest(
title: "Barcode",
barcode: PassRequest.Barcode.ean13("444444444444"),
locations: [],
dates: []
)
_ = Pass(request)
}
}

0 comments on commit fb9bc62

Please sign in to comment.