Module: Etna::MultipartSerializableNestedHash

Included in:
Clients::Magma::UpdateRequest, Clients::Metis::UploadBlobRequest
Defined in:
lib/etna/multipart_serializable_nested_hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(cls) ⇒ Object



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|
          # This is necessary to ensure that arrays of hashes that have hetergenous keys still get parsed correctly
          # Since the only way to indicate a new entry in the array of hashes is by re-using a key that existed in
          # the previous hash.
          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

Instance Method Details

#encode_multipart_content(base_key = '') ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/etna/multipart_serializable_nested_hash.rb', line 41

def encode_multipart_content(base_key = '')
  value = self
  if value.respond_to? :as_json
    value = value.as_json
  end

  self.class.encode_multipart_content(value, base_key)
end