apia 3.6.0 → 3.7.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3d20a8612da20a33fb3df02632c88c71be48a6bb8741e599257526eb95b8fab8
4
- data.tar.gz: 62b60c3afadb025f22d460bfed84c6fcbae83a9189a51c39d00d4eedde94c72a
3
+ metadata.gz: 3ca83d2cf83582c065e6af3a0ee926956ef3be223c0855103ee3df4a1bb5cd06
4
+ data.tar.gz: b6c384351af2236c7755c64e4b6c138594967079d6cb67587420f7773d64666f
5
5
  SHA512:
6
- metadata.gz: 861c7382e3a4b0d2f14af9e20379b14338b7aa4acf93e3c0b3ebda438d8c010d371a1c9e088caa1cc321d22261e201d994acc6346a20948f14b10a6edef8fe18
7
- data.tar.gz: 8c8b32ca974212bfaf1df295cd59b3154ef92f810385b772e57b75dced6b0de2cc31d9214382152c255ad06debe40652374c2d94d56d02c1fc5792c5fcd469f4
6
+ metadata.gz: 6ec614a5e14c0b146f1deb03e9193b3aad0a9f3e093f8b4fef298bc4e258308396c7d7ace3a3518b71b299b69ca202518ccd4edc957ab0014628643d46fc2740
7
+ data.tar.gz: 994a2a935d9c78da37e88f983b4e2e2466f229fba9801615a07f87a52ea3196fe49405311a56e481810c812efee6f342e7d92d133c6a9c292843af6a674f34f9
@@ -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
 
@@ -37,7 +37,7 @@ module Apia
37
37
  # API.
38
38
  #
39
39
  # @param errors [Apia::ManifestErrors]
40
- # @reeturn [void]
40
+ # @return [void]
41
41
  def validate(errors)
42
42
  unless code.is_a?(Symbol)
43
43
  errors.add self, 'InvalidCode', 'Code must be a symbol'
@@ -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
@@ -159,7 +159,7 @@ module Apia
159
159
  # @param headers [Hash]
160
160
  # @return [Array]
161
161
  def plain_triplet(body, status: 200, headers: {})
162
- response_triplet(body, content_type: 'text/plain', status: status, headers: headers)
162
+ response_triplet(body, content_type: Apia::Response::PLAIN, status: status, headers: headers)
163
163
  end
164
164
 
165
165
  # Return a JSON-ready triplet for the given body.
@@ -169,7 +169,7 @@ module Apia
169
169
  # @param headers [Hash]
170
170
  # @return [Array]
171
171
  def json_triplet(body, status: 200, headers: {})
172
- response_triplet(body.to_json, content_type: 'application/json', status: status, headers: headers)
172
+ response_triplet(body.to_json, content_type: Apia::Response::JSON, status: status, headers: headers)
173
173
  end
174
174
 
175
175
  # Return a triplet for the given body.
data/lib/apia/response.rb CHANGED
@@ -7,8 +7,8 @@ module Apia
7
7
  class Response
8
8
 
9
9
  TYPES = [
10
- JSON = :json,
11
- PLAIN = :plain
10
+ JSON = 'application/json',
11
+ PLAIN = 'text/plain'
12
12
  ].freeze
13
13
 
14
14
  attr_accessor :status
@@ -21,11 +21,14 @@ module Apia
21
21
  @endpoint = endpoint
22
22
 
23
23
  @status = @endpoint.definition.http_status_code
24
+ @type = @endpoint.definition.response_type
24
25
  @fields = {}
25
26
  @headers = {}
26
27
  end
27
28
 
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
+
29
32
  @type = PLAIN
30
33
  @body = body
31
34
  end
@@ -63,19 +66,18 @@ module Apia
63
66
  @body || hash
64
67
  end
65
68
 
66
- def type
67
- @type || JSON
68
- end
69
-
70
69
  # Return the rack triplet for this response
71
70
  #
72
71
  # @return [Array]
73
72
  def rack_triplet
74
- case type
75
- when JSON
76
- Rack.json_triplet(body, headers: headers, status: status)
77
- when PLAIN
73
+ # Errors will always be sent as a hash intended for JSON encoding,
74
+ # even if the endpoint specifies a plain text response, so only
75
+ # send a pain response if the type is plaintext _and_ the body is
76
+ # a string
77
+ if @type == PLAIN && body.is_a?(String)
78
78
  Rack.plain_triplet(body, headers: headers, status: status)
79
+ else
80
+ Rack.json_triplet(body, headers: headers, status: status)
79
81
  end
80
82
  end
81
83
 
data/lib/apia/version.rb CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Apia
4
4
 
5
- VERSION = '3.6.0'
5
+ VERSION = '3.7.1'
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.6.0
4
+ version: 3.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-17 00:00:00.000000000 Z
11
+ date: 2024-05-21 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.