httmultiparty_temp 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
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,114 @@
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
+ @base = base
20
+ end
21
+
22
+ def self.file_to_upload_io(file)
23
+ if file.respond_to? :original_filename
24
+ filename = file.original_filename
25
+ else
26
+ filename = File.split(file.path).last
27
+ end
28
+ content_type = @base.upload_file_content_type()
29
+ content_type = 'application/octet-stream' if content_type.blank?
30
+ UploadIO.new(file, content_type, filename)
31
+ end
32
+
33
+ def self.flatten_params(params={}, prefix='')
34
+ flattened = []
35
+ params.each do |(k,v)|
36
+ if params.is_a?(Array)
37
+ v = k
38
+ k = ""
39
+ end
40
+
41
+ flattened_key = prefix == "" ? "#{k}" : "#{prefix}[#{k}]"
42
+ if v.is_a?(Hash) || v.is_a?(Array)
43
+ flattened += flatten_params(v, flattened_key)
44
+ else
45
+ flattened << [flattened_key, v]
46
+ end
47
+ end
48
+ flattened
49
+ end
50
+
51
+ def self.get(*args)
52
+ Basement.get(*args)
53
+ end
54
+
55
+ def self.post(*args)
56
+ Basement.post(*args)
57
+ end
58
+
59
+ def self.put(*args)
60
+ Basement.put(*args)
61
+ end
62
+
63
+ def self.delete(*args)
64
+ Basement.delete(*args)
65
+ end
66
+
67
+ def self.head(*args)
68
+ Basement.head(*args)
69
+ end
70
+
71
+ def self.options(*args)
72
+ Basement.options(*args)
73
+ end
74
+
75
+ module ClassMethods
76
+ def post(path, options={})
77
+ method = Net::HTTP::Post
78
+ options[:body] ||= options.delete(:query)
79
+ if hash_contains_files?(options[:body])
80
+ method = MultipartPost
81
+ options[:query_string_normalizer] = HTTMultiParty::QUERY_STRING_NORMALIZER
82
+ end
83
+ perform_request method, path, options
84
+ end
85
+
86
+ def put(path, options={})
87
+ method = Net::HTTP::Put
88
+ options[:body] ||= options.delete(:query)
89
+ if hash_contains_files?(options[:body])
90
+ method = MultipartPut
91
+ options[:query_string_normalizer] = HTTMultiParty::QUERY_STRING_NORMALIZER
92
+ end
93
+ perform_request method, path, options
94
+ end
95
+
96
+ mattr_accessor :upload_file_content_type
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
+
106
+ class Basement
107
+ include HTTMultiParty
108
+ end
109
+ end
110
+
111
+ require 'httmultiparty/version'
112
+ require 'httmultiparty/multipartable'
113
+ require 'httmultiparty/multipart_post'
114
+ 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.7'
3
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: httmultiparty_temp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.7
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kedar Mhaswade
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-11 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: &17146340 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.7.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *17146340
25
+ - !ruby/object:Gem::Dependency
26
+ name: multipart-post
27
+ requirement: &17145960 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *17145960
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &17145500 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *17145500
47
+ - !ruby/object:Gem::Dependency
48
+ name: fakeweb
49
+ requirement: &17193840 !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: *17193840
58
+ description: HTTMultiParty is a thin wrapper around HTTParty to provide multipart
59
+ uploads.
60
+ email:
61
+ - kedar.mhaswade@gmail.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - lib/httmultiparty/multipartable.rb
67
+ - lib/httmultiparty/multipart_post.rb
68
+ - lib/httmultiparty/multipart_put.rb
69
+ - lib/httmultiparty/version.rb
70
+ - lib/httmultiparty.rb
71
+ - README.md
72
+ homepage: http://github.com/kedarmhaswade/httmultiparty
73
+ licenses: []
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: 1.3.6
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.6
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: HTTMultiParty is a thin wrapper around HTTParty to provide multipart uploads.
96
+ This version is available only to work around RubyMine problems -- I am not able
97
+ to see :git gems in external libs.
98
+ test_files: []