httmultiparty_sf 0.3.14

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 54f0193a6c526bbbb92f68ff2b0eb804596900bc
4
+ data.tar.gz: 20c9a2857190351daaa342375953054b2a7c76e2
5
+ SHA512:
6
+ metadata.gz: 300dad012082b0d803046532af329e2bdb364beab6e36da18eb27e89ea92c0b57181ef38213a058ba85504ac708f3f1bcbe99cf9f31236046b04e4b25a0f8322
7
+ data.tar.gz: 9298f40555ad3cbf76f07f1ef35023efd6b2a6011dfef8f31b7ea99e2703f63ffa90ae7b2067f278bb7cd248e17e22b1e0944432e8b53f5b17f38f1182d5fd10
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ [![Build Status](https://travis-ci.org/jwagener/httmultiparty.svg?branch=v0.3.14)](https://travis-ci.org/jwagener/httmultiparty)
2
+
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/httmultiparty.svg)](http://badge.fury.io/rb/httmultiparty)
5
+
6
+
7
+ ## Description
8
+
9
+ HTTMultiParty is a thin wrapper around HTTParty to provide multipart uploads.
10
+
11
+ ## Requirements
12
+
13
+ - httparty
14
+ - multipart-post
15
+ - mimemagic
16
+
17
+ ## Quick Start and Example
18
+
19
+ To start just "include HTTMultiParty" instead of "include HTTParty" into
20
+ your client class. When you pass a query with an instance of a File as a value for a PUT or POST request, the wrapper will use a bit of magic 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
+ You can use any class that responds to a `read` method. This method should act similar to the `IO#read` method. To set the filename your file class can optionally respond to the `original_filename` method, which should return a `String`.
@@ -0,0 +1,134 @@
1
+ gem 'httparty'
2
+ gem 'multipart-post'
3
+ require 'tempfile'
4
+ require 'httparty'
5
+ require 'net/http/post/multipart'
6
+ require 'mimemagic'
7
+
8
+ module HTTMultiParty
9
+ def self.included(base)
10
+ base.send :include, HTTParty
11
+ base.extend ClassMethods
12
+ end
13
+
14
+ def self.file_to_upload_io(file, detect_mime_type = false)
15
+ if file.respond_to? :original_filename
16
+ filename = file.original_filename
17
+ else
18
+ filename = File.split(file.path).last
19
+ end
20
+ content_type = detect_mime_type ? MimeMagic.by_path(filename) : 'application/octet-stream'
21
+ UploadIO.new(file, content_type, filename)
22
+ end
23
+
24
+ def self.query_string_normalizer(options = {})
25
+ detect_mime_type = options.fetch(:detect_mime_type, false)
26
+ Proc.new do |params|
27
+ if file_present_in_params?(params)
28
+ HTTMultiParty.flatten_params(params).map do |(k,v)|
29
+ [k, v.respond_to?(:read) ? HTTMultiParty.file_to_upload_io(v, detect_mime_type) : v]
30
+ end
31
+ else
32
+ HTTParty::Request::NON_RAILS_QUERY_STRING_NORMALIZER.call(params)
33
+ end
34
+ end
35
+ end
36
+
37
+ def self.flatten_params(params={}, prefix='')
38
+ flattened = []
39
+ params.each do |(k,v)|
40
+ if params.is_a?(Array)
41
+ v = k
42
+ k = ""
43
+ end
44
+
45
+ flattened_key = prefix == "" ? "#{k}" : "#{prefix}[#{k}]"
46
+ if v.is_a?(Hash) || v.is_a?(Array)
47
+ flattened += flatten_params(v, flattened_key)
48
+ else
49
+ flattened << [flattened_key, v]
50
+ end
51
+ end
52
+ flattened
53
+ end
54
+
55
+ def self.get(*args)
56
+ Basement.get(*args)
57
+ end
58
+
59
+ def self.post(*args)
60
+ Basement.post(*args)
61
+ end
62
+
63
+ def self.put(*args)
64
+ Basement.put(*args)
65
+ end
66
+
67
+ def self.delete(*args)
68
+ Basement.delete(*args)
69
+ end
70
+
71
+ def self.head(*args)
72
+ Basement.head(*args)
73
+ end
74
+
75
+ def self.options(*args)
76
+ Basement.options(*args)
77
+ end
78
+
79
+ private
80
+
81
+ def self.file_present_in_params?(params)
82
+ params.values.any? do |v|
83
+ if v.is_a? Array
84
+ v.any? { |vv| file_present?(vv) }
85
+ elsif v.is_a? Hash
86
+ v.values.any? { |vv| file_present?(vv) }
87
+ else
88
+ file_present?(v)
89
+ end
90
+ end
91
+ end
92
+
93
+ def self.file_present?(value)
94
+ value.respond_to?(:read)
95
+ end
96
+
97
+ module ClassMethods
98
+ def post(path, options={})
99
+ method = Net::HTTP::Post
100
+ options[:body] ||= options.delete(:query)
101
+ if hash_contains_files?(options[:body])
102
+ method = MultipartPost
103
+ options[:query_string_normalizer] = HTTMultiParty.query_string_normalizer(options)
104
+ end
105
+ perform_request method, path, options
106
+ end
107
+
108
+ def put(path, options={})
109
+ method = Net::HTTP::Put
110
+ options[:body] ||= options.delete(:query)
111
+ if hash_contains_files?(options[:body])
112
+ method = MultipartPut
113
+ options[:query_string_normalizer] = HTTMultiParty.query_string_normalizer(options)
114
+ end
115
+ perform_request method, path, options
116
+ end
117
+
118
+ private
119
+ def hash_contains_files?(hash)
120
+ hash.is_a?(Hash) && HTTMultiParty.flatten_params(hash).select do |_,v|
121
+ HTTMultiParty.file_present?(v)
122
+ end.size > 0
123
+ end
124
+ end
125
+
126
+ class Basement
127
+ include HTTMultiParty
128
+ end
129
+ end
130
+
131
+ require 'httmultiparty/version'
132
+ require 'httmultiparty/multipartable'
133
+ require 'httmultiparty/multipart_post'
134
+ 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 = 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 @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.14'
3
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: httmultiparty_sf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.14
5
+ platform: ruby
6
+ authors:
7
+ - Johannes Wagener
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-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
+ - lib/httmultiparty/multipart_post.rb
106
+ - lib/httmultiparty/multipart_put.rb
107
+ - lib/httmultiparty/multipartable.rb
108
+ - lib/httmultiparty/version.rb
109
+ - lib/httmultiparty.rb
110
+ - README.md
111
+ homepage: http://github.com/jwagener/httmultiparty
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: 1.3.6
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.1.11
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: HTTMultiParty is a thin wrapper around HTTParty to provide multipart uploads.
135
+ test_files: []