tupper 1.0.0

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.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tupper.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 SHIOYA, Hiromu
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Tupper
2
+
3
+ Tupper is a helper for processing uploaded file via web form.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'tupper'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install tupper
18
+
19
+ ## Usage
20
+
21
+ [TODO] add sample for Sinatra controller.
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ Bundler.setup
5
+ require 'rspec/core/rake_task'
6
+
7
+ desc "run spec"
8
+ RSpec::Core::RakeTask.new(:spec) do |t|
9
+ t.rspec_opts = ["-c", "-fs"]
10
+ end
@@ -0,0 +1,3 @@
1
+ class Tupper
2
+ VERSION = "1.0.0"
3
+ end
data/lib/tupper.rb ADDED
@@ -0,0 +1,60 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "tupper/version"
3
+ require 'json'
4
+
5
+ class Tupper
6
+ DEFAULT_TMP_DIR = File.join(%w{ / tmp tupper })
7
+ SESSION_STORE_KEY = 'tupper_file_info'
8
+
9
+ attr_reader :temp_dir, :file_info
10
+
11
+ def initialize session
12
+ @session = session
13
+ unless ((json = @session.fetch(SESSION_STORE_KEY, '')).empty?)
14
+ begin
15
+ @file_info = JSON.parse(json)
16
+ rescue
17
+ @session.delete(SESSION_STORE_KEY)
18
+ raise RuntimeError.new('invalid session data')
19
+ end
20
+ end
21
+ end
22
+
23
+ def temp_dir= temp_dir
24
+ FileUtils.mkdir_p temp_dir
25
+ @temp_dir = temp_dir
26
+ end
27
+
28
+ def has_uploaded_file?
29
+ @file_info && File.exists?(@file_info.fetch("uploaded_file", ''))
30
+ end
31
+
32
+ def upload file_info
33
+ unless @temp_dir
34
+ self.temp_dir = DEFAULT_TMP_DIR
35
+ end
36
+
37
+ file_hash = "#{Time.now.to_i}_#{Digest::MD5.hexdigest(file_info[:filename]).slice(0, 8)}"
38
+ uploaded_file = File.join(temp_dir, file_hash + File.extname(file_info[:filename]))
39
+ FileUtils.copy(file_info[:tempfile], uploaded_file)
40
+ tupper_file_info = {
41
+ uploaded_file: uploaded_file,
42
+ original_file: file_info[:filename],
43
+ }.to_json
44
+ @session.store SESSION_STORE_KEY, tupper_file_info
45
+ end
46
+
47
+ def uploaded_file
48
+ (@file_info || {}).fetch('uploaded_file', nil)
49
+ end
50
+
51
+ def original_file
52
+ (@file_info || {}).fetch('original_file', nil)
53
+ end
54
+
55
+ def cleanup
56
+ File.unlink uploaded_file if has_uploaded_file?
57
+ @session.delete SESSION_STORE_KEY
58
+ @file_info = nil
59
+ end
60
+ end
@@ -0,0 +1,95 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'bundler'
3
+ Bundler.setup(:default, :test)
4
+ require 'fakefs/spec_helpers'
5
+ require 'tupper'
6
+
7
+ describe Tupper do
8
+ include FakeFS::SpecHelpers
9
+
10
+ let(:collect_json) {
11
+ "{\"uploaded_file\":\"/tmp/tupper/1341556030_54c89662.txt\",\"original_file\":\"dummy_tsv.txt\"}"
12
+ }
13
+
14
+ describe '#initialize' do
15
+ context 'without session data' do
16
+ before do
17
+ @tupper = Tupper.new({})
18
+ @tupper.temp_dir = Tupper::DEFAULT_TMP_DIR
19
+ end
20
+ it "should create default temporary directory" do
21
+ Dir.exists?(@tupper.temp_dir).should be_true
22
+ @tupper.temp_dir.should == '/tmp/tupper'
23
+ end
24
+ end
25
+
26
+ context 'with invalid session data' do
27
+ specify { expect { Tupper.new(Tupper::SESSION_STORE_KEY.to_s => 'invalid json') }.to raise_error RuntimeError }
28
+ end
29
+
30
+ context 'with valid session data' do
31
+ subject { Tupper.new(Tupper::SESSION_STORE_KEY.to_s => collect_json) }
32
+ its(:file_info) { should be_instance_of Hash }
33
+ end
34
+ end
35
+
36
+ describe '#temp_dir=' do
37
+ before do
38
+ @tupper = Tupper.new({})
39
+ @tupper.temp_dir = 'hoge'
40
+ end
41
+
42
+ it 'should set property "temp_dir" and create directory' do
43
+ @tupper.temp_dir.should == 'hoge'
44
+ Dir.exists?(@tupper.temp_dir).should be_true
45
+ end
46
+ end
47
+
48
+ describe '#has_uploaded_file?' do
49
+ context 'before upload' do
50
+ subject { Tupper.new({}) }
51
+ it { should_not have_uploaded_file }
52
+ end
53
+
54
+ context 'has collect session' do
55
+ before do
56
+ @tupper = Tupper.new(Tupper::SESSION_STORE_KEY.to_s => collect_json)
57
+ FileUtils.mkdir_p Tupper::DEFAULT_TMP_DIR
58
+ FileUtils.touch('/tmp/tupper/1341556030_54c89662.txt')
59
+ end
60
+ specify { @tupper.should have_uploaded_file }
61
+ end
62
+ end
63
+
64
+ describe '#uploaded_file' do
65
+ context 'before upload'do
66
+ subject { Tupper.new({}) }
67
+ its(:uploaded_file) { should be_nil }
68
+ end
69
+
70
+ context 'has collect session' do
71
+ before do
72
+ @tupper = Tupper.new(Tupper::SESSION_STORE_KEY.to_s => collect_json)
73
+ FileUtils.mkdir_p Tupper::DEFAULT_TMP_DIR
74
+ FileUtils.touch('/tmp/tupper/1341556030_54c89662.txt')
75
+ end
76
+
77
+ subject { @tupper.uploaded_file }
78
+ it { should == '/tmp/tupper/1341556030_54c89662.txt' }
79
+ end
80
+ end
81
+
82
+ describe '#cleanup' do
83
+ before do
84
+ @tupper = Tupper.new(Tupper::SESSION_STORE_KEY.to_s => collect_json)
85
+ FileUtils.mkdir_p Tupper::DEFAULT_TMP_DIR
86
+ FileUtils.touch('/tmp/tupper/1341556030_54c89662.txt')
87
+ @tupper.cleanup
88
+ end
89
+
90
+ it 'should delete uploaded_file and session' do
91
+ @tupper.instance_variable_get(:@session).should_not be_include Tupper::SESSION_STORE_KEY
92
+ File.exists?('/tmp/tupper/1341556030_54c89662.txt').should be_false
93
+ end
94
+ end
95
+ end
data/tupper.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/tupper/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["SHIOYA, Hiromu"]
6
+ gem.email = ["kwappa.856@gmail.com"]
7
+ gem.description = "Tupper is a helper for processing uploaded file via web form."
8
+ gem.summary = "Tupper is a helper for processing uploaded file via web form."
9
+ gem.homepage = "https://github.com/kwappa/tupper"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "tupper"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Tupper::VERSION
17
+
18
+ gem.add_development_dependency "rspec"
19
+ gem.add_development_dependency "fakefs"
20
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tupper
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - SHIOYA, Hiromu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: fakefs
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Tupper is a helper for processing uploaded file via web form.
47
+ email:
48
+ - kwappa.856@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - lib/tupper.rb
59
+ - lib/tupper/version.rb
60
+ - spec/tupper_spec.rb
61
+ - tupper.gemspec
62
+ homepage: https://github.com/kwappa/tupper
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ segments:
75
+ - 0
76
+ hash: -87756479144681564
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ segments:
84
+ - 0
85
+ hash: -87756479144681564
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.24
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Tupper is a helper for processing uploaded file via web form.
92
+ test_files:
93
+ - spec/tupper_spec.rb