multipart-post 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.txt CHANGED
@@ -12,7 +12,7 @@ supports other methods besides POST.
12
12
  * Appears to actually work. A good feature to have.
13
13
  * Encapsulates posting of file/binary parts and name/value parameter parts, similar to
14
14
  most browsers' file upload forms.
15
- * Provides an UploadIO helper module to prepare IO objects for inclusion in the params
15
+ * Provides an UploadIO helper class to prepare IO objects for inclusion in the params
16
16
  hash of the multipart post object.
17
17
 
18
18
  == SYNOPSIS:
@@ -40,7 +40,7 @@ gem install multipart-post
40
40
 
41
41
  (The MIT License)
42
42
 
43
- Copyright (c) 2007-2010 Nick Sieger <nick@nicksieger.com>
43
+ Copyright (c) 2007-2011 Nick Sieger <nick@nicksieger.com>
44
44
 
45
45
  Permission is hereby granted, free of charge, to any person obtaining
46
46
  a copy of this software and associated documentation files (the
data/Rakefile CHANGED
@@ -1,7 +1,8 @@
1
+ require 'bundler/setup'
2
+
1
3
  begin
2
- require 'rubygems'
3
4
  require 'hoe'
4
- require 'lib/multipart_post'
5
+ require 'multipart_post'
5
6
 
6
7
  Hoe.plugin :gemcutter
7
8
  hoe = Hoe.spec("multipart-post") do |p|
@@ -13,12 +14,12 @@ begin
13
14
  p.description = "Use with Net::HTTP to do multipart form posts. IO values that have #content_type, #original_filename, and #local_path will be posted as a binary file."
14
15
  p.summary = "Creates a multipart form post accessory for Net::HTTP."
15
16
  end
16
- hoe.spec.dependencies.delete_if { |dep| dep.name == "hoe" }
17
+ hoe.spec.dependencies.delete_if { |dep| dep.name == "hoe" || dep.name == "rubyforge" }
17
18
 
18
19
  task :gemspec do
19
20
  File.open("#{hoe.name}.gemspec", "w") {|f| f << hoe.spec.to_ruby }
20
21
  end
21
22
  task :package => :gemspec
22
- rescue LoadError
23
+ rescue LoadError
23
24
  puts "You really need Hoe installed to be able to package this gem"
24
25
  end
data/lib/composite_io.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #--
2
- # (c) Copyright 2007-2008 Nick Sieger.
2
+ # (c) Copyright 2007-2011 Nick Sieger.
3
3
  # See the file README.txt included with the distribution for
4
4
  # software license details.
5
5
  #++
@@ -32,6 +32,7 @@ class CompositeReadIO
32
32
  @ios.shift
33
33
  end
34
34
 
35
+ result.force_encoding("BINARY") if result.respond_to?(:force_encoding)
35
36
  buffer << result if result
36
37
  partial_amount -= result.length if partial_amount && result != done
37
38
 
@@ -48,7 +49,7 @@ class CompositeReadIO
48
49
  end
49
50
 
50
51
  # Convenience methods for dealing with files and IO that are to be uploaded.
51
- module UploadIO
52
+ class UploadIO
52
53
  # Create an upload IO suitable for including in the params hash of a
53
54
  # Net::HTTP::Post::Multipart.
54
55
  #
@@ -59,31 +60,34 @@ module UploadIO
59
60
  #
60
61
  # UploadIO.new("file.txt", "text/plain")
61
62
  # UploadIO.new(file_io, "text/plain", "file.txt")
62
- def self.new(filename_or_io, content_type, filename = nil)
63
+ attr_reader :content_type, :original_filename, :local_path, :io
64
+
65
+ def initialize(filename_or_io, content_type, filename = nil)
63
66
  io = filename_or_io
64
- unless io.respond_to? :read
67
+ local_path = ""
68
+ if io.respond_to? :read
69
+ local_path = filename_or_io.path
70
+ else
65
71
  io = File.open(filename_or_io)
66
- filename = filename_or_io
72
+ local_path = filename_or_io
67
73
  end
68
- convert!(io, content_type, File.basename(filename), filename)
69
- io
74
+ filename ||= local_path
75
+
76
+ @content_type = content_type
77
+ @original_filename = File.basename(filename)
78
+ @local_path = local_path
79
+ @io = io
70
80
  end
71
81
 
72
- # Enhance an existing IO for including in the params hash of a
73
- # Net::HTTP::Post::Multipart by adding #content_type, #original_filename,
74
- # and #local_path methods to the object's singleton class.
75
82
  def self.convert!(io, content_type, original_filename, local_path)
76
- io.instance_eval(<<-EOS, __FILE__, __LINE__)
77
- def content_type
78
- "#{content_type}"
79
- end
80
- def original_filename
81
- "#{original_filename}"
82
- end
83
- def local_path
84
- "#{local_path}"
85
- end
86
- EOS
87
- io
83
+ raise ArgumentError, "convert! has been removed. You must now wrap IOs using:\nUploadIO.new(filename_or_io, content_type, filename=nil)\nPlease update your code."
84
+ end
85
+
86
+ def method_missing(*args)
87
+ @io.send(*args)
88
+ end
89
+
90
+ def respond_to?(meth)
91
+ @io.respond_to?(meth) || super(meth)
88
92
  end
89
93
  end
@@ -23,19 +23,19 @@ class Net::HTTP::Post::MultiPartTest < Test::Unit::TestCase
23
23
  def test_form_multipart_body
24
24
  File.open(TEMP_FILE, "w") {|f| f << "1234567890"}
25
25
  @io = File.open(TEMP_FILE)
26
- UploadIO.convert! @io, "text/plain", TEMP_FILE, TEMP_FILE
26
+ @io = UploadIO.new @io, "text/plain", TEMP_FILE
27
27
  assert_results Net::HTTP::Post::Multipart.new("/foo/bar", :foo => 'bar', :file => @io)
28
28
  end
29
29
  def test_form_multipart_body_put
30
30
  File.open(TEMP_FILE, "w") {|f| f << "1234567890"}
31
31
  @io = File.open(TEMP_FILE)
32
- UploadIO.convert! @io, "text/plain", TEMP_FILE, TEMP_FILE
32
+ @io = UploadIO.new @io, "text/plain", TEMP_FILE
33
33
  assert_results Net::HTTP::Put::Multipart.new("/foo/bar", :foo => 'bar', :file => @io)
34
34
  end
35
-
35
+
36
36
  def test_form_multipart_body_with_stringio
37
37
  @io = StringIO.new("1234567890")
38
- UploadIO.convert! @io, "text/plain", TEMP_FILE, TEMP_FILE
38
+ @io = UploadIO.new @io, "text/plain", TEMP_FILE
39
39
  assert_results Net::HTTP::Post::Multipart.new("/foo/bar", :foo => 'bar', :file => @io)
40
40
  end
41
41
 
@@ -11,25 +11,35 @@ class CompositeReadIOTest < Test::Unit::TestCase
11
11
  def test_full_read_from_several_ios
12
12
  assert_equal 'the quick brown fox', @io.read
13
13
  end
14
-
14
+
15
+ unless RUBY_VERSION < '1.9'
16
+ def test_read_from_multibyte
17
+ utf8 = File.open(File.dirname(__FILE__)+'/multibyte.txt')
18
+ binary = StringIO.new("\x86")
19
+ @io = CompositeReadIO.new(binary,utf8)
20
+ assert_equal "\x86\xE3\x83\x95\xE3\x82\xA1\xE3\x82\xA4\xE3\x83\xAB\n", @io.read
21
+ end
22
+ end
23
+
24
+
15
25
  def test_partial_read
16
26
  assert_equal 'the quick', @io.read(9)
17
27
  end
18
-
28
+
19
29
  def test_partial_read_to_boundary
20
- assert_equal 'the quick ', @io.read(10)
30
+ assert_equal 'the quick ', @io.read(10)
21
31
  end
22
-
32
+
23
33
  def test_read_with_size_larger_than_available
24
34
  assert_equal 'the quick brown fox', @io.read(32)
25
35
  end
26
-
36
+
27
37
  def test_read_into_buffer
28
38
  buf = ''
29
39
  @io.read(nil, buf)
30
40
  assert_equal 'the quick brown fox', buf
31
41
  end
32
-
42
+
33
43
  def test_multiple_reads
34
44
  assert_equal 'the ', @io.read(4)
35
45
  assert_equal 'quic', @io.read(4)
@@ -37,7 +47,7 @@ class CompositeReadIOTest < Test::Unit::TestCase
37
47
  assert_equal 'own ', @io.read(4)
38
48
  assert_equal 'fox', @io.read(4)
39
49
  end
40
-
50
+
41
51
  def test_read_after_end
42
52
  @io.read
43
53
  assert_equal "", @io.read
@@ -47,4 +57,10 @@ class CompositeReadIOTest < Test::Unit::TestCase
47
57
  @io.read(32)
48
58
  assert_equal nil, @io.read(32)
49
59
  end
60
+
61
+ def test_convert_error
62
+ assert_raises(ArgumentError) {
63
+ UploadIO.convert!('tmp.txt', 'text/plain', 'tmp.txt', 'tmp.txt')
64
+ }
65
+ end
50
66
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multipart-post
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 19
4
5
  prerelease: false
5
6
  segments:
6
7
  - 1
7
- - 0
8
8
  - 1
9
- version: 1.0.1
9
+ - 0
10
+ version: 1.1.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - Nick Sieger
@@ -14,23 +15,10 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-04-27 00:00:00 -05:00
18
+ date: 2011-01-11 00:00:00 -06:00
18
19
  default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- name: rubyforge
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 2
29
- - 0
30
- - 4
31
- version: 2.0.4
32
- type: :development
33
- version_requirements: *id001
20
+ dependencies: []
21
+
34
22
  description: "Use with Net::HTTP to do multipart form posts. IO values that have #content_type, #original_filename, and #local_path will be posted as a binary file."
35
23
  email: nick@nicksieger.com
36
24
  executables: []
@@ -61,23 +49,27 @@ rdoc_options:
61
49
  require_paths:
62
50
  - lib
63
51
  required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
64
53
  requirements:
65
54
  - - ">="
66
55
  - !ruby/object:Gem::Version
56
+ hash: 3
67
57
  segments:
68
58
  - 0
69
59
  version: "0"
70
60
  required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
71
62
  requirements:
72
63
  - - ">="
73
64
  - !ruby/object:Gem::Version
65
+ hash: 3
74
66
  segments:
75
67
  - 0
76
68
  version: "0"
77
69
  requirements: []
78
70
 
79
71
  rubyforge_project: caldersphere
80
- rubygems_version: 1.3.6
72
+ rubygems_version: 1.3.7
81
73
  signing_key:
82
74
  specification_version: 3
83
75
  summary: Creates a multipart form post accessory for Net::HTTP.