httmultiparty 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,17 +4,48 @@ require 'httparty'
4
4
  require 'net/http/post/multipart'
5
5
 
6
6
  module HTTMultiParty
7
+ QUERY_STRING_NORMALIZER = Proc.new do |params|
8
+ HTTMultiParty.flatten_params(params).map do |(k,v)|
9
+ [k, v.is_a?(File) ? HTTMultiParty.file_to_upload_io(v) : v]
10
+ end
11
+ end
12
+
7
13
  def self.included(base)
8
14
  base.send :include, HTTParty
9
15
  base.extend ClassMethods
10
16
  end
11
17
 
18
+ def self.file_to_upload_io(file)
19
+ filename = File.split(file.path).last
20
+ content_type = 'application/octet-stream'
21
+ UploadIO.new(file, content_type, filename)
22
+ end
23
+
24
+ def self.flatten_params(params={}, prefix='')
25
+ flattened = []
26
+ params.each do |(k,v)|
27
+ if params.is_a?(Array)
28
+ v = k
29
+ k = ""
30
+ end
31
+
32
+ flattened_key = prefix == "" ? "#{k}" : "#{prefix}[#{k}]"
33
+ if v.is_a?(Hash) || v.is_a?(Array)
34
+ flattened += flatten_params(v, flattened_key)
35
+ else
36
+ flattened << [flattened_key, v]
37
+ end
38
+ end
39
+ flattened
40
+ end
41
+
12
42
  module ClassMethods
13
43
  def post(path, options={})
14
44
  method = Net::HTTP::Post
15
45
  if query_contains_files?(options[:query])
16
46
  method = MultipartPost
17
- options[:body] = map_files_to_upload_io(options.delete(:query))
47
+ options[:body] = options.delete(:query)
48
+ options[:query_string_normalizer] = HTTMultiParty::QUERY_STRING_NORMALIZER
18
49
  end
19
50
  perform_request method, path, options
20
51
  end
@@ -23,34 +54,20 @@ module HTTMultiParty
23
54
  method = Net::HTTP::Put
24
55
  if query_contains_files?(options[:query])
25
56
  method = MultipartPut
26
- options[:body] = map_files_to_upload_io(options.delete(:query))
57
+ options[:body] = options.delete(:query)
58
+ options[:query_string_normalizer] = HTTMultiParty::QUERY_STRING_NORMALIZER
27
59
  end
28
60
  perform_request method, path, options
29
61
  end
30
62
 
31
63
  private
32
64
  def query_contains_files?(query)
33
- query.is_a?(Hash) && query.select { |k,v| v.is_a?(File) }.length > 0
34
- end
35
-
36
- def map_files_to_upload_io(hash)
37
- new_special_hash = SpecialHash.new
38
- hash.each do |k,v|
39
- new_special_hash[k] = v.is_a?(File) ? file_to_upload_io(v) : v
40
- end
41
- new_special_hash
42
- end
43
-
44
- def file_to_upload_io(file)
45
- filename = File.split(file.path).last
46
- content_type = 'application/octet-stream'
47
- UploadIO.new(file, content_type, filename)
65
+ query.is_a?(Hash) && query.select { |k,v| v.is_a?(Hash) ? query_contains_files?(v) : v.is_a?(File) }.length > 0
48
66
  end
49
67
  end
50
68
  end
51
69
 
52
70
  dir = Pathname(__FILE__).dirname.expand_path
53
71
  require dir + 'httmultiparty/multipartable'
54
- require dir + 'httmultiparty/special_hash'
55
72
  require dir + 'httmultiparty/multipart_post'
56
73
  require dir + 'httmultiparty/multipart_put'
@@ -6,9 +6,8 @@ module HTTMultiParty::Multipartable
6
6
  end
7
7
 
8
8
  def body=(value)
9
- raise RuntimeError.new('body needs to be a SpecialHash') unless value.is_a?(HTTMultiParty::SpecialHash)
10
9
  boundary = DEFAULT_BOUNDARY
11
- parts = value.map {|k,v| Parts::Part.new(boundary, k, v)}
10
+ parts = value.map {|(k,v)| Parts::Part.new(boundary, k, v)}
12
11
  parts << Parts::EpiloguePart.new(boundary)
13
12
  self.set_content_type("multipart/form-data", { "boundary" => boundary })
14
13
  self.content_length = parts.inject(0) {|sum,i| sum + i.length }
@@ -1,3 +1,3 @@
1
1
  module HTTMultiParty
2
- VERSION = '0.1'
2
+ VERSION = '0.2'
3
3
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: httmultiparty
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- version: "0.1"
8
+ - 2
9
+ version: "0.2"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Johannes Wagener
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-16 00:00:00 -08:00
17
+ date: 2011-01-25 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -25,10 +25,12 @@ dependencies:
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
- hash: 3
28
+ hash: 5
29
29
  segments:
30
30
  - 0
31
- version: "0"
31
+ - 7
32
+ - 3
33
+ version: 0.7.3
32
34
  type: :runtime
33
35
  version_requirements: *id001
34
36
  - !ruby/object:Gem::Dependency
@@ -86,7 +88,6 @@ files:
86
88
  - lib/httmultiparty/multipart_post.rb
87
89
  - lib/httmultiparty/multipart_put.rb
88
90
  - lib/httmultiparty/multipartable.rb
89
- - lib/httmultiparty/special_hash.rb
90
91
  - lib/httmultiparty/version.rb
91
92
  - lib/httmultiparty.rb
92
93
  - README.md
@@ -1,8 +0,0 @@
1
- # SpecialHash is a hash that is pretending to be not a Hash.
2
- # its just a cheap way to get around the query normalization
3
- # happening in HTTParty::Request#body in lib/httparty/request.rb:118
4
- class HTTMultiParty::SpecialHash < Hash
5
- def is_a?(klass)
6
- klass == ::Hash ? false : super
7
- end
8
- end