rack-multipart_related 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3cf1427a6afbd52d0afc85982e1db8d496e7582d
4
+ data.tar.gz: e0c6a2fba01f073d2081635d5e7a86942fc4f4c3
5
+ SHA512:
6
+ metadata.gz: 2dfbb39f9c1f2558c3373667584957c562c345d82d4b7f940afaf232b0fef389f5a38287ca36e81d74056e2cd04625fa890693a9c92383d630e01741aae3474d
7
+ data.tar.gz: cbeb9237a57029ac2c28abb465b565a4d988b1b94853ace760100b1d2401edcfbaf2b54f8db0f5ed41693a624f9035e5035cce5ce57de2339492bbbcf945655e
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://www.rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,25 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rack-multipart_related (1.0.0)
5
+ json (>= 1.0)
6
+ rack (>= 1.0)
7
+
8
+ GEM
9
+ remote: http://www.rubygems.org/
10
+ specs:
11
+ json (1.8.1)
12
+ metaclass (0.0.4)
13
+ mocha (1.0.0)
14
+ metaclass (~> 0.0.1)
15
+ rack (1.5.2)
16
+ rack-test (0.6.2)
17
+ rack (>= 1.0)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ mocha
24
+ rack-multipart_related!
25
+ rack-test
data/README.md CHANGED
@@ -6,69 +6,78 @@ Rack::MultipartRelated it's a **rack middleware** to parse **multipart/related**
6
6
 
7
7
  When a ruby webserver receives a multipart/related request:
8
8
 
9
- POST /users HTTP/1.1
10
- Content-Type: multipart/related; boundary="the_boundary"; type="application/json"; start="json"
11
-
12
- --the_boundary
13
- Content-Type: application/json; charset=UTF-8
14
- Content-Disposition: inline; name="json"\r
15
-
16
- {"user":{"name": "Jhon", "avatar": "cid:avatar_image" }}
17
- --the_boundary
18
- Content-Type: image/png
19
- Content-Disposition: inline; name="avatar_image"; filename="avatar.png"
20
-
21
- <the binary content of image comes here>
22
- --the_boundary--
9
+ ```bash
10
+ POST /users HTTP/1.1
11
+ Content-Type: multipart/related; boundary="the_boundary"; type="application/json"; start="json"
12
+
13
+ --the_boundary
14
+ Content-Type: application/json; charset=UTF-8
15
+ Content-Disposition: inline; name="json"\r
23
16
 
17
+ {"user":{"name": "Jhon", "avatar": "cid:avatar_image" }}
18
+ --the_boundary
19
+ Content-Type: image/png
20
+ Content-Disposition: inline; name="avatar_image"; filename="avatar.png"
24
21
 
22
+ <the binary content of image comes here>
23
+ --the_boundary--
24
+ ```
25
25
 
26
26
  The parameters hash arrives like this:
27
27
 
28
- {
29
- "json" => {
30
- :type => "application/json; charset=UTF-8",
31
- :tempfile => <File:/var/folders/Iu/IuwHUNlZE8OaYMACfwiapE+++TI/-Tmp-/RackMultipart20101217-30578-l17vkd-0>, # The json content
32
- :head => "Content-Type: application/json; charset=UTF-8\r\nContent-Disposition: inline; name=\"json\"\r\n",
33
- :name => "json"
34
- },
35
- "avatar_image" => {
36
- :type => "image/png",
37
- :filename =>"image.png",
38
- :tempfile => <File:/var/folders/Iu/IuwHUNlZE8OaYMACfwiapE+++TI/-Tmp-/RackMultipart20101217-30578-bt18q9-0>, # The binary content of image
39
- :head => "Content-Type: image/gif\r\nContent-Disposition: inline; name=\"avatar_image\"; filename=\"image.png\"\r\n",
40
- :name =>"avatar_image"
41
- }
42
- }
28
+ ```ruby
29
+ {
30
+ "json" => {
31
+ :type => "application/json; charset=UTF-8",
32
+ :tempfile => <File:/var/folders/Iu/IuwHUNlZE8OaYMACfwiapE+++TI/-Tmp-/RackMultipart20101217-30578-l17vkd-0>, # The json content
33
+ :head => "Content-Type: application/json; charset=UTF-8\r\nContent-Disposition: inline; name=\"json\"\r\n",
34
+ :name => "json"
35
+ },
36
+ "avatar_image" => {
37
+ :type => "image/png",
38
+ :filename =>"image.png",
39
+ :tempfile => <File:/var/folders/Iu/IuwHUNlZE8OaYMACfwiapE+++TI/-Tmp-/RackMultipart20101217-30578-bt18q9-0>, # The binary content of image
40
+ :head => "Content-Type: image/gif\r\nContent-Disposition: inline; name=\"avatar_image\"; filename=\"image.png\"\r\n",
41
+ :name =>"avatar_image"
42
+ }
43
+ }
44
+ ```
43
45
 
44
46
  Pay attention that the second part of the 'request' (in our example, "avatar\_image") needs to be referenced in the first part (in the example, "json") as "cid:REFERENCE\_NAME\_OF\_THE\_SECOND\_PART"
