Skip to content

Commit 436655d

Browse files
committed
Update ruby input file handling
1 parent d2f2f07 commit 436655d

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

templates/ruby/docs/example.md.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ client
1212

1313
{{ service.name | caseSnake }} = {{spec.title | caseUcfirst}}::{{ service.name | caseUcfirst }}.new(client)
1414

15-
response = {{ service.name | caseSnake }}.{{ method.name | caseSnake }}({% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}, {% endif %}{{parameter.name|caseSnake}}: {% if parameter | paramExample == "File.new" %}{{spec.title | caseUcfirst}}::{{ parameter | paramExample }}{% else %}{{ parameter | paramExample }}{% endif %}{% endif %}{% endfor %})
15+
response = {{ service.name | caseSnake }}.{{ method.name | caseSnake }}({% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}, {% endif %}{{parameter.name|caseSnake}}: {% if parameter | paramExample == "InputFile.new" %}{{spec.title | caseUcfirst}}::{{ parameter | paramExample }}{% else %}{{ parameter | paramExample }}{% endif %}{% endif %}{% endfor %})
1616

1717
puts response.inspect

templates/ruby/lib/container/client.rb.twig

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,20 @@ module {{ spec.title | caseUcfirst }}
104104
on_progress: nil,
105105
response_type: nil
106106
)
107-
file_path = params[param_name.to_sym]
108-
size = ::File.size(file_path)
107+
input_file = params[param_name.to_sym]
108+
109+
case input_file.source_type
110+
when 'path'
111+
size = ::File.size(input_file.path)
112+
when 'bytes'
113+
size = input_file.data.bytesize
114+
end
109115

110116
if size < @chunk_size
111-
slice = ::File.read(file_path)
112-
params[param_name] = File.new(file_path, slice)
117+
if input_file.source_type == 'path'
118+
input_file.data = IO.read(input_file.path)
119+
end
120+
params[param_name] = input_file
113121
return call(
114122
method: 'POST',
115123
path: path,
@@ -133,10 +141,16 @@ module {{ spec.title | caseUcfirst }}
133141
offset = [size, (chunks_uploaded * @chunk_size)].min
134142
end
135143

136-
while offset < size
137-
slice = IO.read(file_path, @chunk_size, offset)
144+
cached_input = input_file
138145

139-
params[param_name] = File.new(file_path, slice)
146+
while offset < size
147+
case input_file.source_type
148+
when 'path'
149+
input_file.data = IO.read(input_file.path, @chunk_size, offset)
150+
when 'bytes'
151+
input_file.data = cached_input.data.slice(offset, @chunk_size)
152+
end
153+
params[param_name] = input_file
140154
headers['content-range'] = "bytes #{offset}-#{[offset + @chunk_size - 1, size].min}/#{size}"
141155

142156
result = call(
@@ -250,10 +264,10 @@ module {{ spec.title | caseUcfirst }}
250264
''
251265
else
252266
post_body = []
253-
if value.instance_of? File
254-
post_body << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{value.name}\"\r\n"
267+
if value.instance_of? InputFile
268+
post_body << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{value.filename}\"\r\n"
255269
post_body << "Content-Type: #{value.mime_type}\r\n\r\n"
256-
post_body << value.content
270+
post_body << value.data
257271
post_body << "\r\n--#{@boundary}--\r\n"
258272
else
259273
post_body << "Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n"

0 commit comments

Comments
 (0)