httmultipartymagic 0.3.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bb7ff01b703fb033d358dac328c9087b6163e5cd
4
+ data.tar.gz: 662e5ad0e0956141106166d8ecba2b6d7d16d90f
5
+ SHA512:
6
+ metadata.gz: b8efc6c37e6bee5bb144f7ed7bf03808cb344ca1356227703609bf9e136c9fb20df5d3e3da6640f1fc122d7d28b1a7d0b62b69e44d16434bb63e4ff4312f11a5
7
+ data.tar.gz: f4f610fd034c8b5996d7e77c25220047db23fbdd0284c2f3d3d84ebf8b735c86f3abba8783b48d5795f679e3fec89437a5ebc3920e2b45dbb99854ecadf7515d
@@ -0,0 +1,63 @@
1
+ ## Forked
2
+
3
+ This version was forked in order to change `MimeMagic.by_path` to `MimeMagic.by_magic` when detecting an uploaded file's mime type. The latter was failing on tempfiles with no extensions but the latter correctly identifies the mime type for those.
4
+
5
+ ## Description
6
+
7
+ HTTMultiParty is a thin wrapper around HTTParty to provide multipart uploads.
8
+
9
+ ## Requirements
10
+
11
+ - httparty
12
+ - multipart-post
13
+ - mimemagic
14
+
15
+ ## Quick Start and Example
16
+
17
+ To start just `include HTTMultiParty` instead of `include HTTParty` into
18
+ your client class. When you pass a query with an instance of a File as
19
+ a value for a PUT or POST request, the wrapper will use a bit of magic
20
+ and multipart-post to execute a multipart upload:
21
+
22
+ ```ruby
23
+ require 'httmultiparty'
24
+ class SomeClient
25
+ include HTTMultiParty
26
+ base_uri 'http://localhost:3000'
27
+ end
28
+
29
+ response = SomeClient.post('/', :query => {
30
+ :foo => 'bar',
31
+ :somefile => File.new('README.md')
32
+ })
33
+ ```
34
+
35
+ Aside from that it provides all the usual HTTParty gimmicks.
36
+
37
+ ## MIME type support
38
+
39
+ If you want the library to detect the MIME types of the uploaded files, then
40
+ you need to enable it by supplying the `:detect_mime_type` option as `true`
41
+ for POST or PUT requests. Otherwise, they will be uploaded with the default
42
+ MIME type of `application/octet-stream`. For example:
43
+
44
+ ```ruby
45
+ require 'httmultiparty'
46
+ class SomeClient
47
+ include HTTMultiParty
48
+ base_uri 'http://localhost:3000'
49
+ end
50
+
51
+ response = SomeClient.post('/', :query => {
52
+ :foo => 'bar',
53
+ :somefile => File.new('README.md')
54
+ }, :detect_mime_type => true)
55
+ ```
56
+ ## File class support
57
+
58
+ Instead of using `File` class, you can use any class that responds to
59
+ a `read` method as a file object. If you are using Rails, you can use
60
+ `ActionDispatch::Http::UploadedFile` object directly as it responds to
61
+ `read` method. The `read` method should act similar to the `IO#read`
62
+ method. To set the filename your file class can optionally respond to
63
+ the `original_filename` method, which should return a `String`.
@@ -0,0 +1,167 @@
1
+ require 'tempfile'
2
+ require 'httparty'
3
+ require 'net/http/post/multipart'
4
+ require 'mimemagic'
5
+
6
+ module HTTMultiParty
7
+ def self.included(base)
8
+ base.send :include, HTTParty
9
+ base.extend ClassMethods
10
+ end
11
+
12
+ def self.file_to_upload_io(file, detect_mime_type = false)
13
+ if file.respond_to? :original_filename
14
+ filename = file.original_filename
15
+ else
16
+ filename = File.split(file.path).last
17
+ end
18
+ content_type = detect_mime_type ? MimeMagic.by_magic(file) : 'application/octet-stream'
19
+ UploadIO.new(file, content_type, filename)
20
+ end
21
+
22
+ def self.query_string_normalizer(options = {})
23
+ detect_mime_type = options.fetch(:detect_mime_type, false)
24
+ proc do |params|
25
+ HTTMultiParty.flatten_params(params).map do |(k, v)|
26
+ if file_present?(params)
27
+ v = prepare_value!(v, detect_mime_type)
28
+ [k, v]
29
+ else
30
+ "#{k}=#{v}"
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ def self.flatten_params(params = {}, prefix = '')
37
+ flattened = []
38
+ params.each do |(k, v)|
39
+ if params.is_a?(Array)
40
+ v = k
41
+ k = ''
42
+ end
43
+
44
+ flattened_key = prefix == '' ? "#{k}" : "#{prefix}[#{k}]"
45
+ if v.is_a?(Hash) || v.is_a?(Array)
46
+ flattened += flatten_params(v, flattened_key)
47
+ else
48
+ flattened << [flattened_key, v]
49
+ end
50
+ end
51
+ flattened
52
+ end
53
+
54
+ def self.prepare_value!(value, detect_mime_type)
55
+ return value if does_not_need_conversion?(value)
56
+ HTTMultiParty.file_to_upload_io(value, detect_mime_type)
57
+ end
58
+
59
+ def self.get(*args)
60
+ Basement.get(*args)
61
+ end
62
+
63
+ def self.post(*args)
64
+ Basement.post(*args)
65
+ end
66
+
67
+ def self.put(*args)
68
+ Basement.put(*args)
69
+ end
70
+
71
+ def self.patch(*args)
72
+ Basement.patch(*args)
73
+ end
74
+
75
+ def self.delete(*args)
76
+ Basement.delete(*args)
77
+ end
78
+
79
+ def self.head(*args)
80
+ Basement.head(*args)
81
+ end
82
+
83
+ def self.options(*args)
84
+ Basement.options(*args)
85
+ end
86
+
87
+ private
88
+
89
+ def self.file_present?(params)
90
+ if params.is_a? Array
91
+ file_present_in_array?(params)
92
+ elsif params.is_a? Hash
93
+ file_present_in_array?(params.values)
94
+ else
95
+ file?(params)
96
+ end
97
+ end
98
+
99
+ def self.file_present_in_array?(ary)
100
+ ary.any? { |a| file_present?(a) }
101
+ end
102
+
103
+ def self.file?(value)
104
+ value.respond_to?(:read)
105
+ end
106
+
107
+ def self.not_a_file?(value)
108
+ !file?(value)
109
+ end
110
+
111
+ def self.upload_io?(value)
112
+ value.is_a?(UploadIO)
113
+ end
114
+
115
+ def self.does_not_need_conversion?(value)
116
+ not_a_file?(value) ||
117
+ upload_io?(value)
118
+ end
119
+
120
+ module ClassMethods
121
+ def post(path, options = {})
122
+ method = Net::HTTP::Post
123
+ options[:body] ||= options.delete(:query)
124
+ if hash_contains_files?(options[:body])
125
+ method = MultipartPost
126
+ options[:query_string_normalizer] = HTTMultiParty.query_string_normalizer(options)
127
+ end
128
+ perform_request method, path, options
129
+ end
130
+
131
+ def put(path, options = {})
132
+ method = Net::HTTP::Put
133
+ options[:body] ||= options.delete(:query)
134
+ if hash_contains_files?(options[:body])
135
+ method = MultipartPut
136
+ options[:query_string_normalizer] = HTTMultiParty.query_string_normalizer(options)
137
+ end
138
+ perform_request method, path, options
139
+ end
140
+
141
+ def patch(path, options={})
142
+ method = Net::HTTP::Patch
143
+ options[:body] ||= options.delete(:query)
144
+ if hash_contains_files?(options[:body])
145
+ method = MultipartPatch
146
+ options[:query_string_normalizer] = HTTMultiParty.query_string_normalizer(options)
147
+ end
148
+ perform_request method, path, options
149
+ end
150
+
151
+ private
152
+
153
+ def hash_contains_files?(hash)
154
+ HTTMultiParty.file_present?(hash)
155
+ end
156
+ end
157
+
158
+ class Basement
159
+ include HTTMultiParty
160
+ end
161
+ end
162
+
163
+ require 'httmultiparty/version'
164
+ require 'httmultiparty/multipartable'
165
+ require 'httmultiparty/multipart_post'
166
+ require 'httmultiparty/multipart_put'
167
+ require 'httmultiparty/multipart_patch'
@@ -0,0 +1,5 @@
1
+ class HTTMultiParty::MultipartPatch < Net::HTTP::Patch
2
+ include HTTMultiParty::Multipartable
3
+ end
4
+
5
+ HTTParty::Request::SupportedHTTPMethods << HTTMultiParty::MultipartPatch
@@ -0,0 +1,5 @@
1
+ class HTTMultiParty::MultipartPost < Net::HTTP::Post
2
+ include HTTMultiParty::Multipartable
3
+ end
4
+
5
+ HTTParty::Request::SupportedHTTPMethods << HTTMultiParty::MultipartPost
@@ -0,0 +1,5 @@
1
+ class HTTMultiParty::MultipartPut < Net::HTTP::Put
2
+ include HTTMultiParty::Multipartable
3
+ end
4
+
5
+ HTTParty::Request::SupportedHTTPMethods << HTTMultiParty::MultipartPut
@@ -0,0 +1,28 @@
1
+ module HTTMultiParty::Multipartable
2
+ DEFAULT_BOUNDARY = '-----------RubyMultipartPost'
3
+ # prevent reinitialization of headers
4
+ def initialize_http_header(initheader)
5
+ super
6
+ set_headers_for_body
7
+ end
8
+
9
+ def body=(value)
10
+ @body_parts = Array(value).map { |(k, v)| Parts::Part.new(boundary, k, v) }
11
+ @body_parts << Parts::EpiloguePart.new(boundary)
12
+ set_headers_for_body
13
+ end
14
+
15
+ def boundary
16
+ DEFAULT_BOUNDARY
17
+ end
18
+
19
+ private
20
+
21
+ def set_headers_for_body
22
+ if defined?(@body_parts) && @body_parts
23
+ set_content_type('multipart/form-data', 'boundary' => boundary)
24
+ self.content_length = @body_parts.inject(0) { |sum, i| sum + i.length }
25
+ self.body_stream = CompositeReadIO.new(*@body_parts.map(&:to_io))
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module HTTMultiParty
2
+ VERSION = '0.3.17'
3
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: httmultipartymagic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.17
5
+ platform: ruby
6
+ authors:
7
+ - Johannes Wagener
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.7.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.7.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: multipart-post
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mimemagic
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: fakeweb
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: HTTMultiParty is a thin wrapper around HTTParty to provide multipart
98
+ uploads.
99
+ email:
100
+ - johannes@wagener.cc
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - README.md
106
+ - lib/httmultiparty.rb
107
+ - lib/httmultiparty/multipart_patch.rb
108
+ - lib/httmultiparty/multipart_post.rb
109
+ - lib/httmultiparty/multipart_put.rb
110
+ - lib/httmultiparty/multipartable.rb
111
+ - lib/httmultiparty/version.rb
112
+ homepage: http://github.com/jwagener/httmultiparty
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: 1.3.6
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.4.6
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: HTTMultiParty is a thin wrapper around HTTParty to provide multipart uploads.
136
+ test_files: []