-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
381 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
class Reynard | ||
# Contains built-in (de)serializer classes. | ||
module Coders | ||
autoload :ApplicationJson, 'reynard/coders/application_json' | ||
autoload :MultipartFormData, 'reynard/coders/multipart_form_data' | ||
autoload :TextPlain, 'reynard/coders/text_plain' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
class Reynard | ||
module Coders | ||
# Generates a multi part form data request body. | ||
class ApplicationJson | ||
def initialize(data:) | ||
@data = data | ||
end | ||
|
||
def mime_type | ||
'application/json' | ||
end | ||
|
||
def headers | ||
{ 'Content-Type' => mime_type.to_s } | ||
end | ||
|
||
def body | ||
MultiJson.dump(@data) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rack/utils' | ||
|
||
class Reynard | ||
module Coders | ||
# Generates a multi part form data request body. | ||
class MultipartFormData | ||
# We use all lowercase and no punctiation to be nice to other implementation. | ||
ALPHABET = ('a'..'z').to_a + ('0'..'9').to_a | ||
MULTIPART_BOUNDARY = 0.upto(69).map { ALPHABET.sample }.join.freeze | ||
|
||
# Returns the multipart boundary generated for this request body. | ||
attr_reader :multipart_boundary | ||
|
||
def initialize(data:) | ||
@data = data | ||
end | ||
|
||
def mime_type | ||
'multipart/form-data' | ||
end | ||
|
||
def headers | ||
{ 'Content-Type' => %(#{mime_type}; boundary="#{MULTIPART_BOUNDARY}") } | ||
end | ||
|
||
def body | ||
body = StringIO.new | ||
@data.each do |name, value| | ||
body << "--#{MULTIPART_BOUNDARY}\r" | ||
case value | ||
when Tempfile, IO | ||
write_io_part(body, name, value) | ||
else | ||
write_part(body, name, value) | ||
end | ||
end | ||
body.string | ||
end | ||
|
||
def write_io_part(body, name, io) | ||
filename = self.class.filename(io) | ||
filename = Rack::Utils.escape_path(File.basename(filename)) if filename | ||
content_type = io.respond_to?(:content_type) ? io.content_type : nil | ||
content_length = File.stat(io).size | ||
content_attributes = { 'name' => name, 'filename' => filename } | ||
|
||
body << %(Content-Disposition: #{join('form-data', *serialize(content_attributes))}\r) | ||
body << %(Content-Type: #{content_type}\r) if content_type | ||
body << %(Content-Length: #{content_length}\r) if content_length | ||
body << %(\r) | ||
IO.copy_stream(io, body) | ||
body << %(\r) | ||
end | ||
|
||
def write_part(body, name, value) | ||
body << %(Content-Disposition: #{join('form-data', *serialize({ 'name' => name }))}\r) | ||
body << %(\r) | ||
body << value | ||
body << %(\r) | ||
end | ||
|
||
def join(*items) | ||
items.join('; ') | ||
end | ||
|
||
def serialize(attributes) | ||
attributes.compact.flat_map do |name, value| | ||
%(#{name}="#{quote(value)}") | ||
end | ||
end | ||
|
||
def quote(name) | ||
name.to_s.gsub('"', '\"') | ||
end | ||
|
||
def self.filename(io) | ||
# A lot of IO related types are low level and don't necessarily respond to +respond_to?+, so | ||
# we just yeet the method and hope it sticks. | ||
%i[original_filename filename path].each do |method| | ||
return io.public_send(method) | ||
rescue NoMethodError | ||
end | ||
nil | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
class Reynard | ||
module Coders | ||
# Generates a plain text request body. | ||
class TextPlain | ||
def initialize(data:) | ||
@data = data | ||
end | ||
|
||
def mime_type | ||
'text/plain' | ||
end | ||
|
||
def headers | ||
{ 'Content-Type' => mime_type.to_s } | ||
end | ||
|
||
def body | ||
@data.to_s | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class Reynard | ||
# Wraps the choice for a serializer request content type, and returns headers and a serializer | ||
# to generate a request body. | ||
SerializerSelection = Struct.new( | ||
:content_type, | ||
:serializer_class, | ||
keyword_init: true | ||
) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.