multiparty 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +9 -0
- data/lib/multiparty.rb +9 -0
- data/spec/multiparty_spec.rb +16 -0
- data/spec/spec_helper.rb +0 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -12,6 +12,9 @@ You can add multiple values, corresponding to multiple <input> statements:
|
|
12
12
|
@multiparty = Multiparty.new
|
13
13
|
@multiparty[:name] = "David Verhasselt"
|
14
14
|
@multiparty[:state] = "awesome"
|
15
|
+
# or in one statement:
|
16
|
+
@multiparty << {:name => "David Verhasselt", :state => "awesome"}
|
17
|
+
|
15
18
|
@multiparty[:avatar] = {:filename => "avatar.jpg", :content => "...jpegdata..."}
|
16
19
|
|
17
20
|
# Retrieve the header and body like this:
|
@@ -64,6 +67,12 @@ tempfile.rewind
|
|
64
67
|
@multiparty[:message] => File.open(tempfile.path)
|
65
68
|
```
|
66
69
|
|
70
|
+
Multiparty has the ```to_s``` method aliased to ```body``` so you can use it as a ```String```:
|
71
|
+
|
72
|
+
```
|
73
|
+
puts "Hello World! My multipart body: #{@multiparty}"
|
74
|
+
```
|
75
|
+
|
67
76
|
Installation
|
68
77
|
------------
|
69
78
|
|
data/lib/multiparty.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "mime/types"
|
2
|
+
require "tempfile"
|
2
3
|
|
3
4
|
class Multiparty
|
4
5
|
attr_accessor :boundary
|
@@ -34,6 +35,7 @@ class Multiparty
|
|
34
35
|
|
35
36
|
result << "--"
|
36
37
|
end
|
38
|
+
alias_method :to_s, :body
|
37
39
|
|
38
40
|
def parse_part(name, value)
|
39
41
|
content_disposition = "form-data"
|
@@ -71,4 +73,11 @@ class Multiparty
|
|
71
73
|
end
|
72
74
|
alias_method :[]=, :add_part
|
73
75
|
|
76
|
+
def add_parts(parts)
|
77
|
+
parts.each do |index, value|
|
78
|
+
add_part(index, value)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
alias_method :<<, :add_parts
|
82
|
+
|
74
83
|
end
|
data/spec/multiparty_spec.rb
CHANGED
@@ -38,6 +38,22 @@ describe "multiparty" do
|
|
38
38
|
@multiparty[:key] = {:filename => "hello.jpg", :content => ""}
|
39
39
|
end
|
40
40
|
|
41
|
+
it "should have a decent #to_s method" do
|
42
|
+
@multiparty[:key] = :value
|
43
|
+
@multiparty.body.should == "#{@multiparty}"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should be able to add multiple parts at once" do
|
47
|
+
multiparty1 = Multiparty.new("my-boundary")
|
48
|
+
multiparty2 = Multiparty.new("my-boundary")
|
49
|
+
|
50
|
+
multiparty1 << {:key1 => :value1, :key2 => :value2}
|
51
|
+
multiparty2[:key1] = :value1
|
52
|
+
multiparty2[:key2] = :value2
|
53
|
+
|
54
|
+
multiparty1.body.should == multiparty2.body
|
55
|
+
end
|
56
|
+
|
41
57
|
it "should return a correctly formed multipart response" do
|
42
58
|
@multiparty.boundary = "AaB03x"
|
43
59
|
@multiparty['submit-name'] = "Larry"
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 2
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- David Verhasselt
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-09-
|
17
|
+
date: 2011-09-19 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|