apiphobic-middleware 1.1.0 → 1.2.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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d5fc848c052df27e6f94826ab6401718c276e1d24f1055b3eda185591cee5362
|
4
|
+
data.tar.gz: 9a20b4f0002aa360ec928e7cc501721b85b75afd28463c629e6f78be4c942b89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9a079597f41bebd6ada817d9470b974f86f3e66678bc2a342bfe6e3bd1adcf8333cc9e635456aa6a6f54637bcb7a7e8582bd8e39f5f34517854a18bcc1b4a713
|
7
|
+
data.tar.gz: 78bcf6c812b9017d35172a3dc0e494311227b0f8df6321bbc3e9b6afa7fdfdf8ea561f8a8f6d4b46014be9c249095cdd342c9e128549a486fda07a9f6b542d57
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
@@ -3,6 +3,7 @@
|
|
3
3
|
require 'json'
|
4
4
|
require 'apiphobic/requests/transform_json_api'
|
5
5
|
require 'apiphobic/responses/invalid_request_body'
|
6
|
+
require 'apiphobic/responses/transform_json_api'
|
6
7
|
|
7
8
|
module Apiphobic
|
8
9
|
module Middleware
|
@@ -17,7 +18,11 @@ class JsonApiParameters
|
|
17
18
|
|
18
19
|
json_api_request = request.transform
|
19
20
|
|
20
|
-
@app.call(json_api_request)
|
21
|
+
raw_response = @app.call(json_api_request)
|
22
|
+
|
23
|
+
response = Responses::TransformJsonApi.new(*raw_response)
|
24
|
+
|
25
|
+
response.transform
|
21
26
|
rescue JSON::ParserError
|
22
27
|
Responses::InvalidRequestBody.call(env)
|
23
28
|
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Apiphobic
|
4
|
+
module Responses
|
5
|
+
class TransformJsonApi
|
6
|
+
attr_accessor :status,
|
7
|
+
:headers,
|
8
|
+
:body
|
9
|
+
|
10
|
+
def initialize(status, headers, body)
|
11
|
+
self.status = status
|
12
|
+
self.headers = headers
|
13
|
+
self.body = body
|
14
|
+
end
|
15
|
+
|
16
|
+
def transform
|
17
|
+
[
|
18
|
+
status,
|
19
|
+
transformed_headers,
|
20
|
+
body,
|
21
|
+
]
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def transformed_headers
|
27
|
+
headers[content_parameter] = transformed_body
|
28
|
+
|
29
|
+
headers
|
30
|
+
end
|
31
|
+
|
32
|
+
def transformed_body
|
33
|
+
return request_body if request_body == '' || request_body.nil?
|
34
|
+
|
35
|
+
relationships = request_hash.dig('data', 'relationships') || {}
|
36
|
+
|
37
|
+
relationships.dup.each do |relationship_name, data|
|
38
|
+
relationships.delete(relationship_name)
|
39
|
+
relationships[relationship_name.tr('_', '-')] = data
|
40
|
+
end
|
41
|
+
|
42
|
+
JSON.dump(request_hash)
|
43
|
+
end
|
44
|
+
|
45
|
+
def content_parameter
|
46
|
+
headers['rack.input'] ? 'rack.input' : 'RACK_INPUT'
|
47
|
+
end
|
48
|
+
|
49
|
+
def request_hash
|
50
|
+
@request_hash ||= JSON.parse(request_body)
|
51
|
+
end
|
52
|
+
|
53
|
+
def request_body
|
54
|
+
if headers['rack.input']
|
55
|
+
headers['rack.input'].read
|
56
|
+
else
|
57
|
+
headers['RACK_INPUT'].to_s
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: apiphobic-middleware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- thegranddesign
|
@@ -163,6 +163,7 @@ files:
|
|
163
163
|
- lib/apiphobic/responses/invalid_request_body.rb
|
164
164
|
- lib/apiphobic/responses/invalid_subdomain.rb
|
165
165
|
- lib/apiphobic/responses/invalid_token.rb
|
166
|
+
- lib/apiphobic/responses/transform_json_api.rb
|
166
167
|
homepage: ''
|
167
168
|
licenses:
|
168
169
|
- MIT
|
metadata.gz.sig
CHANGED
Binary file
|