uploadable_file 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ *.iml
23
+ *.ipr
24
+ *.idea/*
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Nick Zalabak
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,12 @@
1
+ = uploadable_file
2
+
3
+ UploadableFile is a simple wrapper around Tempfile that adds original_filename and content_type as attributes.
4
+ It essentially acts as a multipart file would if uploaded in a form.
5
+ The idea was borrowed from ActionPack::TestUploadedFile, but I wanted something more lightweight that doesn't
6
+ have the baggage of activesupport.
7
+ Other viable alternatives to this would be to use Rack's Multipart::UploadedFile, but again you'd need the entire
8
+ Rack gem.
9
+
10
+ == Copyright
11
+
12
+ Copyright (c) 2010 Nick Zalabak. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "uploadable_file"
8
+ gem.summary = %Q{Simple wrapper around a Tempfile that adds content_type and original file name}
9
+ gem.description = %Q{Useful for file attachment (paperclip, attachment_fu, etc) outside your standard web frameworks}
10
+ gem.email = "techwhizbang@gmail.com"
11
+ gem.homepage = "http://github.com/techwhizbang/uploadable_file"
12
+ gem.authors = ["Nick Zalabak"]
13
+ gem.add_dependency "mime-types", ">= 1.16"
14
+ gem.add_development_dependency "shoulda", ">= 2.11.3"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "uploadable_file #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,38 @@
1
+ require 'tempfile'
2
+ require 'fileutils'
3
+ require 'mime/types'
4
+
5
+ class UploadableFile
6
+ # The filename, *not* including the path, of the "uploaded" file
7
+ attr_reader :original_filename
8
+
9
+ # The content type of the "uploaded" file
10
+ attr_accessor :content_type
11
+
12
+ def initialize(path, options = {})
13
+ raise "#{path} file does not exist" unless File.exist?(path)
14
+
15
+ if options[:content_type]
16
+ @content_type = options[:content_type]
17
+ else
18
+ mime_types = MIME::Types.type_for(path)
19
+ @content_type = mime_types.empty? ? MIME::Types['text/plain'].first.content_type : mime_types.first.content_type
20
+ end
21
+
22
+ @original_filename = path.sub(/^.*#{File::SEPARATOR}([^#{File::SEPARATOR}]+)$/) { $1 }
23
+ @tempfile = Tempfile.new(@original_filename)
24
+ @tempfile.set_encoding(Encoding::BINARY) if @tempfile.respond_to?(:set_encoding)
25
+ @tempfile.binmode if options[:binary]
26
+ FileUtils.copy_file(path, @tempfile.path)
27
+ end
28
+
29
+ def path #:nodoc:
30
+ @tempfile.path
31
+ end
32
+
33
+ alias local_path path
34
+
35
+ def method_missing(method_name, *args, &block) #:nodoc:
36
+ @tempfile.__send__(method_name, *args, &block)
37
+ end
38
+ end
data/test/bowling.zip ADDED
Binary file
data/test/dilbert.jpg ADDED
Binary file
data/test/helper.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'uploadable_file'
@@ -0,0 +1,37 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/helper")
2
+
3
+ class TestUploadableFile < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @zip_file_path = File.expand_path(File.dirname(__FILE__) + "/bowling.zip")
7
+ @image_file_path = File.expand_path(File.dirname(__FILE__) + "/dilbert.jpg")
8
+ @text_file_path = File.expand_path(File.dirname(__FILE__) + "/text.txt")
9
+ end
10
+
11
+ should "return the original file name" do
12
+ assert_equal "bowling.zip", UploadableFile.new(@zip_file_path).original_filename
13
+ end
14
+
15
+ should "raise an Exception if the file does not exist" do
16
+ assert_raise RuntimeError, "/dev/null/foo.txt file does not exist" do
17
+ UploadableFile.new("/dev/null/foo.txt")
18
+ end
19
+ end
20
+
21
+ should "set the content type with MIME::Type" do
22
+ assert_equal "application/zip", UploadableFile.new(@zip_file_path).content_type
23
+ end
24
+
25
+ should "set manually set the content type if specified in the options" do
26
+ assert_equal "imaginary/content_type", UploadableFile.new(@zip_file_path, :content_type => "imaginary/content_type").content_type
27
+ end
28
+
29
+ should "set the encoding to binary if file responds to set_encoding" do
30
+ assert_equal "imaginary/content_type", UploadableFile.new(@zip_file_path, :content_type => "imaginary/content_type").content_type
31
+ end
32
+
33
+ should "copy the file to the tempfile path" do
34
+ assert File.exists?(UploadableFile.new(@zip_file_path).path)
35
+ end
36
+
37
+ end
data/test/text.txt ADDED
File without changes
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{uploadable_file}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Nick Zalabak"]
12
+ s.date = %q{2010-08-31}
13
+ s.description = %q{Useful for file attachment (paperclip, attachment_fu, etc) outside your standard web frameworks}
14
+ s.email = %q{techwhizbang@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/uploadable_file.rb",
27
+ "test/bowling.zip",
28
+ "test/dilbert.jpg",
29
+ "test/helper.rb",
30
+ "test/test_uploadable_file.rb",
31
+ "test/text.txt",
32
+ "uploadable_file.gemspec"
33
+ ]
34
+ s.homepage = %q{http://github.com/techwhizbang/uploadable_file}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.7}
38
+ s.summary = %q{Simple wrapper around a Tempfile that adds content_type and original file name}
39
+ s.test_files = [
40
+ "test/helper.rb",
41
+ "test/test_uploadable_file.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<mime-types>, [">= 1.16"])
50
+ s.add_development_dependency(%q<shoulda>, [">= 2.11.3"])
51
+ else
52
+ s.add_dependency(%q<mime-types>, [">= 1.16"])
53
+ s.add_dependency(%q<shoulda>, [">= 2.11.3"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<mime-types>, [">= 1.16"])
57
+ s.add_dependency(%q<shoulda>, [">= 2.11.3"])
58
+ end
59
+ end
60
+
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uploadable_file
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Nick Zalabak
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-31 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: mime-types
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 47
30
+ segments:
31
+ - 1
32
+ - 16
33
+ version: "1.16"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: shoulda
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 37
45
+ segments:
46
+ - 2
47
+ - 11
48
+ - 3
49
+ version: 2.11.3
50
+ type: :development
51
+ version_requirements: *id002
52
+ description: Useful for file attachment (paperclip, attachment_fu, etc) outside your standard web frameworks
53
+ email: techwhizbang@gmail.com
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ extra_rdoc_files:
59
+ - LICENSE
60
+ - README.rdoc
61
+ files:
62
+ - .document
63
+ - .gitignore
64
+ - LICENSE
65
+ - README.rdoc
66
+ - Rakefile
67
+ - VERSION
68
+ - lib/uploadable_file.rb
69
+ - test/bowling.zip
70
+ - test/dilbert.jpg
71
+ - test/helper.rb
72
+ - test/test_uploadable_file.rb
73
+ - test/text.txt
74
+ - uploadable_file.gemspec
75
+ has_rdoc: true
76
+ homepage: http://github.com/techwhizbang/uploadable_file
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options:
81
+ - --charset=UTF-8
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ requirements: []
103
+
104
+ rubyforge_project:
105
+ rubygems_version: 1.3.7
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Simple wrapper around a Tempfile that adds content_type and original file name
109
+ test_files:
110
+ - test/helper.rb
111
+ - test/test_uploadable_file.rb