Class: Flexirest::Multipart::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/flexirest/multipart.rb

Constant Summary collapse

BOUNDARY =
"FLEXIRESTBOUNDARY-20190918-FLEXIRESTBOUNDARY"
CONTENT_TYPE =
"multipart/form-data; boundary=#{BOUNDARY}"
HEADER =
{"Content-Type" => CONTENT_TYPE}

Class Method Summary collapse

Class Method Details

.append_parameter(fp, key, value) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/flexirest/multipart.rb', line 31

def self.append_parameter(fp, key, value)
  if value.is_a?(Array)
    value.each do |i|
      append_parameter(fp, "#{key}[]", i)
    end
  elsif value.is_a?(Hash)
    value.stringify_keys.each do |k, i|
      append_parameter(fp, "#{key}[#{k}]", i)
    end
  elsif value.respond_to?(:path) and value.respond_to?(:read) then
    fp.push(FileParam.new(key, value.path, value.read))
  else
    fp.push(StringParam.new(key, value))
  end

end

.prepare_query(params) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/flexirest/multipart.rb', line 20

def self.prepare_query(params)
  fp = []

  params.stringify_keys.each do |k, v|
    append_parameter(fp, k, v)
  end

  query = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--"
  return query, HEADER
end