Skip to content

Commit

Permalink
spike: Add pain text responses
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbearman committed Mar 22, 2024
1 parent b1d9d42 commit 5420ab0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/apia/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ def triplet_for_exception(exception)

class << self

# Return a triplet for the given body.
#
# @param body [Hash, Array]
# @param status [Integer]
# @param headers [Hash]
# @return [Array]
def plain_triplet(body, status: 200, headers: {})
[
status,
headers.merge('content-type' => 'text/plain', 'content-length' => body.bytesize.to_s),
[body]
]
end

# Return a JSON-ready triplet for the given body.
#
# @param body [Hash, Array]
Expand Down
10 changes: 9 additions & 1 deletion lib/apia/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Response
attr_reader :fields
attr_reader :headers
attr_writer :body
attr_writer :type

def initialize(request, endpoint)
@request = request
Expand Down Expand Up @@ -53,11 +54,18 @@ def body
@body || hash
end

def type
@type || :json
end

# Return the rack triplet for this response
#
# @return [Array]
def rack_triplet
Rack.json_triplet(body, headers: @headers, status: @status)
return Rack.json_triplet(body, headers: @headers, status: @status) if type == :json
return Rack.plain_triplet(body, headers: @headers, status: @status) if type == :plain

raise "Unknown response type '#{type}'"
end

end
Expand Down

0 comments on commit 5420ab0

Please sign in to comment.