passninja-ruby 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cb5c833457f6b6748d951bd149464e7606f52aa340221a616e4cb172cb1ef913
4
- data.tar.gz: 1284c7b44e3b170eef7b5bd67c3dc6ec9560f6d1b0dcdaca232555257fdde734
3
+ metadata.gz: 92f1013947e431264a666642af2059bb8ea847069b14dfdc316784e4e1ce913e
4
+ data.tar.gz: f673fed1bc923ed1107e394880816fae4aa76abb2eea4b62a7405e68b3d6d38b
5
5
  SHA512:
6
- metadata.gz: 2807cf73a22b0edf295fd0a004858df0e02eabbef48891b9d47bab7ffce0a4352c9b69896029cd189fd7c487bbf77c9981b98298acda70e47f50866c4dc3f152
7
- data.tar.gz: 9636df9027075650f47c816b6d5a6d2e18f99c3a9c7209dfc186c1574bca94cb089081d4d843fbf25e6c2cad23d0557950708d3aaf1d1cf5ce98b3c1427c0915
6
+ metadata.gz: e46dbb205e276e7db9a7c346516e1eedab7bdabad8ec2bad662fe11000339e1ff2e5e3a71d78d0d0b6cd1009d5e8d8a3684d0e4f63656034fe381cafcb22ac30
7
+ data.tar.gz: decda426f3ff08c7a9c9ced3100ff96477d659e4d9c5d1aed0477cdc95f40ef3a2e313b16f56e9af3ca2d908f08f48733438917b61bb9e12c841ab37d903f52c
@@ -1,3 +1,3 @@
1
1
  module Passninja
2
- VERSION = "0.1.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/passninja.rb CHANGED
@@ -1,123 +1,166 @@
1
1
  require "net/http"
2
2
  require "json"
3
3
 
4
- module Passninja
4
+ module PassNinja
5
5
  class Client
6
- def initialize(account_id, api_key)
6
+ def initialize(account_id, api_key, host = 'https://api.passninja.com')
7
7
  @account_id = account_id
8
8
  @api_key = api_key
9
+ @host = host
10
+ url = URI.parse(host)
11
+ if url.instance_of? URI::HTTPS
12
+ @use_ssl = true
13
+ else
14
+ @use_ssl = false
15
+ end
9
16
  end
10
17
 
11
18
  def pass_templates
12
- PassTemplates.new(@account_id, @api_key)
19
+ PassTemplates.new(@account_id, @api_key, @host)
13
20
  end
14
21
 
15
22
  def passes
16
- Passes.new(@account_id, @api_key)
23
+ Passes.new(@account_id, @api_key, @host, @use_ssl)
17
24
  end
18
25
  end
19
26
 
20
27
  class PassTemplates
21
- def initialize(account_id, api_key)
28
+ def initialize(account_id, api_key, host, use_ssl)
22
29
  @account_id = account_id
23
30
  @api_key = api_key
31
+ @host = host
32
+ @use_ssl = use_ssl
24
33
  end
25
34
 
26
35
  def find(pass_template_key)
27
- uri = URI("https://api.passninja.com/v1/pass_templates/#{pass_template_key}")
36
+ uri = URI("#{@host}/v1/pass_templates/#{pass_template_key}")
37
+ puts uri
28
38
  request = Net::HTTP::Get.new(uri)
29
- request.basic_auth(@account_id, @api_key)
39
+ request["X-API-KEY"] = @api_key
40
+ request["X-ACCOUNT-ID"] = @account_id
30
41
 
31
- response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
42
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: @use_ssl) do |http|
32
43
  http.request(request)
33
44
  end
34
45
 
35
- JSON.parse(response.body)
46
+ begin
47
+ JSON.parse(response.body)
48
+ rescue JSON::ParserError
49
+ { error: "Unable to parse response" }
50
+ end
36
51
  end
37
52
  end
38
53
 
39
54
  class Passes
40
- def initialize(account_id, api_key)
55
+ def initialize(account_id, api_key, host, use_ssl)
41
56
  @account_id = account_id
42
57
  @api_key = api_key
58
+ @host = host
59
+ @use_ssl = use_ssl
43
60
  end
44
61
 
45
62
  def create(pass_type, pass_data)
46
63
  required_fields = fetch_required_keys_set(pass_type)
47
64
  validate_fields(pass_data, required_fields)
48
- uri = URI("https://api.passninja.com/v1/passes")
65
+ uri = URI("#{@host}/v1/passes")
49
66
  request = Net::HTTP::Post.new(uri)
50
- request.basic_auth(@account_id, @api_key)
67
+ request["X-API-KEY"] = @api_key
68
+ request["X-ACCOUNT-ID"] = @account_id
51
69
  request.content_type = "application/json"
52
- request.body = { passType: pass_type, passData: pass_data }.to_json
70
+ request.body = { passType: pass_type, pass: pass_data }.to_json
53
71
 
54
- response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
72
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: @use_ssl) do |http|
55
73
  http.request(request)
56
74
  end
57
75
 
58
- JSON.parse(response.body)
76
+ begin
77
+ JSON.parse(response.body)
78
+ rescue JSON::ParserError
79
+ { error: "Unable to parse response" }
80
+ end
59
81
  end
60
82
 
61
83
  def find(pass_type)
62
- uri = URI("https://api.passninja.com/v1/passes?passType=#{pass_type}")
84
+ uri = URI("#{@host}/v1/passes/#{pass_type}")
63
85
  request = Net::HTTP::Get.new(uri)
64
- request.basic_auth(@account_id, @api_key)
86
+ request["X-API-KEY"] = @api_key
87
+ request["X-ACCOUNT-ID"] = @account_id
65
88
 
