petail 0.0.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 +7 -0
- checksums.yaml.gz.sig +0 -0
- data/LICENSE.adoc +134 -0
- data/README.adoc +369 -0
- data/lib/petail/payload.rb +70 -0
- data/lib/petail/xml/deserializer.rb +25 -0
- data/lib/petail/xml/serializer.rb +15 -0
- data/lib/petail.rb +22 -0
- data/petail.gemspec +30 -0
- data.tar.gz.sig +0 -0
- metadata +95 -0
- metadata.gz.sig +0 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d15f3fa3be5b23d1cb72cb42074e5de52fde3ec85da8858b3a79b5ee073de98b
|
4
|
+
data.tar.gz: e06bd23c70b3e9e5220a02ca3e7e4ce723800eab81b76769b767ddcc5a0d8b94
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 78bee6fe73d3ff5c2031aca94a6f2a96c7cd63651e53e7fa760009048e1c1d568edb53838ba904bc517552a6b6324e808c62516cbfe462c3af1c408b798e73a9
|
7
|
+
data.tar.gz: eed52a98b68b76540efd7b11129502e388d711bb2e123a0c594b83878aab59509c06e4feb31313929a40bfab8228aae54cf8dc3cb3f966f2f85a689175372ad9
|
checksums.yaml.gz.sig
ADDED
Binary file
|
data/LICENSE.adoc
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
= Hippocratic License
|
2
|
+
|
3
|
+
Version: 2.1.0.
|
4
|
+
|
5
|
+
Purpose. The purpose of this License is for the Licensor named above to
|
6
|
+
permit the Licensee (as defined below) broad permission, if consistent
|
7
|
+
with Human Rights Laws and Human Rights Principles (as each is defined
|
8
|
+
below), to use and work with the Software (as defined below) within the
|
9
|
+
full scope of Licensor’s copyright and patent rights, if any, in the
|
10
|
+
Software, while ensuring attribution and protecting the Licensor from
|
11
|
+
liability.
|
12
|
+
|
13
|
+
Permission and Conditions. The Licensor grants permission by this
|
14
|
+
license ("License"), free of charge, to the extent of Licensor’s
|
15
|
+
rights under applicable copyright and patent law, to any person or
|
16
|
+
entity (the "Licensee") obtaining a copy of this software and
|
17
|
+
associated documentation files (the "Software"), to do everything with
|
18
|
+
the Software that would otherwise infringe (i) the Licensor’s copyright
|
19
|
+
in the Software or (ii) any patent claims to the Software that the
|
20
|
+
Licensor can license or becomes able to license, subject to all of the
|
21
|
+
following terms and conditions:
|
22
|
+
|
23
|
+
* Acceptance. This License is automatically offered to every person and
|
24
|
+
entity subject to its terms and conditions. Licensee accepts this
|
25
|
+
License and agrees to its terms and conditions by taking any action with
|
26
|
+
the Software that, absent this License, would infringe any intellectual
|
27
|
+
property right held by Licensor.
|
28
|
+
* Notice. Licensee must ensure that everyone who gets a copy of any part
|
29
|
+
of this Software from Licensee, with or without changes, also receives
|
30
|
+
the License and the above copyright notice (and if included by the
|
31
|
+
Licensor, patent, trademark and attribution notice). Licensee must cause
|
32
|
+
any modified versions of the Software to carry prominent notices stating
|
33
|
+
that Licensee changed the Software. For clarity, although Licensee is
|
34
|
+
free to create modifications of the Software and distribute only the
|
35
|
+
modified portion created by Licensee with additional or different terms,
|
36
|
+
the portion of the Software not modified must be distributed pursuant to
|
37
|
+
this License. If anyone notifies Licensee in writing that Licensee has
|
38
|
+
not complied with this Notice section, Licensee can keep this License by
|
39
|
+
taking all practical steps to comply within 30 days after the notice. If
|
40
|
+
Licensee does not do so, Licensee’s License (and all rights licensed
|
41
|
+
hereunder) shall end immediately.
|
42
|
+
* Compliance with Human Rights Principles and Human Rights Laws.
|
43
|
+
[arabic]
|
44
|
+
. Human Rights Principles.
|
45
|
+
[loweralpha]
|
46
|
+
.. Licensee is advised to consult the articles of the United Nations
|
47
|
+
Universal Declaration of Human Rights and the United Nations Global
|
48
|
+
Compact that define recognized principles of international human rights
|
49
|
+
(the "Human Rights Principles"). Licensee shall use the Software in a
|
50
|
+
manner consistent with Human Rights Principles.
|
51
|
+
.. Unless the Licensor and Licensee agree otherwise, any dispute,
|
52
|
+
controversy, or claim arising out of or relating to (i) Section 1(a)
|
53
|
+
regarding Human Rights Principles, including the breach of Section 1(a),
|
54
|
+
termination of this License for breach of the Human Rights Principles,
|
55
|
+
or invalidity of Section 1(a) or (ii) a determination of whether any Law
|
56
|
+
is consistent or in conflict with Human Rights Principles pursuant to
|
57
|
+
Section 2, below, shall be settled by arbitration in accordance with the
|
58
|
+
Hague Rules on Business and Human Rights Arbitration (the "Rules");
|
59
|
+
provided, however, that Licensee may elect not to participate in such
|
60
|
+
arbitration, in which event this License (and all rights licensed
|
61
|
+
hereunder) shall end immediately. The number of arbitrators shall be one
|
62
|
+
unless the Rules require otherwise.
|
63
|
+
+
|
64
|
+
Unless both the Licensor and Licensee agree to the contrary: (1) All
|
65
|
+
documents and information concerning the arbitration shall be public and
|
66
|
+
may be disclosed by any party; (2) The repository referred to under
|
67
|
+
Article 43 of the Rules shall make available to the public in a timely
|
68
|
+
manner all documents concerning the arbitration which are communicated
|
69
|
+
to it, including all submissions of the parties, all evidence admitted
|
70
|
+
into the record of the proceedings, all transcripts or other recordings
|
71
|
+
of hearings and all orders, decisions and awards of the arbitral
|
72
|
+
tribunal, subject only to the arbitral tribunal’s powers to take such
|
73
|
+
measures as may be necessary to safeguard the integrity of the arbitral
|
74
|
+
process pursuant to Articles 18, 33, 41 and 42 of the Rules; and (3)
|
75
|
+
Article 26(6) of the Rules shall not apply.
|
76
|
+
. Human Rights Laws. The Software shall not be used by any person or
|
77
|
+
entity for any systems, activities, or other uses that violate any Human
|
78
|
+
Rights Laws. "Human Rights Laws" means any applicable laws,
|
79
|
+
regulations, or rules (collectively, "Laws") that protect human,
|
80
|
+
civil, labor, privacy, political, environmental, security, economic, due
|
81
|
+
process, or similar rights; provided, however, that such Laws are
|
82
|
+
consistent and not in conflict with Human Rights Principles (a dispute
|
83
|
+
over the consistency or a conflict between Laws and Human Rights
|
84
|
+
Principles shall be determined by arbitration as stated above). Where
|
85
|
+
the Human Rights Laws of more than one jurisdiction are applicable or in
|
86
|
+
conflict with respect to the use of the Software, the Human Rights Laws
|
87
|
+
that are most protective of the individuals or groups harmed shall
|
88
|
+
apply.
|
89
|
+
. Indemnity. Licensee shall hold harmless and indemnify Licensor (and
|
90
|
+
any other contributor) against all losses, damages, liabilities,
|
91
|
+
deficiencies, claims, actions, judgments, settlements, interest, awards,
|
92
|
+
penalties, fines, costs, or expenses of whatever kind, including
|
93
|
+
Licensor’s reasonable attorneys’ fees, arising out of or relating to
|
94
|
+
Licensee’s use of the Software in violation of Human Rights Laws or
|
95
|
+
Human Rights Principles.
|
96
|
+
* Failure to Comply. Any failure of Licensee to act according to the
|
97
|
+
terms and conditions of this License is both a breach of the License and
|
98
|
+
an infringement of the intellectual property rights of the Licensor
|
99
|
+
(subject to exceptions under Laws, e.g., fair use). In the event of a
|
100
|
+
breach or infringement, the terms and conditions of this License may be
|
101
|
+
enforced by Licensor under the Laws of any jurisdiction to which
|
102
|
+
Licensee is subject. Licensee also agrees that the Licensor may enforce
|
103
|
+
the terms and conditions of this License against Licensee through
|
104
|
+
specific performance (or similar remedy under Laws) to the extent
|
105
|
+
permitted by Laws. For clarity, except in the event of a breach of this
|
106
|
+
License, infringement, or as otherwise stated in this License, Licensor
|
107
|
+
may not terminate this License with Licensee.
|
108
|
+
* Enforceability and Interpretation. If any term or provision of this
|
109
|
+
License is determined to be invalid, illegal, or unenforceable by a
|
110
|
+
court of competent jurisdiction, then such invalidity, illegality, or
|
111
|
+
unenforceability shall not affect any other term or provision of this
|
112
|
+
License or invalidate or render unenforceable such term or provision in
|
113
|
+
any other jurisdiction; provided, however, subject to a court
|
114
|
+
modification pursuant to the immediately following sentence, if any term
|
115
|
+
or provision of this License pertaining to Human Rights Laws or Human
|
116
|
+
Rights Principles is deemed invalid, illegal, or unenforceable against
|
117
|
+
Licensee by a court of competent jurisdiction, all rights in the
|
118
|
+
Software granted to Licensee shall be deemed null and void as between
|
119
|
+
Licensor and Licensee. Upon a determination that any term or provision
|
120
|
+
is invalid, illegal, or unenforceable, to the extent permitted by Laws,
|
121
|
+
the court may modify this License to affect the original purpose that
|
122
|
+
the Software be used in compliance with Human Rights Principles and
|
123
|
+
Human Rights Laws as closely as possible. The language in this License
|
124
|
+
shall be interpreted as to its fair meaning and not strictly for or
|
125
|
+
against any party.
|
126
|
+
* Disclaimer. TO THE FULL EXTENT ALLOWED BY LAW, THIS SOFTWARE COMES
|
127
|
+
"AS IS," WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED, AND LICENSOR AND
|
128
|
+
ANY OTHER CONTRIBUTOR SHALL NOT BE LIABLE TO ANYONE FOR ANY DAMAGES OR
|
129
|
+
OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE
|
130
|
+
OR THIS LICENSE, UNDER ANY KIND OF LEGAL CLAIM.
|
131
|
+
|
132
|
+
This Hippocratic License is an link:https://ethicalsource.dev[Ethical Source license] and is offered
|
133
|
+
for use by licensors and licensees at their own risk, on an "AS IS" basis, and with no warranties
|
134
|
+
express or implied, to the maximum extent permitted by Laws.
|
data/README.adoc
ADDED
@@ -0,0 +1,369 @@
|
|
1
|
+
:toc: macro
|
2
|
+
:toclevels: 5
|
3
|
+
:figure-caption!:
|
4
|
+
|
5
|
+
= Petail
|
6
|
+
|
7
|
+
Petail is a portmanteau (i.e. `[p]roblem + d[etail] = petail`) that implements link:https://datatracker.ietf.org/doc/html/rfc7807[RFC 7807: Problem Details for HTTP APIs]. This allows you to produce HTTP error responses that are structured, machine readable, and consistent.
|
8
|
+
|
9
|
+
toc::[]
|
10
|
+
|
11
|
+
== Features
|
12
|
+
|
13
|
+
* Provides JSON and XML serialization and deserialization.
|
14
|
+
* Provides HTTP header and media type support.
|
15
|
+
|
16
|
+
== Requirements
|
17
|
+
|
18
|
+
. link:https://www.ruby-lang.org[Ruby].
|
19
|
+
|
20
|
+
== Setup
|
21
|
+
|
22
|
+
To install _with_ security, run:
|
23
|
+
|
24
|
+
[source,bash]
|
25
|
+
----
|
26
|
+
# 💡 Skip this line if you already have the public certificate installed.
|
27
|
+
gem cert --add <(curl --compressed --location https://alchemists.io/gems.pem)
|
28
|
+
gem install petail --trust-policy HighSecurity
|
29
|
+
----
|
30
|
+
|
31
|
+
To install _without_ security, run:
|
32
|
+
|
33
|
+
[source,bash]
|
34
|
+
----
|
35
|
+
gem install petail
|
36
|
+
----
|
37
|
+
|
38
|
+
You can also add the gem directly to your project:
|
39
|
+
|
40
|
+
[source,bash]
|
41
|
+
----
|
42
|
+
bundle add petail
|
43
|
+
----
|
44
|
+
|
45
|
+
Once the gem is installed, you only need to require it:
|
46
|
+
|
47
|
+
[source,ruby]
|
48
|
+
----
|
49
|
+
require "petail"
|
50
|
+
----
|
51
|
+
|
52
|
+
== Usage
|
53
|
+
|
54
|
+
The quickest way to get started is to create a new instance and then cast as JSON or XML:
|
55
|
+
|
56
|
+
[source,ruby]
|
57
|
+
----
|
58
|
+
payload = Petail.new type: "https://demo.io/problem_details/timeout",
|
59
|
+
status: 413,
|
60
|
+
detail: "You've exceeded the 5MB upload limit.",
|
61
|
+
instance: "/profile/3a1bfd54-ae6c-4a61-8d0d-90c132428dc3"
|
62
|
+
|
63
|
+
payload.to_json
|
64
|
+
|
65
|
+
# {
|
66
|
+
# "type": "https://demo.io/problem_details/timeout",
|
67
|
+
# "title": "Content Too Large",
|
68
|
+
# "status": 413,
|
69
|
+
# "detail": "You've exceeded the 5MB upload limit.",
|
70
|
+
# "instance": "/profile/3a1bfd54-ae6c-4a61-8d0d-90c132428dc3"
|
71
|
+
# }
|
72
|
+
|
73
|
+
payload.to_xml
|
74
|
+
|
75
|
+
# <?xml version='1.0' encoding='UTF-8'?>
|
76
|
+
# <problem xmlns='urn:ietf:rfc:7807'>
|
77
|
+
# <type>https://demo.io/problem_details/timeout</type>
|
78
|
+
# <title>Content Too Large</title>
|
79
|
+
# <status>413</status>
|
80
|
+
# <detail>You've exceeded the 5MB upload limit.</detail>
|
81
|
+
# <instance>/profile/3a1bfd54-ae6c-4a61-8d0d-90c132428dc3</instance>
|
82
|
+
# </problem>
|
83
|
+
----
|
84
|
+
|
85
|
+
=== Media Types
|
86
|
+
|
87
|
+
For convenience, you can obtain the necessary media types for your HTTP headers as follows:
|
88
|
+
|
89
|
+
[source,ruby]
|
90
|
+
----
|
91
|
+
Petail::MEDIA_TYPE_JSON # "application/problem+json"
|
92
|
+
Petail::MEDIA_TYPE_XML # "application/problem+xml"
|
93
|
+
|
94
|
+
Petail.media_type_for :json # "application/problem+json"
|
95
|
+
Petail.media_type_for :xml # "application/problem+xml"
|
96
|
+
----
|
97
|
+
|
98
|
+
=== Payload
|
99
|
+
|
100
|
+
You'll always get a `Petail::Payload` object answered back when using `Petail.new` for which you can cast to JSON, XML, and other types. There are few conveniences provided for you when constructing a new payload. For instance, you can also use status to set default title:
|
101
|
+
|
102
|
+
[source,ruby]
|
103
|
+
----
|
104
|
+
Petail.new status: 413
|
105
|
+
# #<Struct:Petail::Payload:0x0000ec80
|
106
|
+
# detail = nil,
|
107
|
+
# extensions = {},
|
108
|
+
# instance = nil,
|
109
|
+
# status = 413,
|
110
|
+
# title = "Content Too Large",
|
111
|
+
# type = "about:blank"
|
112
|
+
# >
|
113
|
+
----
|
114
|
+
|
115
|
+
Notice that standard HTTP 413 title of "Content Too Large" is provided for you but only if you don't supply a title. This works for symbols too. Example:
|
116
|
+
|
117
|
+
[source,ruby]
|
118
|
+
----
|
119
|
+
Petail.new status: :bad_request
|
120
|
+
# #<Struct:Petail::Payload:0x0000f280
|
121
|
+
# detail = nil,
|
122
|
+
# extensions = {},
|
123
|
+
# instance = nil,
|
124
|
+
# status = 400,
|
125
|
+
# title = "Bad Request",
|
126
|
+
# type = "about:blank"
|
127
|
+
# >
|
128
|
+
----
|
129
|
+
|
130
|
+
This is similar to the above, but notice the status is cast to an integer while the title is also populated for you. Using either an integer or symbol for the HTTP status is handy for situations where you don't need a custom title and prefer the default HTTP title.
|
131
|
+
|
132
|
+
Due to the payload being a `Struct`, you have all of the standard methods available to you. One thing to note is that the payload is frozen by default so you can't mutate attributes. That said, you can still add or check for extensions after the fact. Example:
|
133
|
+
|
134
|
+
[source,]
|
135
|
+
----
|
136
|
+
payload = Petail.new status: :forbidden
|
137
|
+
|
138
|
+
payload.add_extension(:account, "/accounts/1")
|
139
|
+
.add_extension(:balance, 50)
|
140
|
+
|
141
|
+
# #<Struct:Petail::Payload:0x000122c0
|
142
|
+
# detail = nil,
|
143
|
+
# extensions = {
|
144
|
+
# :account => "/accounts/1",
|
145
|
+
# :balance => 50
|
146
|
+
# },
|
147
|
+
# instance = nil,
|
148
|
+
# status = 403,
|
149
|
+
# title = "Forbidden",
|
150
|
+
# type = "about:blank"
|
151
|
+
# >
|
152
|
+
----
|
153
|
+
|
154
|
+
Given the above, you can also check if an extension exists:
|
155
|
+
|
156
|
+
[source,ruby]
|
157
|
+
----
|
158
|
+
payload.extension? :account # true
|
159
|
+
payload.extension? :bogus # false
|
160
|
+
----
|
161
|
+
|
162
|
+
=== JSON
|
163
|
+
|
164
|
+
Both serialization and deserialization of JSON is supported. For example, given the following payload:
|
165
|
+
|
166
|
+
[source,ruby]
|
167
|
+
----
|
168
|
+
payload = Petail.new(
|
169
|
+
type: "https://test.io/problem_details/out_of_credit",
|
170
|
+
title: "You do not have enough credit.",
|
171
|
+
status: 403,
|
172
|
+
detail: "Your current balance is 30, but that costs 50.",
|
173
|
+
instance: "/accounts/1",
|
174
|
+
extensions: {
|
175
|
+
balance: 30,
|
176
|
+
accounts: %w[/accounts/1 /accounts/10]
|
177
|
+
}
|
178
|
+
)
|
179
|
+
----
|
180
|
+
|
181
|
+
This means you can serialize as follows:
|
182
|
+
|
183
|
+
[source,ruby]
|
184
|
+
----
|
185
|
+
payload.to_json
|
186
|
+
# {"type":"https://test.io/problem_details/out_of_credit","title":"You do not have enough credit.","status":403,"detail":"Your current balance is 30, but that costs 50.","instance":"/accounts/1","extensions":{"balance":30,"accounts":["/accounts/1","/accounts/10"]}}
|
187
|
+
|
188
|
+
payload.to_json indent: " ", space: " ", object_nl: "\n", array_nl: "\n"
|
189
|
+
# {
|
190
|
+
# "type": "https://test.io/problem_details/out_of_credit",
|
191
|
+
# "title": "You do not have enough credit.",
|
192
|
+
# "status": 403,
|
193
|
+
# "detail": "Your current balance is 30, but that costs 50.",
|
194
|
+
# "instance": "/accounts/1",
|
195
|
+
# "extensions": {
|
196
|
+
# "balance": 30,
|
197
|
+
# "accounts": [
|
198
|
+
# "/accounts/1",
|
199
|
+
# "/accounts/10"
|
200
|
+
# ]
|
201
|
+
# }
|
202
|
+
# }
|
203
|
+
----
|
204
|
+
|
205
|
+
💡 All of the link:https://docs.ruby-lang.org/en/master/JSON.html#module-JSON-label-Output+Options[JSON] output options are available to you when casting to JSON.
|
206
|
+
|
207
|
+
You can also deserialize by taking the result of the above and turning the raw JSON back into a `Petail::Payload`:
|
208
|
+
|
209
|
+
[source,ruby]
|
210
|
+
----
|
211
|
+
Petail.from_json "{\"type\":\"https://test.io/problem_details/out_of_credit\",\"title\":\"You do not have enough credit.\",\"status\":403,\"detail\":\"Your current balance is 30, but that costs 50.\",\"instance\":\"/accounts/1\",\"extensions\":{\"balance\":30,\"accounts\":[\"/accounts/1\",\"/accounts/10\"]}}"
|
212
|
+
|
213
|
+
# #<Struct:Petail::Payload:0x00007670
|
214
|
+
# detail = "Your current balance is 30, but that costs 50.",
|
215
|
+
# extensions = {
|
216
|
+
# :balance => 30,
|
217
|
+
# :accounts => [
|
218
|
+
# "/accounts/1",
|
219
|
+
# "/accounts/10"
|
220
|
+
# ]
|
221
|
+
# },
|
222
|
+
# instance = "/accounts/1",
|
223
|
+
# status = 403,
|
224
|
+
# title = "You do not have enough credit.",
|
225
|
+
# type = "https://test.io/problem_details/out_of_credit"
|
226
|
+
# >
|
227
|
+
----
|
228
|
+
|
229
|
+
=== XML
|
230
|
+
|
231
|
+
XML is supported too but isn't as robust as JSON support, at the moment. This is mostly due to the fact that extensions can be deeply nested so your mileage may vary. For example, given the following payload:
|
232
|
+
|
233
|
+
[source,ruby]
|
234
|
+
----
|
235
|
+
payload = Petail.new(
|
236
|
+
type: "https://test.io/problem_details/out_of_credit",
|
237
|
+
title: "You do not have enough credit.",
|
238
|
+
status: 403,
|
239
|
+
detail: "Your current balance is 30, but that costs 50.",
|
240
|
+
instance: "/accounts/1",
|
241
|
+
extensions: {
|
242
|
+
balance: 30,
|
243
|
+
accounts: %w[/accounts/1 /accounts/10]
|
244
|
+
}
|
245
|
+
)
|
246
|
+
----
|
247
|
+
|
248
|
+
This means you can serialize as follows:
|
249
|
+
|
250
|
+
[source,ruby]
|
251
|
+
----
|
252
|
+
payload.to_xml
|
253
|
+
# "<?xml version='1.0' encoding='UTF-8'?><problem xmlns='urn:ietf:rfc:7807'><type>https://test.io/problem_details/out_of_credit</type><title>You do not have enough credit.</title><status>403</status><detail>Your current balance is 30, but that costs 50.</detail><instance>/accounts/1</instance><balance>30</balance><accounts><i>/accounts/1</i><i>/accounts/10</i></accounts></problem>"
|
254
|
+
|
255
|
+
payload.to_xml indent: 2
|
256
|
+
# <?xml version='1.0' encoding='UTF-8'?>
|
257
|
+
# <problem xmlns='urn:ietf:rfc:7807'>
|
258
|
+
# <type>
|
259
|
+
# https://test.io/problem_details/out_of_credit
|
260
|
+
# </type>
|
261
|
+
# <title>
|
262
|
+
# You do not have enough credit.
|
263
|
+
# </title>
|
264
|
+
# <status>
|
265
|
+
# 403
|
266
|
+
# </status>
|
267
|
+
# <detail>
|
268
|
+
# Your current balance is 30, but that costs 50.
|
269
|
+
# </detail>
|
270
|
+
# <instance>
|
271
|
+
# /accounts/1
|
272
|
+
# </instance>
|
273
|
+
# <balance>
|
274
|
+
# 30
|
275
|
+
# </balance>
|
276
|
+
# <accounts>
|
277
|
+
# <i>
|
278
|
+
# /accounts/1
|
279
|
+
# </i>
|
280
|
+
# <i>
|
281
|
+
# /accounts/10
|
282
|
+
# </i>
|
283
|
+
# </accounts>
|
284
|
+
# </problem>
|
285
|
+
----
|
286
|
+
|
287
|
+
💡 All of the link:https://ruby.github.io/rexml/REXML/Document.html#method-i-write[REXML::Document.write] output options are available to you when casting to XML.
|
288
|
+
|
289
|
+
You can also deserialize by taking the result of the above and turning the raw JSON back into a `Petail::Payload`:
|
290
|
+
|
291
|
+
[source,ruby]
|
292
|
+
----
|
293
|
+
payload = Petail.from_xml <<~XML
|
294
|
+
<?xml version='1.0' encoding='UTF-8'?>
|
295
|
+
<problem xmlns='urn:ietf:rfc:7807'>
|
296
|
+
<type>https://test.io/problem_details/out_of_credit</type>
|
297
|
+
<title>You do not have enough credit.</title>
|
298
|
+
<status>403</status>
|
299
|
+
<detail>Your current balance is 30, but that costs 50.</detail>
|
300
|
+
<instance>/accounts/1</instance>
|
301
|
+
<balance>30</balance>
|
302
|
+
<accounts>
|
303
|
+
<i>/accounts/1</i>
|
304
|
+
<i>/accounts/10</i>
|
305
|
+
</accounts>
|
306
|
+
</problem>
|
307
|
+
XML
|
308
|
+
|
309
|
+
# #<Struct:Petail::Payload:0x00007670
|
310
|
+
# detail = "Your current balance is 30, but that costs 50.",
|
311
|
+
# extensions = {
|
312
|
+
# :balance => "30",
|
313
|
+
# :accounts => [
|
314
|
+
# "/accounts/1",
|
315
|
+
# "/accounts/10"
|
316
|
+
# ]
|
317
|
+
# },
|
318
|
+
# instance = "/accounts/1",
|
319
|
+
# status = 403,
|
320
|
+
# title = "You do not have enough credit.",
|
321
|
+
# type = "https://test.io/problem_details/out_of_credit"
|
322
|
+
# >
|
323
|
+
----
|
324
|
+
|
325
|
+
== Development
|
326
|
+
|
327
|
+
To contribute, run:
|
328
|
+
|
329
|
+
[source,bash]
|
330
|
+
----
|
331
|
+
git clone https://github.com/bkuhlmann/petail
|
332
|
+
cd petail
|
333
|
+
bin/setup
|
334
|
+
----
|
335
|
+
|
336
|
+
You can also use the IRB console for direct access to all objects:
|
337
|
+
|
338
|
+
[source,bash]
|
339
|
+
----
|
340
|
+
bin/console
|
341
|
+
----
|
342
|
+
|
343
|
+
== Tests
|
344
|
+
|
345
|
+
To test, run:
|
346
|
+
|
347
|
+
[source,bash]
|
348
|
+
----
|
349
|
+
bin/rake
|
350
|
+
----
|
351
|
+
|
352
|
+
== link:https://alchemists.io/policies/license[License]
|
353
|
+
|
354
|
+
== link:https://alchemists.io/policies/security[Security]
|
355
|
+
|
356
|
+
== link:https://alchemists.io/policies/code_of_conduct[Code of Conduct]
|
357
|
+
|
358
|
+
== link:https://alchemists.io/policies/contributions[Contributions]
|
359
|
+
|
360
|
+
== link:https://alchemists.io/policies/developer_certificate_of_origin[Developer Certificate of Origin]
|
361
|
+
|
362
|
+
== link:https://alchemists.io/projects/petail/versions[Versions]
|
363
|
+
|
364
|
+
== link:https://alchemists.io/community[Community]
|
365
|
+
|
366
|
+
== Credits
|
367
|
+
|
368
|
+
* Built with link:https://alchemists.io/projects/gemsmith[Gemsmith].
|
369
|
+
* Engineered by link:https://alchemists.io/team/brooke_kuhlmann[Brooke Kuhlmann].
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
require "rack/utils"
|
5
|
+
require "rexml"
|
6
|
+
|
7
|
+
module Petail
|
8
|
+
# Models the problem details response payload.
|
9
|
+
Payload = Struct.new :type, :title, :status, :detail, :instance, :extensions do
|
10
|
+
def self.for(**attributes)
|
11
|
+
status = attributes.delete(:status).then { Rack::Utils.status_code it if it }
|
12
|
+
title = attributes.delete(:title).then { it || Rack::Utils::HTTP_STATUS_CODES[status] }
|
13
|
+
|
14
|
+
new title:, status:, **attributes
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.from_json(body) = self.for(**JSON(body, symbolize_names: true))
|
18
|
+
|
19
|
+
# :reek:TooManyStatements
|
20
|
+
def self.from_xml body, deserializer: XML::Deserializer
|
21
|
+
elements = REXML::Document.new(body).root.elements
|
22
|
+
|
23
|
+
attributes = elements.each_with_object({extensions: {}}) do |element, collection|
|
24
|
+
name = element.name
|
25
|
+
text = element.text
|
26
|
+
|
27
|
+
case name
|
28
|
+
when "type", "title", "detail", "instance", "status" then collection[name.to_sym] = text
|
29
|
+
else collection[:extensions].merge! deserializer.call(element)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
self.for(**attributes)
|
34
|
+
end
|
35
|
+
|
36
|
+
def initialize(**)
|
37
|
+
super
|
38
|
+
self[:type] ||= "about:blank"
|
39
|
+
self[:title] ||= Rack::Utils::HTTP_STATUS_CODES[status]
|
40
|
+
self[:extensions] ||= {}
|
41
|
+
freeze
|
42
|
+
end
|
43
|
+
|
44
|
+
def add_extension name, value
|
45
|
+
self[:extensions][name] = value
|
46
|
+
self
|
47
|
+
end
|
48
|
+
|
49
|
+
def extension?(name) = extensions.key? name
|
50
|
+
|
51
|
+
def to_h = super.compact.tap { it.delete :extensions if extensions.empty? }
|
52
|
+
|
53
|
+
def to_json(*) = to_h.to_json(*)
|
54
|
+
|
55
|
+
# :reek:TooManyStatements
|
56
|
+
def to_xml(serializer: XML::Serializer, **options)
|
57
|
+
document = REXML::Document.new
|
58
|
+
document.add REXML::XMLDecl.new("1.0", "UTF-8")
|
59
|
+
|
60
|
+
problem = REXML::Element.new("problem").add_namespace("urn:ietf:rfc:7807")
|
61
|
+
document.add problem
|
62
|
+
|
63
|
+
attributes = to_h
|
64
|
+
attributes.merge! attributes.delete :extensions if extensions.any?
|
65
|
+
attributes.each { |name, value| serializer.call name, value, problem }
|
66
|
+
|
67
|
+
"".dup.tap { document.write(**options, output: it) }
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Petail
|
4
|
+
module XML
|
5
|
+
# Converts an XML element into a Hash.
|
6
|
+
Deserializer = lambda do |node, attributes = {}|
|
7
|
+
if node.has_elements?
|
8
|
+
node.each_element do |element|
|
9
|
+
if element.name == "i"
|
10
|
+
attributes[node.name.to_sym] ||= []
|
11
|
+
attributes[node.name.to_sym].push element.text
|
12
|
+
elsif element.elements.empty?
|
13
|
+
attributes[element.name.to_sym] = element.text
|
14
|
+
else
|
15
|
+
attributes = {element.name.to_sym => Deserializer.call(element, attributes)}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
else
|
19
|
+
attributes[node.name.to_sym] = node.text
|
20
|
+
end
|
21
|
+
|
22
|
+
attributes
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Petail
|
4
|
+
module XML
|
5
|
+
# Converts name and value into an XML element.
|
6
|
+
Serializer = lambda do |name, value, node|
|
7
|
+
if value.is_a? Array
|
8
|
+
element = node.add_element name.to_s
|
9
|
+
value.map { |item| element.add_element("i").add_text item.to_s }
|
10
|
+
else
|
11
|
+
node.add_element(name.to_s).add_text value.to_s
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/petail.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "petail/payload"
|
4
|
+
require "petail/xml/deserializer"
|
5
|
+
require "petail/xml/serializer"
|
6
|
+
|
7
|
+
# Main namespace.
|
8
|
+
module Petail
|
9
|
+
MEDIA_TYPE_JSON = "application/problem+json"
|
10
|
+
MEDIA_TYPE_XML = "application/problem+xml"
|
11
|
+
TYPES = %i[json xml].freeze
|
12
|
+
|
13
|
+
def self.from_json(...) = Payload.from_json(...)
|
14
|
+
|
15
|
+
def self.from_xml(...) = Payload.from_xml(...)
|
16
|
+
|
17
|
+
def self.media_type_for key, types: TYPES
|
18
|
+
types.include?(key) ? const_get("MEDIA_TYPE_#{key.upcase}") : ""
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.new(**) = Payload.for(**)
|
22
|
+
end
|
data/petail.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "petail"
|
5
|
+
spec.version = "0.0.0"
|
6
|
+
spec.authors = ["Brooke Kuhlmann"]
|
7
|
+
spec.email = ["brooke@alchemists.io"]
|
8
|
+
spec.homepage = "https://alchemists.io/projects/petail"
|
9
|
+
spec.summary = "A RFC 7807 Problem Details for HTTP APIs implementation."
|
10
|
+
spec.license = "Hippocratic-2.1"
|
11
|
+
|
12
|
+
spec.metadata = {
|
13
|
+
"bug_tracker_uri" => "https://github.com/bkuhlmann/petail/issues",
|
14
|
+
"changelog_uri" => "https://alchemists.io/projects/petail/versions",
|
15
|
+
"homepage_uri" => "https://alchemists.io/projects/petail",
|
16
|
+
"funding_uri" => "https://github.com/sponsors/bkuhlmann",
|
17
|
+
"label" => "Petail",
|
18
|
+
"rubygems_mfa_required" => "true",
|
19
|
+
"source_code_uri" => "https://github.com/bkuhlmann/petail"
|
20
|
+
}
|
21
|
+
|
22
|
+
spec.signing_key = Gem.default_key_path
|
23
|
+
spec.cert_chain = [Gem.default_cert_path]
|
24
|
+
|
25
|
+
spec.required_ruby_version = "~> 3.4"
|
26
|
+
spec.add_dependency "rack", "~> 3.1"
|
27
|
+
|
28
|
+
spec.extra_rdoc_files = Dir["README*", "LICENSE*"]
|
29
|
+
spec.files = Dir["*.gemspec", "lib/**/*"]
|
30
|
+
end
|
data.tar.gz.sig
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: petail
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brooke Kuhlmann
|
8
|
+
bindir: bin
|
9
|
+
cert_chain:
|
10
|
+
- |
|
11
|
+
-----BEGIN CERTIFICATE-----
|
12
|
+
MIIENjCCAp6gAwIBAgIBAjANBgkqhkiG9w0BAQsFADBBMQ8wDQYDVQQDDAZicm9v
|
13
|
+
a2UxGjAYBgoJkiaJk/IsZAEZFgphbGNoZW1pc3RzMRIwEAYKCZImiZPyLGQBGRYC
|
14
|
+
aW8wHhcNMjUwMzIyMTQ1NDE3WhcNMjYwMzIyMTQ1NDE3WjBBMQ8wDQYDVQQDDAZi
|
15
|
+
cm9va2UxGjAYBgoJkiaJk/IsZAEZFgphbGNoZW1pc3RzMRIwEAYKCZImiZPyLGQB
|
16
|
+
GRYCaW8wggGiMA0GCSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQCro8tj5/E1Hg88
|
17
|
+
f4qfiwPVd2zJQHvdYt4GHVvuHRRgx4HGhJuNp+4BId08RBn7V6V1MW6MY3kezRBs
|
18
|
+
M+7QOQ4b1xNLTvY7FYQB1wGK5a4x7TTokDrPYQxDB2jmsdDYCzVbIMrAvUfcecRi
|
19
|
+
khyGZCdByiiCl4fKv77P12tTT+NfsvXkLt/AYCGwjOUyGKTQ01Z6eC09T27GayPH
|
20
|
+
QQvIkakyFgcJtzSyGzs8bzK5q9u7wQ12MNTjJoXzW69lqp0oNvDylu81EiSUb5S6
|
21
|
+
QzzPxZBiRB1sgtbt1gUbVI262ZDq1gR+HxPFmp+Cgt7ZLIJZAtesQvtcMzseXpfn
|
22
|
+
hpmm0Sw22KGhRAy/mqHBRhDl5HqS1SJp2Ko3lcnpXeFResp0HNlt8NSu13vhC08j
|
23
|
+
GUHU9MyIXbFOsnp3K3ADrAVjPWop8EZkmUR3MV/CUm00w2cZHCSGiXl1KMpiVKvk
|
24
|
+
Ywr1gd2ZME4QLSo+EXUtLxDUa/W3xnBS8dBOuMMz02FPWYr3PN8CAwEAAaM5MDcw
|
25
|
+
CQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0OBBYEFAFgmv0tYMZnItuPycSM
|
26
|
+
F5wykJEVMA0GCSqGSIb3DQEBCwUAA4IBgQBlzRfyAYx/fCFjizS0Npxw4+4T3aYL
|
27
|
+
hbXoDqQRWjxuhFZcXUymhz3r8/Ltyri9lSof8grzB+8/+mrMVms7Gwt5qolk6zdn
|
28
|
+
FkySGy/jmpN12ldOHFbBEnyVBZNBvOBVb8zkkw8PhiHdBdXOUm4Jy39yJvBLfjcC
|
29
|
+
iM1aeWPmgPy1GbvZU+leRGZLt6dRIR9oCDXcWLRjha8xLMoz6Yn9fJBYexBA3iEz
|
30
|
+
h5S7pn4AX/JhVRiSyl8pAy4jEKydpyQrliH3gHkpNmUS/XDczP+9xX1bAB4BvqL2
|
31
|
+
NCxMcQ+hiJNqCKpPgHxaOOHZfIxV33logIuPEQ8NryHAwZ9ZWnwtYDE8kQGGKskI
|
32
|
+
Kkm6QT474hZl7MpwiJjWgW313CR7jUEekQahX1QxCxHPI7LSrKpno0plH3uWIOQp
|
33
|
+
KUlkb9uyACBgyRO52ZHiDVI8YvtU5O/j9pSes9/3XgvBeC1onx4qWp+uRX7eVsYS
|
34
|
+
GiijocTc3enZVrXERetaXj8/9XWs3fB3HWY=
|
35
|
+
-----END CERTIFICATE-----
|
36
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
37
|
+
dependencies:
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rack
|
40
|
+
requirement: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - "~>"
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '3.1'
|
45
|
+
type: :runtime
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - "~>"
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '3.1'
|
52
|
+
email:
|
53
|
+
- brooke@alchemists.io
|
54
|
+
executables: []
|
55
|
+
extensions: []
|
56
|
+
extra_rdoc_files:
|
57
|
+
- LICENSE.adoc
|
58
|
+
- README.adoc
|
59
|
+
files:
|
60
|
+
- LICENSE.adoc
|
61
|
+
- README.adoc
|
62
|
+
- lib/petail.rb
|
63
|
+
- lib/petail/payload.rb
|
64
|
+
- lib/petail/xml/deserializer.rb
|
65
|
+
- lib/petail/xml/serializer.rb
|
66
|
+
- petail.gemspec
|
67
|
+
homepage: https://alchemists.io/projects/petail
|
68
|
+
licenses:
|
69
|
+
- Hippocratic-2.1
|
70
|
+
metadata:
|
71
|
+
bug_tracker_uri: https://github.com/bkuhlmann/petail/issues
|
72
|
+
changelog_uri: https://alchemists.io/projects/petail/versions
|
73
|
+
homepage_uri: https://alchemists.io/projects/petail
|
74
|
+
funding_uri: https://github.com/sponsors/bkuhlmann
|
75
|
+
label: Petail
|
76
|
+
rubygems_mfa_required: 'true'
|
77
|
+
source_code_uri: https://github.com/bkuhlmann/petail
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - "~>"
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '3.4'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubygems_version: 3.6.7
|
93
|
+
specification_version: 4
|
94
|
+
summary: A RFC 7807 Problem Details for HTTP APIs implementation.
|
95
|
+
test_files: []
|
metadata.gz.sig
ADDED
Binary file
|