Class: Salesfly::Multipart

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

Instance Method Summary collapse

Instance Method Details

#encode(params, files) ⇒ Object



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
# File 'lib/salesfly/multipart.rb', line 8

def encode(params, files)
  boundary = self.get_random_string()
  lines = []
  
  params.each do |key, value|
    if value.kind_of?(Array)
      value.each do |v| 
        lines.push(StringParam.new(key, v))
      end  
    else  
      lines.push(StringParam.new(key, value))
    end  
  end

  files.each do |fn|
    lines.push(FileParam.new(fn))
  end

  content = lines.collect {|p| "--" + boundary + "\r\n" + p.to_multipart }.join("")  + "--" + boundary + "--"
  headers = { 
    "Content-Type" => "multipart/form-data; boundary=#{ boundary }",
    "Content-Length" => content.length.to_s
  }
  return content, headers
end

#get_random_string(length = 32) ⇒ Object



34
35
36
37
38
39
# File 'lib/salesfly/multipart.rb', line 34

def get_random_string(length=32)
  source=("a".."z").to_a + ("A".."Z").to_a + (0..9).to_a
  key=""
  length.times{ key += source[rand(source.size)].to_s }
  return key
end