pkoch-httmultiparty 0.3.6a

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.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ <h2>Description</h2>
2
+ <p>HTTMultiParty is a thin wrapper around HTTParty to provide multipart uploads.</p>
3
+
4
+ <h2>Requirements</h2>
5
+ <ul>
6
+ <li>httparty</li>
7
+ <li>multipart-post</li>
8
+ </ul>
9
+
10
+ <h2>Quick Start and Example</h2>
11
+ <p>To start just "include HTTMultiParty" instead of "include HTTParty" into your client class.
12
+ When you pass a query with an instance of a File as a value for a PUT or POST request, the wrapper will
13
+ use a bit of magic and multipart-post to execute a multipart upload:</p>
14
+
15
+ <pre>
16
+ require 'httmultiparty'
17
+ class SomeClient
18
+ include HTTMultiParty
19
+ base_uri 'http://localhost:3000'
20
+ end
21
+
22
+ response = SomeClient.post('/', :query => {
23
+ :foo => 'bar',
24
+ :somefile => File.new('README.md')
25
+ })
26
+ </pre>
27
+
28
+ Aside from that it provides all the usual HTTParty gimmicks.
@@ -0,0 +1,110 @@
1
+ gem 'httparty'
2
+ gem 'multipart-post'
3
+ require 'tempfile'
4
+ require 'httparty'
5
+ require 'net/http/post/multipart'
6
+
7
+ module HTTMultiParty
8
+ TRANSFORMABLE_TYPES = [File, Tempfile]
9
+
10
+ QUERY_STRING_NORMALIZER = Proc.new do |params|
11
+ HTTMultiParty.flatten_params(params).map do |(k,v)|
12
+ [k, TRANSFORMABLE_TYPES.include?(v.class) ? HTTMultiParty.file_to_upload_io(v) : v]
13
+ end
14
+ end
15
+
16
+ def self.included(base)
17
+ base.send :include, HTTParty
18
+ base.extend ClassMethods
19
+ end
20
+
21
+ def self.file_to_upload_io(file)
22
+ if file.respond_to? :original_filename
23
+ filename = file.original_filename
24
+ else
25
+ filename = File.split(file.path).last
26
+ end
27
+ content_type = 'application/octet-stream'
28
+ UploadIO.new(file, content_type, filename)
29
+ end
30
+
31
+ def self.flatten_params(params={}, prefix='')
32
+ flattened = []
33
+ params.each do |(k,v)|
34
+ if params.is_a?(Array)
35
+ v = k
36
+ k = ""
37
+ end
38
+
39
+ flattened_key = prefix == "" ? "#{k}" : "#{prefix}[#{k}]"
40
+ if v.is_a?(Hash) || v.is_a?(Array)
41
+ flattened += flatten_params(v, flattened_key)
42
+ else
43
+ flattened << [flattened_key, v]
44
+ end
45
+ end
46
+ flattened
47
+ end
48
+
49
+ class Basement
50
+ include HTTMultiParty
51
+ end
52
+
53
+ def self.get(*args)
54
+ Basement.get(*args)
55
+ end
56
+
57
+ def self.post(*args)
58
+ Basement.post(*args)
59
+ end
60
+
61
+ def self.put(*args)
62
+ Basement.put(*args)
63
+ end
64
+
65
+ def self.delete(*args)
66
+ Basement.delete(*args)
67
+ end
68
+
69
+ def self.head(*args)
70
+ Basement.head(*args)
71
+ end
72
+
73
+ def self.options(*args)
74
+ Basement.options(*args)
75
+ end
76
+
77
+ module ClassMethods
78
+ def post(path, options={})
79
+ method = Net::HTTP::Post
80
+ options[:body] ||= options.delete(:query)
81
+ if hash_contains_files?(options[:body])
82
+ method = MultipartPost
83
+ options[:query_string_normalizer] = HTTMultiParty::QUERY_STRING_NORMALIZER
84
+ end
85
+ perform_request method, path, options
86
+ end
87
+
88
+ def put(path, options={})
89
+ method = Net::HTTP::Put
90
+ options[:body] ||= options.delete(:query)
91
+ if hash_contains_files?(options[:body])
92
+ method = MultipartPut
93
+ options[:query_string_normalizer] = HTTMultiParty::QUERY_STRING_NORMALIZER
94
+ end
95
+ perform_request method, path, options
96
+ end
97
+
98
+ private
99
+ def hash_contains_files?(hash)
100
+ hash.is_a?(Hash) && HTTMultiParty.flatten_params(hash).select do |(k,v)|
101
+ TRANSFORMABLE_TYPES.include?(v.class) || v.is_a?(UploadIO)
102
+ end.size > 0
103
+ end
104
+ end
105
+ end
106
+
107
+ require 'httmultiparty/version'
108
+ require 'httmultiparty/multipartable'
109
+ require 'httmultiparty/multipart_post'
110
+ require 'httmultiparty/multipart_put'
@@ -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 = 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 @body_parts
23
+ self.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 { |part| part.to_io })
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module HTTMultiParty
2
+ VERSION = '0.3.6a'
3
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pkoch-httmultiparty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.6a
5
+ prerelease: 5
6
+ platform: ruby
7
+ authors:
8
+ - Paulo Köch"
9
+ - Johannes Wagener
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-04-04 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 0.7.3
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: 0.7.3
31
+ - !ruby/object:Gem::Dependency
32
+ name: multipart-post
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: fakeweb
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ description: HTTMultiParty is a thin wrapper around HTTParty to provide multipart
80
+ uploads.
81
+ email:
82
+ - paulo.koch@gmail.com
83
+ - johannes@wagener.cc
84
+ executables: []
85
+ extensions: []
86
+ extra_rdoc_files: []
87
+ files:
88
+ - lib/httmultiparty/multipart_post.rb
89
+ - lib/httmultiparty/multipart_put.rb
90
+ - lib/httmultiparty/multipartable.rb
91
+ - lib/httmultiparty/version.rb
92
+ - lib/httmultiparty.rb
93
+ - README.md
94
+ homepage: http://github.com/pkoch/httmultiparty
95
+ licenses: []
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: 1.3.6
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 1.8.21
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: HTTMultiParty is a thin wrapper around HTTParty to provide multipart uploads.
118
+ test_files: []