apia 3.5.0 → 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: 360c2cb98ee125b41392e2005987b1736a452d02307bc234f074ba74e6dda86a
4
- data.tar.gz: d82e4a00e799fbac207669f8ba011e57212d5f0e2afb513932aabd0b2cc31d9d
3
+ metadata.gz: 3d20a8612da20a33fb3df02632c88c71be48a6bb8741e599257526eb95b8fab8
4
+ data.tar.gz: 62b60c3afadb025f22d460bfed84c6fcbae83a9189a51c39d00d4eedde94c72a
5
5
  SHA512:
6
- metadata.gz: a0ecfdfa99d462409158ea2545215839567b2728ff0e8e91d1e4b6a2391fe517efb2976f038a73b04d320b1968532620acd38f728553a5682116ff06a336109a
7
- data.tar.gz: 406100fb8a0afdb2fda77bd9a3a8c3bcab5abc0d0db54232d0e1480b7a2a7e3930102e93a2d4784ee958d5a338db75def9efe66bf6c587945295f71250706956
6
+ metadata.gz: 861c7382e3a4b0d2f14af9e20379b14338b7aa4acf93e3c0b3ebda438d8c010d371a1c9e088caa1cc321d22261e201d994acc6346a20948f14b10a6edef8fe18
7
+ data.tar.gz: 8c8b32ca974212bfaf1df295cd59b3154ef92f810385b772e57b75dced6b0de2cc31d9214382152c255ad06debe40652374c2d94d56d02c1fc5792c5fcd469f4
data/lib/apia/rack.rb CHANGED
@@ -79,7 +79,8 @@ module Apia
79
79
 
80
80
  validate_api if development?
81
81
 
82
- route = find_route(request_method, api_path)
82
+ access_control_request_method = env['HTTP_ACCESS_CONTROL_REQUEST_METHOD']&.upcase if request_method == 'OPTIONS'
83
+ route = find_route((access_control_request_method || request_method), api_path)
83
84
  if route.nil?
84
85
  Apia::Notifications.notify(:request_route_not_found, notify_hash)
85
86
  raise RackError.new(404, 'route_not_found', "No route matches '#{api_path}' for #{request_method}")
@@ -151,6 +152,16 @@ module Apia
151
152
 
152
153
  class << self
153
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
+
154
165
  # Return a JSON-ready triplet for the given body.
155
166
  #
156
167
  # @param body [Hash, Array]
@@ -158,11 +169,24 @@ module Apia
158
169
  # @param headers [Hash]
159
170
  # @return [Array]
160
171
  def json_triplet(body, status: 200, headers: {})
161
- 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
+
162
186
  [
163
187
  status,
164
- headers.merge('content-type' => 'application/json', 'content-length' => body_as_json.bytesize.to_s),
165
- [body_as_json]
188
+ headers.merge('content-type' => content_type, 'content-length' => content_length),
189
+ body
166
190
  ]
167
191
  end
168
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.0'
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.0
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-08-14 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