t_bird 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8bed597706b18d287438b31c1a9be55fa0528453
4
+ data.tar.gz: 09c9333f2f2946f1a1166e221f0f268310eb7a02
5
+ SHA512:
6
+ metadata.gz: af944ed056e75ff4380f215290d5dd354daeacf9b4db1856495fa1b087d2780c119e1269bdb6353ef02259d1d9f33137daaaf9d42b7927e977379fdf4c7bb409
7
+ data.tar.gz: 43aa1b46dedb3d0c2d1ed0c3cdfb53ce74fe963ddceee50ade17c376a39bd7322b99a8622b0115e9b1c12f84f0bc3b73cc8966bd33bf379f1f01724fb499907d
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ group :test do
5
+ gem 'sinatra', require: false
6
+ gem 'minitest', require: false
7
+ gem 'minitest-spec-context', require: false
8
+ gem 'minitest-reporters', require: false
9
+ gem 'mocha', require: false
10
+ gem 'rack-test', require: false
11
+ gem 'uuid', require: false
12
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Eric Marden
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,41 @@
1
+ # TBird
2
+
3
+ Straight forward file uploads for Ruby Apps.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 't_bird'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ ## Why?
16
+
17
+ I became frustrated by the very coupled design of the leading ruby
18
+ upload libraries and wanted something that was a little more modular (to
19
+ allow for flexibility), wasn't glued to a `model` class, and fulfilled my
20
+ most common use case out of the box:
21
+
22
+ - Upload an image, posted from a multi-part form,
23
+ - process the file into 1 or more versions (resize, crop, etc),
24
+ - and stream the files to s3 for storage without keeping anything on
25
+ the local filesystem.
26
+
27
+ ## Usage
28
+
29
+ _Coming Soon_
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
38
+
39
+ ## Shout Outs
40
+
41
+ Sample image, used in tests, was provided by [cherrylet](http://www.flickr.com/photos/cherrylet/10258332985/sizes/o/in/photostream/), under the Creative Commons 2.0. No endorsement of this library by the photographer is intended or implied.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'rake'
4
+ require "bundler/gem_tasks"
5
+ require 'rake/testtask'
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "spec"
8
+ t.pattern = "spec/**/*_spec.rb"
9
+ end
10
+ task :default => [:test]
@@ -0,0 +1,18 @@
1
+ # encoding utf-8
2
+
3
+ module TBird
4
+ class Configuration
5
+
6
+ class << self
7
+ attr_accessor :aws_key, :aws_secret, :aws_bucket, :thumbnail_size
8
+ end
9
+
10
+ def self.configure
11
+ yield self
12
+ end
13
+
14
+ def self.thumbnail_size
15
+ @thumbnail_size ||= 100
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ require 'digest/sha1'
4
+ require 'pathname'
5
+
6
+ module TBird
7
+ class Namer
8
+ attr_reader :ext, :identifier, :token
9
+ def initialize(original_filename, identifier = nil, token = SecureRandom.uuid)
10
+ @ext = Pathname.new(original_filename).extname
11
+ @identifier = identifier || Digest::SHA1.hexdigest(original_filename)
12
+ @token = token
13
+ end
14
+
15
+ def new_name(version = 'original')
16
+ "#{identifier}/#{token}_#{version}#{ext}"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ require 'mini_magick'
4
+
5
+ module TBird
6
+ class Processor
7
+ attr_reader :image, :thumbnail_size
8
+ def initialize(file_blob)
9
+ @image = MiniMagick::Image.read(file_blob)
10
+ @thumbnail_size = Configuration.thumbnail_size
11
+ end
12
+
13
+ def process(&block)
14
+ image.combine_options do |magick|
15
+ block.call(magick) if block_given?
16
+ end
17
+ image
18
+ end
19
+
20
+ def resize(size)
21
+ process do |magick|
22
+ magick.resize size
23
+ end
24
+ end
25
+
26
+ def thumbnail
27
+ process do |magick|
28
+ magick.auto_orient
29
+ magick.thumbnail "x#{thumbnail_size*2}"
30
+ magick.resize "#{thumbnail_size*2}x<"
31
+ magick.resize "50%"
32
+ magick.gravity "center"
33
+ magick.crop "#{thumbnail_size}x#{thumbnail_size}+0+0"
34
+ magick.quality 92
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+
3
+ require 'aws/s3'
4
+
5
+ module TBird
6
+ class Transmitter
7
+ include AWS::S3
8
+ attr_reader :name, :file, :metadata
9
+ def initialize(name, file, metadata = {})
10
+ @name = name
11
+ @file = file
12
+ @metadata = default_metadata.merge(metadata)
13
+ end
14
+
15
+ def transmit!
16
+ connect!
17
+ @tranmission = S3Object.store(name, file, Configuration.aws_bucket, metadata)
18
+ end
19
+
20
+ def url
21
+ @transmission.url
22
+ end
23
+
24
+ private
25
+
26
+ def connect!
27
+ Base.establish_connection!(access_key_id: Configuration.aws_key, secret_access_key: Configuration.aws_secret, use_ssl: true)
28
+ end
29
+
30
+ def default_metadata
31
+ {
32
+ access: :public_read,
33
+ content_type: 'binary/octet-stream'
34
+ }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ module TBird
4
+ class Upload
5
+ attr_reader :options
6
+ def initialize(file, options = {})
7
+ @options = default_options.merge(options)
8
+ @file = file
9
+ end
10
+
11
+ def content_type
12
+ @file_data ||= @file.content_type
13
+ end
14
+
15
+ def original_filename
16
+ @file_data ||= @file.original_filename
17
+ end
18
+
19
+ def original_file
20
+ @file_data ||= @file.read
21
+ end
22
+
23
+ private
24
+
25
+ def default_options
26
+ {}
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module TBird
2
+ VERSION = "0.0.1"
3
+ end
data/lib/t_bird.rb ADDED
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ require 't_bird/configuration'
4
+ require 't_bird/namer'
5
+ require 't_bird/processor'
6
+ require 't_bird/transmitter'
7
+ require 't_bird/uploader'
8
+ require 't_bird/version'
9
+
10
+ module TBird
11
+ module Errors
12
+ class Unknown < StandardError ; end
13
+ end
14
+ end
data/spec/sample.jpg ADDED
Binary file
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ ENV['RACK_ENV'] = 'test'
4
+
5
+ lib_path = File.expand_path('../../lib', __FILE__)
6
+ ($:.unshift lib_path) unless ($:.include? lib_path)
7
+
8
+ Bundler.setup(:default, ENV['RACK_ENV'])
9
+
10
+ require 't_bird'
11
+
12
+ require 'minitest/autorun'
13
+ require 'minitest/reporters'
14
+
15
+ MiniTest::Reporters.use! MiniTest::Reporters::SpecReporter.new
16
+
17
+ require 'mocha/setup'
18
+ require 'rack/test'
19
+ #include Rack::Test::Methods
20
+
21
+ require 'uuid'
22
+
23
+ module TBirdSpecData
24
+ def sample_file
25
+ File.expand_path('../sample.jpg', __FILE__)
26
+ end
27
+
28
+ def upload_file
29
+ Rack::Test::UploadedFile.new(sample_file, 'image/jpeg')
30
+ end
31
+
32
+ def upload_filedata
33
+ upload_file.read
34
+ end
35
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TBird::Configuration do
6
+ before do
7
+ TBird::Configuration.configure do |config|
8
+ config.aws_key = 'access_key_id'
9
+ config.aws_secret = 'secret_access_key'
10
+ config.aws_bucket = 'bucket_name'
11
+ config.thumbnail_size = 200
12
+ end
13
+ end
14
+
15
+ it "can configure :aws_key" do
16
+ TBird::Configuration.aws_key.must_equal 'access_key_id'
17
+ end
18
+
19
+ it "can configure :aws_secret" do
20
+ TBird::Configuration.aws_secret.must_equal 'secret_access_key'
21
+ end
22
+
23
+ it "can configure :aws_bucket" do
24
+ TBird::Configuration.aws_bucket.must_equal 'bucket_name'
25
+ end
26
+ it "can configure :thumbnail_size" do
27
+ TBird::Configuration.thumbnail_size.must_equal 200
28
+ end
29
+ end
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TBird::Namer do
6
+ include TBirdSpecData
7
+ before do
8
+ @original_filename = Pathname.new(sample_file).basename.to_s
9
+ @namer = TBird::Namer
10
+ end
11
+
12
+ it "returns the extension of the original filename" do
13
+ @namer.new(@original_filename).ext.must_equal '.jpg'
14
+ end
15
+
16
+ it "returns the default identifier" do
17
+ @namer.new(@original_filename).identifier.must_equal Digest::SHA1.hexdigest(@original_filename)
18
+ end
19
+
20
+ it "returns the specified identifier" do
21
+ @namer.new(@original_filename, 1).identifier.must_equal 1
22
+ end
23
+
24
+ it "returns the generated token" do
25
+ UUID.validate(@namer.new(@original_filename).token).must_equal true
26
+ end
27
+
28
+ it "returns the given token" do
29
+ token = SecureRandom.uuid
30
+ @namer.new(@original_filename, nil, token).token.must_equal token
31
+ end
32
+
33
+ it "returns the new name for 'original' version" do
34
+ token = SecureRandom.uuid
35
+ @namer.new(@original_filename, 1, token).new_name.must_equal "1/#{token}_original.jpg"
36
+ end
37
+
38
+ it "returns the new name for the given version" do
39
+ token = SecureRandom.uuid
40
+ @namer.new(@original_filename, 1, token).new_name('thumb').must_equal "1/#{token}_thumb.jpg"
41
+ end
42
+ end
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TBird::Processor do
6
+ include TBirdSpecData
7
+
8
+ before do
9
+ @custom_process = Proc.new do |magick|
10
+ magick.resize '50%'
11
+ magick.quality 88
12
+ end
13
+
14
+ @processor = TBird::Processor.new(upload_file)
15
+ @versions = {
16
+ thumb: '90x90',
17
+ medium: '300',
18
+ large: 'x300',
19
+ custom: @custom_process
20
+ }
21
+ end
22
+
23
+ it "can process an image" do
24
+ image = @processor.process(&@custom_process)
25
+ image.write StringIO.new
26
+ image.valid?.must_equal true
27
+ image.destroy!
28
+ end
29
+
30
+ it "can process image into a thubnail" do
31
+ image = @processor.thumbnail
32
+ image.write StringIO.new
33
+ image.valid?.must_equal true
34
+ image.destroy!
35
+ end
36
+
37
+ it "can resize an image" do
38
+ image = @processor.resize('300')
39
+ image.write StringIO.new
40
+ image.valid?.must_equal true
41
+ image.destroy!
42
+ end
43
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TBird::Transmitter do
6
+ include TBirdSpecData
7
+ before do
8
+ TBird::Configuration.configure do |config|
9
+ config.aws_key = 'abc'
10
+ config.aws_secret = '123'
11
+ config.aws_bucket = 'bucket'
12
+ end
13
+
14
+ keys = { access_key_id: TBird::Configuration.aws_key,
15
+ secret_access_key: TBird::Configuration.aws_secret,
16
+ use_ssl: true }
17
+
18
+ AWS::S3::S3Object.stubs(:connected?).returns(true)
19
+ AWS::S3::Base.stubs(:establish_connection!).with(keys)
20
+
21
+ @stored_filename = '1/sample_original.jpg'
22
+ @transmitter = TBird::Transmitter.new(@stored_filename, upload_filedata, { content_type: 'image/jpeg' })
23
+ end
24
+
25
+ it "transmit file to store" do
26
+ AWS::S3::S3Object.expects(:store).with(@stored_filename, upload_filedata, 'bucket', {:access => :public_read, :content_type => 'image/jpeg'})
27
+ @transmitter.transmit!
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TBird::Upload do
6
+ include TBirdSpecData
7
+
8
+ before do
9
+ @tbird_upload = TBird::Upload.new(upload_file)
10
+ end
11
+
12
+ it "can return original filename" do
13
+ @tbird_upload.original_filename.must_equal 'sample.jpg'
14
+ end
15
+
16
+ it "can return content type" do
17
+ @tbird_upload.content_type.must_equal 'image/jpeg'
18
+ end
19
+
20
+ it "can return the original file binary data" do
21
+ @tbird_upload.original_file.wont_be_nil
22
+ end
23
+ end
data/t_bird.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $:.unshift(lib) unless $:.include?(lib)
4
+ require 't_bird/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 't_bird'
8
+ gem.version = TBird::VERSION
9
+ gem.authors = ['Eric Marden']
10
+ gem.email = ['eric@xentek.net']
11
+ gem.description = %q{Straight forward file uploads for Ruby Apps.}
12
+ gem.summary = %q{Straight forward file uploads for Ruby Apps.}
13
+ gem.homepage = 'https://github.com/xentek/t_bird'
14
+ gem.license = 'MIT'
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ['lib']
20
+
21
+ gem.add_dependency 'mini_magick'
22
+ gem.add_dependency 'aws-s3'
23
+ gem.add_development_dependency 'bundler', '~> 1.3'
24
+ gem.add_development_dependency 'rake'
25
+ gem.add_development_dependency 'gem-release'
26
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: t_bird
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Eric Marden
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mini_magick
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-s3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: gem-release
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Straight forward file uploads for Ruby Apps.
84
+ email:
85
+ - eric@xentek.net
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/t_bird.rb
96
+ - lib/t_bird/configuration.rb
97
+ - lib/t_bird/namer.rb
98
+ - lib/t_bird/processor.rb
99
+ - lib/t_bird/transmitter.rb
100
+ - lib/t_bird/uploader.rb
101
+ - lib/t_bird/version.rb
102
+ - spec/sample.jpg
103
+ - spec/spec_helper.rb
104
+ - spec/t_bird/configuration_spec.rb
105
+ - spec/t_bird/namer_spec.rb
106
+ - spec/t_bird/processor_spec.rb
107
+ - spec/t_bird/transmitter_spec.rb
108
+ - spec/t_bird/uploader_spec.rb
109
+ - t_bird.gemspec
110
+ homepage: https://github.com/xentek/t_bird
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.0.5
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Straight forward file uploads for Ruby Apps.
134
+ test_files:
135
+ - spec/sample.jpg
136
+ - spec/spec_helper.rb
137
+ - spec/t_bird/configuration_spec.rb
138
+ - spec/t_bird/namer_spec.rb
139
+ - spec/t_bird/processor_spec.rb
140
+ - spec/t_bird/transmitter_spec.rb
141
+ - spec/t_bird/uploader_spec.rb