3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/etna/multipart_serializable_nested_hash.rb', line 3
def self.included(cls)
cls.instance_eval do
def self.encode_multipart_pairs(value, base_key, is_root, &block)
if value.is_a? Hash
value.each do |k, v|
encode_multipart_pairs(v, is_root ? k : "#{base_key}[#{k}]", false, &block)
end
elsif value.is_a? Array
value.each_with_index do |v, i|
if v.is_a? Hash
encode_multipart_pairs(i, "#{base_key}[][_idx]", false, &block)
end
encode_multipart_pairs(v, "#{base_key}[]", false, &block)
end
else
raise "base_key cannot be empty for a scalar value!" if base_key.length == 0
if value.respond_to?(:read)
yield [base_key, UploadIO.new(value, 'application/octet-stream'), {filename: 'blob'}]
else
yield [base_key, value.to_s]
end
end
end
def self.encode_multipart_content(value, base_key = '', is_root = true)
result = []
self.encode_multipart_pairs(value, base_key, is_root) { |pair| result << pair }
result
end
end
end
|