multipart_body 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- class Part < Struct.new(:name, :body, :filename, :content_type, :encoding)
1
+ class Part < Struct.new(:name, :body, :filename, :content_disposition, :content_type, :encoding)
2
2
  def initialize(*args)
3
3
  if args.flatten.first.is_a? Hash
4
4
  from_hash(args.flatten.first)
@@ -9,21 +9,29 @@ class Part < Struct.new(:name, :body, :filename, :content_type, :encoding)
9
9
 
10
10
  def from_hash(hash)
11
11
  hash.each_pair do |k, v|
12
- self[k] = v
12
+ if k.to_s == 'body' && (v.is_a?(File) || v.is_a?(Tempfile))
13
+ self[k] = v.read
14
+ self['filename'] = File.basename(v)
15
+ else
16
+ self[k] = v
17
+ end
13
18
  end
14
19
  end
15
20
 
16
21
  def from_args(name, body, filename=nil)
17
- self[:name] = name
18
- self[:body] = body
19
- self[:filename] = filename
22
+ self.from_hash(:name => name, :body => body, :filename => filename)
20
23
  end
21
24
 
22
25
  def header
23
- header = "Content-Disposition: form-data; name=\"#{name}\""
24
- header << "; filename=\"#{filename}\"" if filename
25
- header << "\r\nContent-Type: #{content_type}" if content_type
26
- header << "\r\nContent-Transfer-Encoding: #{encoding}" if encoding
26
+ header = ""
27
+ if content_disposition || name
28
+ header << "Content-Disposition: #{content_disposition || 'form-data'}"
29
+ header << "; name=\"#{name}\"" if name && !content_disposition
30
+ header << "; filename=\"#{filename}\"" if filename && !content_disposition
31
+ header << "\r\n"
32
+ end
33
+ header << "Content-Type: #{content_type}\r\n" if content_type
34
+ header << "Content-Transfer-Encoding: #{encoding}\r\n" if encoding
27
35
  header
28
36
  end
29
37
 
@@ -38,6 +46,6 @@ class Part < Struct.new(:name, :body, :filename, :content_type, :encoding)
38
46
  end
39
47
 
40
48
  def to_s
41
- "#{header}\r\n\r\n#{encoded_body}"
49
+ "#{header}\r\n#{encoded_body}"
42
50
  end
43
51
  end
data/test/file ADDED
@@ -0,0 +1 @@
1
+ hello
data/test/file.txt ADDED
@@ -0,0 +1 @@
1
+ hello
data/test/test.rb CHANGED
@@ -1,10 +1,12 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
+ require 'tempfile'
2
3
 
3
4
  class MultipartBodyTest < Test::Unit::TestCase
4
- context "MultipartBodyBody" do
5
+ context "MultipartBody" do
5
6
  setup do
6
7
  @hash = {:test => 'test', :two => 'two'}
7
8
  @parts = [Part.new('name', 'value'), Part.new('name2', 'value2')]
9
+ @example_text = "------multipart-boundary-307380\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\nvalue\r\n------multipart-boundary-307380\r\nContent-Disposition: form-data; name=\"name2\"\r\n\r\nvalue2\r\n------multipart-boundary-307380--"
8
10
  end
9
11
 
10
12
  should "return a new multipart when sent #from_hash" do
@@ -51,11 +53,21 @@ class MultipartBodyTest < Test::Unit::TestCase
51
53
  multipart = MultipartBody.new(@parts)
52
54
  assert_match multipart.parts.join("\r\n--#{multipart.boundary}\r\n"), multipart.to_s
53
55
  end
56
+
57
+ should "contrsuct a valid multipart text when passed #to_s" do
58
+ multipart = MultipartBody.new(@parts)
59
+ multipart.boundary = '----multipart-boundary-307380'
60
+ assert_equal @example_text, multipart.to_s
61
+ end
54
62
  end
55
63
 
56
64
  context "a Part" do
57
65
  setup do
58
66
  @part = Part
67
+ @file = Tempfile.new('file')
68
+ @file.write('hello')
69
+ @file.flush
70
+ @file.open
59
71
  end
60
72
 
61
73
  should "assign values when sent #new with a hash" do
@@ -86,7 +98,22 @@ class MultipartBodyTest < Test::Unit::TestCase
86
98
  assert_equal nil, part.filename
87
99
  end
88
100
 
89
- should "include a content disposition when sent #header name" do
101
+ should "include a content type when one is set" do
102
+ part = Part.new(:content_type => 'plain/text', :body => 'content')
103
+ assert_match "Content-Type: plain\/text\r\n", part.header
104
+ end
105
+
106
+ should "include a content disposition when sent #header and one is set" do
107
+ part = Part.new(:content_disposition => 'content-dispo', :body => 'content')
108
+ assert_match "Content-Disposition: content-dispo\r\n", part.header
109
+ end
110
+
111
+ should "not include a content disposition of form-data when nothing is set" do
112
+ part = Part.new(:body => 'content')
113
+ assert_no_match /content-disposition/i, part.header
114
+ end
115
+
116
+ should "include a content disposition when sent #header and name is set" do
90
117
  part = Part.new(:name => 'key', :body => 'content')
91
118
  assert_match /content-disposition: form-data; name="key"/i, part.header
92
119
  end
@@ -116,7 +143,17 @@ class MultipartBodyTest < Test::Unit::TestCase
116
143
 
117
144
  should "output the header and body when sent #to_s" do
118
145
  part = Part.new(:name => 'key', :body => 'content')
119
- assert_equal "#{part.header}\r\n\r\n#{part.body}", part.to_s
146
+ assert_equal "#{part.header}\r\n#{part.body}", part.to_s
147
+ end
148
+
149
+ should "add the files content not the file when passed a file" do
150
+ part = Part.new(:name => 'key', :body => @file)
151
+ assert_equal 'hello', part.body
152
+ end
153
+
154
+ should "automatically assign a filename when passed a file to body" do
155
+ part = Part.new(:name => 'key', :body => @file)
156
+ assert_not_nil part.filename
120
157
  end
121
158
  end
122
159
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multipart_body
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Steve Smith
@@ -15,12 +15,12 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-17 00:00:00 +01:00
18
+ date: 2010-09-19 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
22
22
  description: A ruby library to create multipart bodies.
23
- email: gems@scsworld.co.uk
23
+ email: gems@dynedge.co.uk
24
24
  executables: []
25
25
 
26
26
  extensions: []
@@ -32,6 +32,8 @@ files:
32
32
  - lib/multipart_body/multipart_body.rb
33
33
  - lib/multipart_body/part.rb
34
34
  - lib/multipart_body.rb
35
+ - test/file
36
+ - test/file.txt
35
37
  - test/test.rb
36
38
  - test/test_helper.rb
37
39
  has_rdoc: true
@@ -69,5 +71,7 @@ signing_key:
69
71
  specification_version: 3
70
72
  summary: MultipartBody allows you to create consistant multipart bodies
71
73
  test_files:
74
+ - test/file
75
+ - test/file.txt
72
76
  - test/test.rb
73
77
  - test/test_helper.rb