apia 3.5.1 → 3.7.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: 655dd00a146e4b25e600c926b0b57f160b8eb22d5908f0a7617c5c69d2781867
4
- data.tar.gz: 57e7d53b19ed835966326bea22228b873218865505823ebf5cc13c3694274d30
3
+ metadata.gz: 3488b6d92d65ff71eff5ec22b944622042981311d6a7505fc09578093a9e4402
4
+ data.tar.gz: aff3ff29a9674a4eb622e91a89e74d380e11728e683407e3e9bc1546641ea483
5
5
  SHA512:
6
- metadata.gz: 16961608a1f749b7dea60e91b26a475fe2079f1c80b0535216a2a4551d8d481298733b33c304d6c6e2dd2811f253011d991fc234e42186e945f778ff7f595738
7
- data.tar.gz: b611dd81deba0c3114af55c1fd3a8605ae5a4eb746730ba1d19b27f7b95302857962abbc2e7e2c7f7c4b59f4b1cf7bfffc87a440db3f396c350be4b9eb8f3d0a
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
 
@@ -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
- body_as_json = body.to_json
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' => 'application/json', 'content-length' => body_as_json.bytesize.to_s),
166
- [body_as_json]
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
- Rack.json_triplet(body, headers: @headers, status: @status)
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Apia
4
4
 
5
- VERSION = '3.5.1'
5
+ VERSION = '3.7.0'
6
6
 
7
7
  end
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.5.1
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: 2023-11-09 00:00:00.000000000 Z
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.26
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.