Skip to content

Commit 8be5e4a

Browse files
annaswimscoorasse
andauthored
Enhanced examples (coorasse#26)
* Enhanced examples * create an example with user data * fix date formatting in example * remove invalid app_launch_url from example * fix dev host * fix example PASSKIT_CERTIFICATE_KEY * Update lib/passkit/base_pass.rb Co-authored-by: Alessandro Rodi <[email protected]> --------- Co-authored-by: Alessandro Rodi <[email protected]>
1 parent d5bf5b5 commit 8be5e4a

18 files changed

+176
-13
lines changed

.example.env

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PASSKIT_WEB_SERVICE_HOST=https://localhost:3000
2-
PASSKIT_CERTIFICATE_KEY=path/to/certificate.key
2+
PASSKIT_CERTIFICATE_KEY=theCertPassword
33
PASSKIT_PRIVATE_P12_CERTIFICATE=path/to/certificate.p12
44
PASSKIT_APPLE_INTERMEDIATE_CERTIFICATE=path/to/AppleWWDRCA.cer
55
PASSKIT_APPLE_TEAM_IDENTIFIER=XXXXXXXXXX

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/tmp/
99
Gemfile.lock
1010
.env
11-
test/dummy/db/*.sqlite3
11+
test/dummy/db/*.sqlite3*
1212
test/dummy/log/*.log
1313
test/dummy/tmp/
1414
*.gem

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Again, check the example mailer included in the gem to see how to use it.
148148
## Debug issues
149149

150150
* On Mac, open the `Console.app` to view the errors.
151-
* Check the logs on http://localhost:3000/passkit/logs
151+
* Check the logs on http://localhost:3000/passkit/dashboard/logs
152152
* In case of error "The passTypeIdentifier or teamIdentifier provided may not match your certificate,
153153
or the certificate trust chain could not be verified." the certificate (p12) might be expired.
154154

lib/passkit/base_pass.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ def event_ticket
143143

144144
# Date and time the pass expires, must include
145145
# days, hours and minutes (seconds are optional)
146-
# Returns a String representing the date and time in W3C format ("%Y-%m-%dT%H:%M:%S%z")
146+
# Returns a String representing the date and time in W3C format ('%Y-%m-%dT%H:%M:%S%:z')
147+
# For example, 1980-05-07T10:30-05:00.
147148
def expiration_date
148149
end
149150

@@ -167,7 +168,7 @@ def nfc
167168
# Date and time when the pass becomes relevant and should be
168169
# displayed, must include days, hours and minutes
169170
# (seconds are optional)
170-
# Returns a String representing the date and time in W3C format ("%Y-%m-%dT%H:%M:%S%z")
171+
# Returns a String representing the date and time in W3C format ('%Y-%m-%dT%H:%M:%S%:z')
171172
def relevant_date
172173
end
173174

lib/passkit/example_store_card.rb

+2-6
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,13 @@ def logo_text
5858
"Loyalty Card"
5959
end
6060

61-
def app_launch_url
62-
"https://github.com/coorasse/passkit"
63-
end
64-
6561
def relevant_date
66-
Time.current.strftime("%Y-%m-%dT%H:%M:%S%z")
62+
Time.current.strftime('%Y-%m-%dT%H:%M:%S%:z')
6763
end
6864

6965
def expiration_date
7066
# Expire the pass tomorrow
71-
(Time.current + 60*60*24).strftime("%Y-%m-%dT%H:%M:%S%z")
67+
(Time.current + 1.day).strftime('%Y-%m-%dT%H:%M:%S%:z')
7268
end
7369

7470
def semantics
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
module Passkit
2+
class UserStoreCard < BasePass
3+
def pass_type
4+
:storeCard
5+
end
6+
7+
def organization_name
8+
"Passkit"
9+
end
10+
11+
def description
12+
"A basic description for a pass"
13+
end
14+
15+
# A pass can have up to ten relevant locations
16+
#
17+
# @see https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/PassKit_PG/Creating.html
18+
def locations
19+
[
20+
{ latitude: 41.2273414693647, longitude: -95.92925748878405 }, # North Entrance
21+
{ latitude: 41.22476226066285, longitude: -95.92879374051269 } # Main Entrance
22+
]
23+
end
24+
25+
def file_name
26+
@file_name ||= SecureRandom.uuid
27+
end
28+
29+
# QRCode by default
30+
def barcodes
31+
[
32+
{ messageEncoding: "iso-8859-1",
33+
format: "PKBarcodeFormatQR",
34+
message: "https://github.com/coorasse/passkit",
35+
altText: "https://github.com/coorasse/passkit" }
36+
]
37+
end
38+
39+
# Barcode example
40+
# def barcode
41+
# { messageEncoding: 'iso-8859-1',
42+
# format: 'PKBarcodeFormatCode128',
43+
# message: '12345',
44+
# altText: '12345' }
45+
# end
46+
47+
def logo_text
48+
"Loyalty Card"
49+
end
50+
51+
def expiration_date
52+
# Expire the pass tomorrow
53+
(Time.current + 60*60*24).strftime('%Y-%m-%dT%H:%M:%S%:z')
54+
end
55+
56+
def semantics
57+
{
58+
balance: {
59+
amount: "100",
60+
currencyCode: "USD"
61+
}
62+
}
63+
end
64+
65+
def header_fields
66+
[{
67+
key: "balance",
68+
label: "Balance",
69+
value: 100,
70+
currencyCode: "$"
71+
}]
72+
end
73+
74+
def back_fields
75+
[{
76+
key: "example1",
77+
label: "Code",
78+
value: "0123456789"
79+
},
80+
{
81+
key: "example2",
82+
label: "Creator",
83+
value: "https://github.com/coorasse"
84+
},
85+
{
86+
key: "example3",
87+
label: "Contact",
88+
89+
}]
90+
end
91+
92+
def auxiliary_fields
93+
[{
94+
key: "name",
95+
label: "Name",
96+
value: @generator.name
97+
},
98+
{
99+
key: "email",
100+
label: "Email",
101+
102+
},
103+
{
104+
key: "phone",
105+
label: "Phone",
106+
value: "+41 1234567890"
107+
}]
108+
end
109+
110+
private
111+
112+
def folder_name
113+
self.class.name.demodulize.underscore
114+
end
115+
end
116+
end

test/dummy/app/models/ticket.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Ticket < ApplicationRecord
2+
belongs_to :user
3+
end

test/dummy/app/models/user.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class User < ApplicationRecord
2+
has_many :tickets
3+
end

test/dummy/config/environments/development.rb

+2
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,6 @@
6767

6868
# Uncomment if you wish to allow Action Cable access from any origin.
6969
# config.action_cable.disable_request_forgery_protection = true
70+
71+
config.hosts << URI.parse(ENV.fetch("PASSKIT_WEB_SERVICE_HOST")).host
7072
end
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
Passkit.configure do |_config|
1+
Passkit.configure do |config|
2+
config.available_passes['Passkit::UserStoreCard'] = -> { User.create!(name: "ExampleName") }
23
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class CreateUsers < ActiveRecord::Migration[7.1]
2+
def change
3+
create_table :users do |t|
4+
t.string :name
5+
6+
t.timestamps
7+
end
8+
end
9+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreateTickets < ActiveRecord::Migration[7.1]
2+
def change
3+
create_table :tickets do |t|
4+
t.string :name
5+
t.references :user, null: false, foreign_key: true
6+
7+
t.timestamps
8+
end
9+
end
10+
end

test/dummy/db/schema.rb

+17-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.0].define(version: 2022_09_23_124956) do
13+
ActiveRecord::Schema[7.1].define(version: 2024_04_11_013721) do
1414
create_table "passkit_devices", force: :cascade do |t|
1515
t.string "identifier"
1616
t.string "push_token"
@@ -45,4 +45,20 @@
4545
t.index ["passkit_device_id"], name: "index_passkit_registrations_on_passkit_device_id"
4646
t.index ["passkit_pass_id"], name: "index_passkit_registrations_on_passkit_pass_id"
4747
end
48+
49+
create_table "tickets", force: :cascade do |t|
50+
t.string "name"
51+
t.integer "user_id", null: false
52+
t.datetime "created_at", null: false
53+
t.datetime "updated_at", null: false
54+
t.index ["user_id"], name: "index_tickets_on_user_id"
55+
end
56+
57+
create_table "users", force: :cascade do |t|
58+
t.string "name"
59+
t.datetime "created_at", null: false
60+
t.datetime "updated_at", null: false
61+
end
62+
63+
add_foreign_key "tickets", "users"
4864
end

test/dummy/db/seeds.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@user1 = User.create!(name: "First User")
2+
@user2 = User.create!(name: "Second User")
3+
4+
Ticket.create!(name: "Ticket1", user: @user1)
5+
Ticket.create!(name: "Ticket2", user: @user1)
6+
Ticket.create!(name: "Ticket3", user: @user2)
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)