66
- response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
89
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: @use_ssl) do |http|
67
90
  http.request(request)
68
91
  end
69
92
 
70
- JSON.parse(response.body)
93
+ begin
94
+ JSON.parse(response.body)
95
+ rescue JSON::ParserError
96
+ { error: "Unable to parse response" }
97
+ end
71
98
  end
72
99
 
73
100
  def get(pass_type, serial_number)
74
- uri = URI("https://api.passninja.com/v1/passes/#{pass_type}/#{serial_number}")
101
+ uri = URI("#{@host}/v1/passes/#{pass_type}/#{serial_number}")
75
102
  request = Net::HTTP::Get.new(uri)
76
- request.basic_auth(@account_id, @api_key)
103
+ request["X-API-KEY"] = @api_key
104
+ request["X-ACCOUNT-ID"] = @account_id
77
105
 
78
- response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
106
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: @use_ssl) do |http|
79
107
  http.request(request)
80
108
  end
81
109
 
82
- JSON.parse(response.body)
110
+ begin
111
+ JSON.parse(response.body)
112
+ rescue JSON::ParserError
113
+ { error: "Unable to parse response. Server response code: #{response.code}" }
114
+ end
83
115
  end
84
116
 
85
117
  def decrypt(pass_type, payload)
86
- uri = URI("https://api.passninja.com/v1/passes/decrypt")
118
+ uri = URI("#{@host}/v1/passes/decrypt")
87
119
  request = Net::HTTP::Post.new(uri)
88
- request.basic_auth(@account_id, @api_key)
120
+ request["X-API-KEY"] = @api_key
121
+ request["X-ACCOUNT-ID"] = @account_id
89
122
  request.content_type = "application/json"
90
123
  request.body = { passType: pass_type, payload: payload }.to_json
91
124
 
92
- response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
125
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: @use_ssl) do |http|
93
126
  http.request(request)
94
127
  end
95
128
 
96
- JSON.parse(response.body)
129
+ begin
130
+ JSON.parse(response.body)
131
+ rescue JSON::ParserError
132
+ { error: "Unable to parse response" }
133
+ end
97
134
  end
98
135
 
99
136
  def update(pass_type, serial_number, pass_data)
100
137
  required_fields = fetch_required_keys_set(pass_type)
101
138
  validate_fields(pass_data, required_fields)
102
- uri = URI("https://api.passninja.com/v1/passes/#{pass_type}/#{serial_number}")
139
+ uri = URI("#{@host}/v1/passes/#{pass_type}/#{serial_number}")
103
140
  request = Net::HTTP::Put.new(uri)
104
- request.basic_auth(@account_id, @api_key)
141
+ request["X-API-KEY"] = @api_key
142
+ request["X-ACCOUNT-ID"] = @account_id
105
143
  request.content_type = "application/json"
106
- request.body = { passData: pass_data }.to_json
144
+ request.body = { pass: pass_data }.to_json
107
145
 
108
- response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
146
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: @use_ssl) do |http|
109
147
  http.request(request)
110
148
  end
111
149
 
112
- JSON.parse(response.body)
150
+ begin
151
+ JSON.parse(response.body)
152
+ rescue JSON::ParserError
153
+ { error: "Unable to parse response" }
154
+ end
113
155
  end
114
156
 
115
157
  def delete(pass_type, serial_number)
116
- uri = URI("https://api.passninja.com/v1/passes/#{pass_type}/#{serial_number}")
158
+ uri = URI("#{@host}/v1/passes/#{pass_type}/#{serial_number}")
117
159
  request = Net::HTTP::Delete.new(uri)
118
- request.basic_auth(@account_id, @api_key)
160
+ request["X-API-KEY"] = @api_key
161
+ request["X-ACCOUNT-ID"] = @account_id
119
162
 
120
- response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
163
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: @use_ssl) do |http|
121
164
  http.request(request)
122
165
  end
123
166
 
@@ -127,22 +170,24 @@ module Passninja
127
170
  private
128
171
 
129
172
  def fetch_required_keys_set(pass_type)
130
- uri = URI("https://api.passninja.com/v1/pass_templates/#{pass_type}/required_fields")
173
+ uri = URI("#{@host}/v1/passtypes/keys/#{pass_type}")
174
+
131
175
  request = Net::HTTP::Get.new(uri)
132
- request.basic_auth(@account_id, @api_key)
176
+ request["X-API-KEY"] = @api_key
177
+ request["X-ACCOUNT-ID"] = @account_id
133
178
 
134
- response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
179
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: @use_ssl) do |http|
135
180
  http.request(request)
136
181
  end
137
182
 
138
- JSON.parse(response.body)["required_fields"].map(&:to_sym)
183
+ JSON.parse(response.body)["keys"]
139
184
  end
140
185
 
141
186
  def validate_fields(fields, required_fields)
142
- missing_fields = required_fields - fields.keys
187
+ missing_fields = required_fields.map(&:to_sym) - fields.keys.map(&:to_sym)
143
188
  unless missing_fields.empty?
144
189
  raise ArgumentError, "Missing required fields: #{missing_fields.join(', ')}"
145
190
  end
146
191
  end
147
192
  end
148
- end
193
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passninja-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Grundy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-18 00:00:00.000000000 Z
11
+ date: 2025-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-http
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  requirements: []
98
- rubygems_version: 3.3.7
98
+ rubygems_version: 3.3.26
99
99
  signing_key:
100
100
  specification_version: 4
101
101
  summary: A Ruby gem for interacting with the PassNinja API