marnen-typhoeus 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- marnen-typhoeus (0.3.4)
4
+ marnen-typhoeus (0.3.6)
5
5
  mime-types
6
6
 
7
7
  GEM
data/lib/typhoeus.rb CHANGED
@@ -9,6 +9,7 @@ require 'typhoeus/form'
9
9
  require 'typhoeus/multi'
10
10
  require 'typhoeus/native'
11
11
  require 'typhoeus/filter'
12
+ require 'typhoeus/param_processor'
12
13
  require 'typhoeus/remote_method'
13
14
  require 'typhoeus/remote'
14
15
  require 'typhoeus/remote_proxy_object'
@@ -0,0 +1,41 @@
1
+ module Typhoeus
2
+ class ParamProcessor
3
+ class << self
4
+ def traverse_params_hash(hash, result = nil, current_key = nil)
5
+ result ||= { :files => [], :params => [] }
6
+
7
+ hash.keys.sort { |a, b| a.to_s <=> b.to_s }.collect do |key|
8
+ new_key = (current_key ? "#{current_key}[#{key}]" : key).to_s
9
+ current_value = hash[key]
10
+ process_value current_value, :result => result, :new_key => new_key
11
+ end
12
+ result
13
+ end
14
+
15
+ def process_value(current_value, options)
16
+ result = options[:result]
17
+ new_key = options[:new_key]
18
+
19
+ case current_value
20
+ when Hash
21
+ traverse_params_hash(current_value, result, new_key)
22
+ when Array
23
+ current_value.each do |v|
24
+ result[:params] << [new_key, v.to_s]
25
+ end
26
+ when File, Tempfile
27
+ filename = File.basename(current_value.path)
28
+ types = MIME::Types.type_for(filename)
29
+ result[:files] << [
30
+ new_key,
31
+ filename,
32
+ types.empty? ? 'application/octet-stream' : types[0].to_s,
33
+ File.expand_path(current_value.path)
34
+ ]
35
+ else
36
+ result[:params] << [new_key, current_value.to_s]
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -12,14 +12,7 @@ module Typhoeus
12
12
 
13
13
  # Params are NOT escaped.
14
14
  def traverse_params_hash(hash, result = nil, current_key = nil)
15
- result ||= { :files => [], :params => [] }
16
-
17
- hash.keys.sort { |a, b| a.to_s <=> b.to_s }.collect do |key|
18
- new_key = (current_key ? "#{current_key}[#{key}]" : key).to_s
19
- current_value = hash[key]
20
- process_value current_value, :result => result, :new_key => new_key
21
- end
22
- result
15
+ result = ParamProcessor.traverse_params_hash hash, result, current_key
23
16
  end
24
17
  module_function :traverse_params_hash
25
18
 
@@ -1,3 +1,3 @@
1
1
  module Typhoeus
2
- VERSION = '0.3.5'
2
+ VERSION = '0.3.6'
3
3
  end
@@ -0,0 +1,85 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ module Typhoeus
4
+ describe ParamProcessor do
5
+ describe '.process_value' do
6
+ let(:result) { {:files => [], :params => []} }
7
+ let(:params) { result[:params] }
8
+
9
+ context 'simple values' do
10
+ it 'should write a key-value pair to result[:params]' do
11
+ ParamProcessor.process_value 'value', :result => result, :new_key => 'key'
12
+ params.should == [['key', 'value']]
13
+ end
14
+
15
+ it "should not overwrite what's already in result[:params]" do
16
+ result[:params] << ['old', '1']
17
+ ParamProcessor.process_value 2, :result => result, :new_key => 'new'
18
+ params.should == [['old', '1'], ['new', '2']]
19
+ end
20
+ end
21
+
22
+ context 'arrays' do
23
+ it 'should write a value for each array item' do
24
+ ParamProcessor.process_value [1, 'two', :'3'], :result => result, :new_key => 'array'
25
+ params.should == [['array', '1'], ['array', 'two'], ['array', '3']]
26
+ end
27
+ end
28
+
29
+ context 'files' do
30
+ let(:files) { result[:files] }
31
+
32
+ context 'regular files' do
33
+ let(:temp_directory) { Dir.mktmpdir }
34
+ let(:file) do
35
+ file = File.new File.join(temp_directory, 'testfile.txt'), 'w'
36
+ file.puts 'some text'
37
+ file
38
+ end
39
+
40
+ after :each do
41
+ file.close
42
+ FileUtils.remove_entry_secure temp_directory if temp_directory
43
+ end
44
+
45
+ it 'should write file information to result[:files]' do
46
+ ParamProcessor.process_value file, :result => result, :new_key => 'file'
47
+ path = file.path
48
+ files.should == [['file', File.basename(path), MIME::Types.type_for(path).first, path]]
49
+ end
50
+ end
51
+
52
+ context 'temporary files' do
53
+ let(:tempfile) do
54
+ tempfile = Tempfile.new(['foo', '.txt'])
55
+ tempfile.puts 'some text'
56
+ tempfile
57
+ end
58
+
59
+ after :each do
60
+ tempfile.close
61
+ tempfile.unlink
62
+ end
63
+
64
+ it 'should write file information to result[:files]' do
65
+ ParamProcessor.process_value tempfile, :result => result, :new_key => 'tempfile'
66
+ path = tempfile.path
67
+ files.should == [['tempfile', File.basename(path), MIME::Types.type_for(path).first, path]]
68
+ end
69
+ end
70
+ end
71
+
72
+ context 'hashes' do
73
+ it 'should nest values under the key' do
74
+ ParamProcessor.process_value({:one => 1, 'two' => 2}, :result => result, :new_key => 'key')
75
+ params.should == [['key[one]', '1'], ['key[two]', '2']]
76
+ end
77
+
78
+ it 'should nest values recursively' do
79
+ ParamProcessor.process_value({:array => [1, 2], :hash => {:key => 'value'}}, :result => result, :new_key => 'outer_key')
80
+ params.should == [['outer_key[array]', '1'], ['outer_key[array]', '2'], ['outer_key[hash][key]', 'value']]
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: marnen-typhoeus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.3.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -174,6 +174,7 @@ files:
174
174
  - lib/typhoeus/hydra_mock.rb
175
175
  - lib/typhoeus/multi.rb
176
176
  - lib/typhoeus/normalized_header_hash.rb
177
+ - lib/typhoeus/param_processor.rb
177
178
  - lib/typhoeus/remote.rb
178
179
  - lib/typhoeus/remote_method.rb
179
180
  - lib/typhoeus/remote_proxy_object.rb
@@ -196,6 +197,7 @@ files:
196
197
  - spec/typhoeus/hydra_spec.rb
197
198
  - spec/typhoeus/multi_spec.rb
198
199
  - spec/typhoeus/normalized_header_hash_spec.rb
200
+ - spec/typhoeus/param_processor_spec.rb
199
201
  - spec/typhoeus/remote_method_spec.rb
200
202
  - spec/typhoeus/remote_proxy_object_spec.rb
201
203
  - spec/typhoeus/remote_spec.rb