45
47
 
46
48
  Using this middleware, the hash above is parsed and rebuilt like the code below:
47
49
 
48
- {
49
- "user" => {
50
- "name" => "Jhon",
51
- "avatar" => {
52
- :type => "image/png",
53
- :filename =>"image.png",
54
- :tempfile => <File:/var/folders/Iu/IuwHUNlZE8OaYMACfwiapE+++TI/-Tmp-/RackMultipart20101217-30578-bt18q9-0>, # The binary content of image
55
- :head => "Content-Type: image/gif\r\nContent-Disposition: inline; name=\"avatar_image\"; filename=\"image.png\"\r\n",
56
- :name =>"avatar_image"
57
- }
58
- }
50
+ ```ruby
51
+ {
52
+ "user" => {
53
+ "name" => "Jhon",
54
+ "avatar" => {
55
+ :type => "image/png",
56
+ :filename =>"image.png",
57
+ :tempfile => <File:/var/folders/Iu/IuwHUNlZE8OaYMACfwiapE+++TI/-Tmp-/RackMultipart20101217-30578-bt18q9-0>, # The binary content of image
58
+ :head => "Content-Type: image/gif\r\nContent-Disposition: inline; name=\"avatar_image\"; filename=\"image.png\"\r\n",
59
+ :name =>"avatar_image"
59
60
  }
61
+ }
62
+ }
63
+ ```
60
64
 
61
65
  ## Usage
62
66
 
63
67
  ### Rails apps
64
68
 
65
69
  In your Gemfile:
66
-
67
- gem 'rack-multipart_related'
70
+
71
+ ```bash
72
+ gem 'rack-multipart_related'
73
+ ```
74
+
68
75
  In your environment.rb:
69
76
 
70
- require 'rack/multipart_related'
71
- config.middleware.use Rack::MultipartRelated
77
+ ```ruby
78
+ require 'rack/multipart_related'
79
+ config.middleware.use Rack::MultipartRelated
80
+ ```
72
81
 
73
82
  ### Non-Rails apps
74
83
 
@@ -20,9 +20,13 @@ module Rack
20
20
 
21
21
  params = env["rack.request.form_hash"]
22
22
  start_part_attribute = get_attribute(params, start_part)
23
- json_data = ::JSON.parse(start_part_attribute[:tempfile].read)
24
- start_part_attribute[:tempfile].rewind
25
- env['START_CONTENT_TYPE'] ||= start_part_attribute[:type]
23
+ if start_part_attribute.is_a? String then
24
+ json_data = ::JSON.parse(start_part_attribute)
25
+ else
26
+ json_data = ::JSON.parse(start_part_attribute[:tempfile].read)
27
+ start_part_attribute[:tempfile].rewind
28
+ env['START_CONTENT_TYPE'] ||= start_part_attribute[:type]
29
+ end
26
30
 
27
31
  new_params = handle_atributtes_with_part_refs(json_data, params)
28
32
  env["rack.request.form_hash"] = new_params
@@ -1,12 +1,7 @@
1
1
  module Rack
2
2
  class MultipartRelated
3
- version = nil
4
- version = $1 if ::File.expand_path('../../..', __FILE__) =~ /\/rack-multipart_related-([\w\.\-]+)/
5
- if version.nil? && ::File.exists?(::File.expand_path('../../../../.git', __FILE__))
6
- require "step-up"
7
- version = StepUp::Driver::Git.last_version
8
- end
9
- version = "0.0.0" if version.nil?
10
- VERSION = version.gsub(/^v?([^\+]+)\+?\d*$/, '\1')
3
+
4
+ VERSION = "1.0.0"
5
+
11
6
  end
12
7
  end
@@ -73,6 +73,44 @@ class MultipartRelatedTest < Test::Unit::TestCase
73
73
  assert last_response.ok?
74
74
  assert_equal last_request.env["rack.request.form_hash"], expected_request_form_hash_after_middleware
75
75
  end
76
+
77
+ def test_multipart_related_with_json_notin_tempfile
78
+ request_form_hash = {
79
+ "json" => '{"user": {"name": "Jhon", "avatar": "cid:avatar_image"} }',
80
+ "avatar_image" => {
81
+ :type => "image/png",
82
+ :filename =>"image.png",
83
+ :tempfile => imagefile,
84
+ :head => "Content-Type: image/gif\r\nContent-Disposition: inline; name=\"avatar_image\"; filename=\"image.png\"\r\n",
85
+ :name =>"avatar_image"
86
+ }
87
+ }
88
+
89
+ expected_request_form_hash_after_middleware = {
90
+ "user" => {
91
+ "name" => "Jhon",
92
+ "avatar" => {
93
+ :type => "image/png",
94
+ :filename =>"image.png",
95
+ :tempfile => imagefile,
96
+ :head => "Content-Type: image/gif\r\nContent-Disposition: inline; name=\"avatar_image\"; filename=\"image.png\"\r\n",
97
+ :name =>"avatar_image"
98
+ }
99
+ }
100
+ }
101
+
102
+ env = {
103
+ 'REQUEST_METHOD' => 'POST',
104
+ 'CONTENT_TYPE' => 'multipart/related; boundary="the_boundary"; type=application/json; start=json',
105
+ 'PATH_INFO' => '/some/path',
106
+ 'rack.request.form_hash' => request_form_hash
107
+ }
108
+
109
+ request(env['PATH_INFO'], env)
110
+
111
+ assert last_response.ok?
112
+ assert_equal last_request.env["rack.request.form_hash"], expected_request_form_hash_after_middleware
113
+ end
76
114
 
77
115
  def test_multipart_related_with_start_and_type_without_quotes
78
116
  jsonfile = make_tempfile("json", '{"user": {"name": "Jhon", "avatar": "cid:avatar_image"} }')
metadata CHANGED
@@ -1,93 +1,83 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: rack-multipart_related
3
- version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 1
9
- - 0
10
- version: 0.1.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Lucas Fais
14
8
  - Marcelo Manzan
15
9
  autorequire:
16
10
  bindir: bin
17
11
  cert_chain: []
18
-
19
- date: 2010-12-30 00:00:00 -02:00
20
- default_executable:
21
- dependencies:
22
- - !ruby/object:Gem::Dependency
12
+ date: 2014-03-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
23
15
  name: rack
24
- prerelease: false
25
- requirement: &id001 !ruby/object:Gem::Requirement
26
- none: false
27
- requirements:
28
- - - ">="
29
- - !ruby/object:Gem::Version
30
- hash: 15
31
- segments:
32
- - 1
33
- - 0
34
- version: "1.0"
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '1.0'
35
21
  type: :runtime
36
- version_requirements: *id001
37
- - !ruby/object:Gem::Dependency
38
- name: json
39
22
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- hash: 15
46
- segments:
47
- - 1
48
- - 0
49
- version: "1.0"
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '1.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: json
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '1.0'
50
35
  type: :runtime
51
- version_requirements: *id002
52
- - !ruby/object:Gem::Dependency
53
- name: step-up
54
36
  prerelease: false
55
- requirement: &id003 !ruby/object:Gem::Requirement
56
- none: false
57
- requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- hash: 3
61
- segments:
62
- - 0
63
- version: "0"
64
- type: :development
65
- version_requirements: *id003
66
- - !ruby/object:Gem::Dependency
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '1.0'
42
+ - !ruby/object:Gem::Dependency
67
43
  name: mocha
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
68
50
  prerelease: false
69
- requirement: &id004 !ruby/object:Gem::Requirement
70
- none: false
71
- requirements:
72
- - - ">="
73
- - !ruby/object:Gem::Version
74
- hash: 3
75
- segments:
76
- - 0
77
- version: "0"
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rack-test
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
78
63
  type: :development
79
- version_requirements: *id004
80
- description: It's a rack middleware to parse multipart/related requests and rebuild a simple/merged parameters hash.
81
- email:
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description: It's a rack middleware to parse multipart/related requests and rebuild
71
+ a simple/merged parameters hash.
72
+ email:
82
73
  - lucasfais@gmail.com
83
74
  - manzan@gmail.com
84
75
  executables: []
85
-
86
76
  extensions: []
87
-
88
77
  extra_rdoc_files: []
89
-
90
- files:
78
+ files:
79
+ - Gemfile
80
+ - Gemfile.lock
91
81
  - README.md
92
82
  - Rakefile
93
83
  - lib/rack/multipart_related.rb
@@ -95,43 +85,30 @@ files:
95
85
  - test/rack/multipart_related_test.rb
96
86
  - test/resources/image.png
97
87
  - test/test_helper.rb
98
- has_rdoc: true
99
88
  homepage: https://github.com/lucasfais/rack-multipart_related
100
89
  licenses: []
101
-
90
+ metadata: {}
102
91
  post_install_message:
103
92
  rdoc_options: []
104
-
105
- require_paths:
93
+ require_paths:
106
94
  - lib
107
- required_ruby_version: !ruby/object:Gem::Requirement
108
- none: false
109
- requirements:
110
- - - ">="
111
- - !ruby/object:Gem::Version
112
- hash: 3
113
- segments:
114
- - 0
115
- version: "0"
116
- required_rubygems_version: !ruby/object:Gem::Requirement
117
- none: false
118
- requirements:
119
- - - ">="
120
- - !ruby/object:Gem::Version
121
- hash: 23
122
- segments:
123
- - 1
124
- - 3
125
- - 6
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
126
104
  version: 1.3.6
127
105
  requirements: []
128
-
129
106
  rubyforge_project: rack-multipart_related
130
- rubygems_version: 1.3.7
107
+ rubygems_version: 2.0.3
131
108
  signing_key:
132
- specification_version: 3
109
+ specification_version: 4
133
110
  summary: Makes easy to handle mutipart/related requests.
134
- test_files:
111
+ test_files:
135
112
  - test/rack/multipart_related_test.rb
136
113
  - test/resources/image.png
137
114
  - test/test_helper.rb