Skip to content

Commit

Permalink
Move NFC to its own model
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Callaghan committed Aug 5, 2020
1 parent c770e49 commit c55818c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
35 changes: 35 additions & 0 deletions spec/models/pass_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,41 @@ describe PassKit::Pass do
locations[2].latitude.should eq 31.1
locations[2].longitude.should eq -121.1
end

it "can be passed an array of beacons" do
pass = PassKit::Pass.new(
pass_type_identifier: "pass.com.example",
organization_name: "PlaceOS",
serial_number: "12345",
team_identifier: "TM123",
description: "The golden ticket",
beacons: [
{ proximity_uuid: "1234ABCD" },
{ proximity_uuid: "4321DCBA", major: UInt16.new(1), minor: UInt16.new(2) },
PassKit::Beacon.new(proximity_uuid: "1111AAAA")
]
)

beacons = pass.beacons.as(Array(PassKit::Beacon))
beacons[0].proximity_uuid.should eq "1234ABCD"
beacons[1].proximity_uuid.should eq "4321DCBA"
beacons[1].major.should eq 1
beacons[1].minor.should eq 2
beacons[2].proximity_uuid.should eq "1111AAAA"
end

it "can be passed an NFC tuple" do
pass = PassKit::Pass.new(
pass_type_identifier: "pass.com.example",
organization_name: "PlaceOS",
serial_number: "12345",
team_identifier: "TM123",
description: "The golden ticket",
nfc: { message: "hello" }
)

pass.nfc.not_nil!.message.should eq "hello"
end
end

private def fixture_file(file : String)
Expand Down
23 changes: 23 additions & 0 deletions src/models/nfc.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "json"

module PassKit
alias NFCTuple = NamedTuple(message: String) |
NamedTuple(message: String, encryption_public_key: String?)

struct NFC
include JSON::Serializable

property message : String

@[JSON::Field(key: "encryptionPublicKey")]
property encryption_public_key : String?

def initialize(@message, @encryption_public_key = nil)
end

def initialize(attributes : NFCTuple)
@message = attributes[:message]
@encryption_public_key = attributes[:encryption_public_key]?
end
end
end
14 changes: 5 additions & 9 deletions src/models/pass.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require "json"
require "./barcode"
require "./beacon"
require "./location"
require "./nfc"
require "./style"

module PassKit
Expand Down Expand Up @@ -161,7 +162,6 @@ module PassKit
@suppress_strip_shine = nil,
@authentication_token = nil,
@web_service_url = nil,
@nfc = nil,

# Style related
type : PassType = PassType::Generic,
Expand All @@ -176,6 +176,7 @@ module PassKit
locations : Array(LocationTuple | Location)? = nil,
barcodes : Array(BarcodeTuple | Barcode)? = nil,
beacons : Array(BeaconTuple | Beacon)? = nil,
nfc : (NFC | NFCTuple)? = nil
)
@format_version = APPLE_PASS_FORMAT_VERSION

Expand Down Expand Up @@ -220,15 +221,10 @@ module PassKit
beacon.is_a?(Beacon) ? beacon : Beacon.new(beacon)
end
end
end

struct NFC
include JSON::Serializable

property message : String

@[JSON::Field(key: "encryptionPublicKey")]
property encryption_public_key : String?
if nfc
@nfc = nfc.is_a?(NFC) ? nfc : NFC.new(nfc)
end
end
end
end

0 comments on commit c55818c

Please sign in to comment.