apia 3.5.1 → 3.6.0

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: 655dd00a146e4b25e600c926b0b57f160b8eb22d5908f0a7617c5c69d2781867
4
- data.tar.gz: 57e7d53b19ed835966326bea22228b873218865505823ebf5cc13c3694274d30
3
+ metadata.gz: 3d20a8612da20a33fb3df02632c88c71be48a6bb8741e599257526eb95b8fab8
4
+ data.tar.gz: 62b60c3afadb025f22d460bfed84c6fcbae83a9189a51c39d00d4eedde94c72a
5
5
  SHA512:
6
- metadata.gz: 16961608a1f749b7dea60e91b26a475fe2079f1c80b0535216a2a4551d8d481298733b33c304d6c6e2dd2811f253011d991fc234e42186e945f778ff7f595738
7
- data.tar.gz: b611dd81deba0c3114af55c1fd3a8605ae5a4eb746730ba1d19b27f7b95302857962abbc2e7e2c7f7c4b59f4b1cf7bfffc87a440db3f396c350be4b9eb8f3d0a
6
+ metadata.gz: 861c7382e3a4b0d2f14af9e20379b14338b7aa4acf93e3c0b3ebda438d8c010d371a1c9e088caa1cc321d22261e201d994acc6346a20948f14b10a6edef8fe18
7
+ data.tar.gz: 8c8b32ca974212bfaf1df295cd59b3154ef92f810385b772e57b75dced6b0de2cc31d9214382152c255ad06debe40652374c2d94d56d02c1fc5792c5fcd469f4
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: 'text/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: 'application/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 = :json,
11
+ PLAIN = :plain
12
+ ].freeze
13
+
9
14
  attr_accessor :status
10
15
  attr_reader :fields
11
16
  attr_reader :headers
@@ -20,6 +25,11 @@ module Apia
20
25
  @headers = {}
21
26
  end
22
27
 
28
+ def plain_text_body(body)
29
+ @type = PLAIN
30
+ @body = body
31
+ end
32
+
23
33
  # Add a field value for this endpoint
24
34
  #
25
35
  # @param name [Symbol]
@@ -53,11 +63,20 @@ module Apia
53
63
  @body || hash
54
64
  end
55
65
 
66
+ def type
67
+ @type || JSON
68
+ end
69
+
56
70
  # Return the rack triplet for this response
57
71
  #
58
72
  # @return [Array]
59
73
  def rack_triplet
60
- Rack.json_triplet(body, headers: @headers, status: @status)
74
+ case type
75
+ when JSON
76
+ Rack.json_triplet(body, headers: headers, status: status)
77
+ when PLAIN
78
+ Rack.plain_triplet(body, headers: headers, status: status)
79
+ end
61
80
  end
62
81
 
63
82
  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.6.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.6.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-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json