apia 3.5.1 → 3.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/apia/definitions/endpoint.rb +2 -0
- data/lib/apia/dsls/endpoint.rb +4 -0
- data/lib/apia/rack.rb +26 -3
- data/lib/apia/response.rb +19 -1
- data/lib/apia/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3488b6d92d65ff71eff5ec22b944622042981311d6a7505fc09578093a9e4402
|
4
|
+
data.tar.gz: aff3ff29a9674a4eb622e91a89e74d380e11728e683407e3e9bc1546641ea483
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 749d596efa11efc2965952a4ae2fc6af4c62fb9189e1839675ca3e00040fb4c4a99ab888d2e1589b546b002434e019735c1896398d494404d8cc7bd8a7e6f944
|
7
|
+
data.tar.gz: 682ff1cf40fe2399ae2754ef9e78388282cfe8c28b38b7153425565ab568084826d7d49fccdbaf5a539e80bd4b4cfa79e1b968258ebb5e23525f14b4d3051c58
|
@@ -14,6 +14,7 @@ module Apia
|
|
14
14
|
attr_accessor :authenticator
|
15
15
|
attr_accessor :action
|
16
16
|
attr_accessor :http_status
|
17
|
+
attr_accessor :response_type
|
17
18
|
attr_accessor :paginated_field
|
18
19
|
attr_reader :fields
|
19
20
|
attr_reader :scopes
|
@@ -21,6 +22,7 @@ module Apia
|
|
21
22
|
def setup
|
22
23
|
@fields = FieldSet.new
|
23
24
|
@http_status = 200
|
25
|
+
@response_type = Apia::Response::JSON
|
24
26
|
@scopes = []
|
25
27
|
end
|
26
28
|
|
data/lib/apia/dsls/endpoint.rb
CHANGED
@@ -40,6 +40,10 @@ module Apia
|
|
40
40
|
@definition.http_status = status
|
41
41
|
end
|
42
42
|
|
43
|
+
def response_type(type)
|
44
|
+
@definition.response_type = type
|
45
|
+
end
|
46
|
+
|
43
47
|
def field(name, *args, type: nil, **options, &block)
|
44
48
|
if @definition.fields_overriden?
|
45
49
|
raise Apia::StandardError, 'Cannot add fields to an endpoint that has a separate fieldset'
|
data/lib/apia/rack.rb
CHANGED
@@ -152,6 +152,16 @@ module Apia
|
|
152
152
|
|
153
153
|
class << self
|
154
154
|
|
155
|
+
# Return a plain text triplet for the given body.
|
156
|
+
#
|
157
|
+
# @param body [String]
|
158
|
+
# @param status [Integer]
|
159
|
+
# @param headers [Hash]
|
160
|
+
# @return [Array]
|
161
|
+
def plain_triplet(body, status: 200, headers: {})
|
162
|
+
response_triplet(body, content_type: Apia::Response::PLAIN, status: status, headers: headers)
|
163
|
+
end
|
164
|
+
|
155
165
|
# Return a JSON-ready triplet for the given body.
|
156
166
|
#
|
157
167
|
# @param body [Hash, Array]
|
@@ -159,11 +169,24 @@ module Apia
|
|
159
169
|
# @param headers [Hash]
|
160
170
|
# @return [Array]
|
161
171
|
def json_triplet(body, status: 200, headers: {})
|
162
|
-
|
172
|
+
response_triplet(body.to_json, content_type: Apia::Response::JSON, status: status, headers: headers)
|
173
|
+
end
|
174
|
+
|
175
|
+
# Return a triplet for the given body.
|
176
|
+
#
|
177
|
+
# @param body [Hash, Array]
|
178
|
+
# @param content_type [String]
|
179
|
+
# @param status [Integer]
|
180
|
+
# @param headers [Hash]
|
181
|
+
# @return [Array]
|
182
|
+
def response_triplet(body, content_type:, status: 200, headers: {})
|
183
|
+
content_length = body.bytesize.to_s
|
184
|
+
body = [body] unless body.respond_to?(:each)
|
185
|
+
|
163
186
|
[
|
164
187
|
status,
|
165
|
-
headers.merge('content-type' =>
|
166
|
-
|
188
|
+
headers.merge('content-type' => content_type, 'content-length' => content_length),
|
189
|
+
body
|
167
190
|
]
|
168
191
|
end
|
169
192
|
|
data/lib/apia/response.rb
CHANGED
@@ -6,6 +6,11 @@ require 'apia/rack'
|
|
6
6
|
module Apia
|
7
7
|
class Response
|
8
8
|
|
9
|
+
TYPES = [
|
10
|
+
JSON = 'application/json',
|
11
|
+
PLAIN = 'text/plain'
|
12
|
+
].freeze
|
13
|
+
|
9
14
|
attr_accessor :status
|
10
15
|
attr_reader :fields
|
11
16
|
attr_reader :headers
|
@@ -16,10 +21,18 @@ module Apia
|
|
16
21
|
@endpoint = endpoint
|
17
22
|
|
18
23
|
@status = @endpoint.definition.http_status_code
|
24
|
+
@type = @endpoint.definition.response_type
|
19
25
|
@fields = {}
|
20
26
|
@headers = {}
|
21
27
|
end
|
22
28
|
|
29
|
+
def plain_text_body(body)
|
30
|
+
warn '[DEPRECATION] `plain_text_body` is deprecated. Please set use `response_type` in the endpoint definition, and set the response `body` directly instead.'
|
31
|
+
|
32
|
+
@type = PLAIN
|
33
|
+
@body = body
|
34
|
+
end
|
35
|
+
|
23
36
|
# Add a field value for this endpoint
|
24
37
|
#
|
25
38
|
# @param name [Symbol]
|
@@ -57,7 +70,12 @@ module Apia
|
|
57
70
|
#
|
58
71
|
# @return [Array]
|
59
72
|
def rack_triplet
|
60
|
-
|
73
|
+
case @type
|
74
|
+
when JSON
|
75
|
+
Rack.json_triplet(body, headers: headers, status: status)
|
76
|
+
when PLAIN
|
77
|
+
Rack.plain_triplet(body, headers: headers, status: status)
|
78
|
+
end
|
61
79
|
end
|
62
80
|
|
63
81
|
end
|
data/lib/apia/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: apia
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Cooke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -185,7 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
185
185
|
- !ruby/object:Gem::Version
|
186
186
|
version: '0'
|
187
187
|
requirements: []
|
188
|
-
rubygems_version: 3.3.
|
188
|
+
rubygems_version: 3.3.27
|
189
189
|
signing_key:
|
190
190
|
specification_version: 4
|
191
191
|
summary: This gem provides a friendly DSL for constructing HTTP APIs.
|