tori 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 13b7fb38fdb55e8c050508df44902f0eba8e395c
4
- data.tar.gz: 454e9d1433db7d637d01dc29595a69cf53668095
3
+ metadata.gz: 804032544a779fc9014553a31b0ea5c2314a3db1
4
+ data.tar.gz: 487ba2e0af21edf8bf41b4163301f66049d27768
5
5
  SHA512:
6
- metadata.gz: 355e802d40cd93d147c573656f5ef4fba70b396eeb4db1927eabd0cdf7776505546252a2d97673a3a1cdc4b0e203ad86d9903c6339079765f0e61e1c37aa1a6e
7
- data.tar.gz: 4962d066361815dd3aa739af5e1b87cb7ec196c699f2a6944e18e4f0614aaf3a3d1ad8e5dce43a0890e508697def5fb711562a0cc67c6b5b7d0f8d65e07ea772
6
+ metadata.gz: 7027bb399d67358423ce0feb6655843f27381773afa1f500f1e35f1cec75fc8f2435b3e211f591e98f50bed23b144dbb31718444321c17f81cb0a64f9f266361
7
+ data.tar.gz: 2d47426d4c1afb1b07458ff8ed442031995fda718ba1659723cc6ed630ce64b1a7adfc61fa56f94fae3ee261c265b126241a0578cb9dbb7c7efa7a1aa74eabff
data/README.md CHANGED
@@ -88,6 +88,11 @@ class Photo < ActiveRecord::Base
88
88
  tori :striped_image do |model|
89
89
  "#{model.class}/striped/#{model.striped_filename}"
90
90
  end
91
+
92
+ # customize backend each by `tori` method.
93
+ tori :custom, to: Tori::Backend::FileSystem.new(Pathname("custom")) do |model|
94
+ "#{__tori__}/#{id}"
95
+ end
91
96
  end
92
97
 
93
98
  class PhotoController < ApplicationController
@@ -8,6 +8,12 @@ module Tori
8
8
  attr_accessor :bucket
9
9
  attr_reader :client
10
10
 
11
+ class << self
12
+ def type_for(path)
13
+ (MIME::Types.type_for(path).first || DEFAULT_CONTENT_TYPE).to_s
14
+ end
15
+ end
16
+
11
17
  # Must be set bucket name.
12
18
  # And it use aws-sdk-core >= 2.0
13
19
  # ENV takes precedence over credentials file and instance profile
@@ -47,6 +53,10 @@ module Tori
47
53
 
48
54
  def write(filename, resource, opts = nil)
49
55
  opts ||= {}
56
+ if from_path = opts.delete(:from_path)
57
+ opts[:content_type] = self.class.type_for(from_path)
58
+ end
59
+
50
60
  case resource
51
61
  when String
52
62
  put_object({
@@ -56,7 +66,7 @@ module Tori
56
66
  }.merge(opts))
57
67
  when File, Pathname
58
68
  path = resource.to_path
59
- content_type = MIME::Types.type_for(path).first || DEFAULT_CONTENT_TYPE
69
+ content_type = self.class.type_for(path)
60
70
  ::File.open(path) { |f|
61
71
  put_object({
62
72
  key: filename,
@@ -91,10 +101,12 @@ module Tori
91
101
  body(filename).read
92
102
  end
93
103
 
104
+ def get(filename)
105
+ get_object(key: filename)
106
+ end
107
+
94
108
  def body(filename)
95
- get_object(
96
- key: filename
97
- )[:body]
109
+ get(filename)[:body]
98
110
  end
99
111
 
100
112
  def put(filename, body, opts={})
data/lib/tori/define.rb CHANGED
@@ -1,15 +1,15 @@
1
1
  module Tori
2
2
  module Define
3
- def tori(name, &block)
3
+ def tori(name, to: nil, &block)
4
4
  name_ivar = "@#{name}".to_sym
5
5
 
6
6
  define_method(name) do
7
7
  ivar = instance_variable_get name_ivar
8
- ivar || instance_variable_set(name_ivar, File.new(self, title: name, &block))
8
+ ivar || instance_variable_set(name_ivar, File.new(self, title: name, to: to, &block))
9
9
  end
10
10
 
11
11
  define_method("#{name}=") do |uploader|
12
- file = File.new(self, title: name, from: uploader, &block)
12
+ file = File.new(self, title: name, from: uploader, to: to, &block)
13
13
  instance_variable_set name_ivar, file
14
14
  end
15
15
  end
data/lib/tori/file.rb CHANGED
@@ -1,24 +1,28 @@
1
1
  module Tori
2
2
  class File
3
- def initialize(model, title: nil, from: nil, &block)
3
+ def initialize(model, title: nil, from: nil, to: nil, &block)
4
4
  @model = model
5
5
  @title = title.kind_of?(String) ? title.to_sym : title
6
- if from.respond_to?(:read) and from.respond_to?(:rewind)
6
+
7
+ @from_path = if from.respond_to?(:path)
8
+ from.path
9
+ else
10
+ nil
11
+ end
12
+
13
+ @backend = to
14
+
15
+ @from = if from.respond_to?(:read) and from.respond_to?(:rewind)
7
16
  from.rewind
8
- @from = from.read
17
+ from.read
9
18
  else
10
- @from = from
19
+ from
11
20
  end
12
21
  @filename_callback = block
13
22
  end
14
23
 
15
24
  def name
16
25
  context = Context.new(@title)
17
- filename_callback = if @filename_callback
18
- @filename_callback
19
- else
20
- Tori.config.filename_callback
21
- end
22
26
  context.define_singleton_method(:__filename_callback__, filename_callback)
23
27
  context.__filename_callback__(@model)
24
28
  end
@@ -29,24 +33,35 @@ module Tori
29
33
  end
30
34
 
31
35
  def read
32
- Tori.config.backend.read name
36
+ backend.read name
33
37
  end
34
38
 
35
39
  def write(opts = nil)
36
- Tori.config.backend.write name, @from, opts
40
+ opts ||= {}
41
+ backend.write name, @from, opts.merge(from_path: @from_path)
37
42
  end
38
43
 
39
44
  def delete
40
- Tori.config.backend.delete name if exist?
45
+ backend.delete name if exist?
46
+ end
47
+
48
+ def filename_callback
49
+ @filename_callback || Tori.config.filename_callback
41
50
  end
42
51
 
52
+ def backend
53
+ @backend || Tori.config.backend
54
+ end
55
+
56
+ private
57
+
43
58
  def respond_to_missing?(sym, include_private)
44
- Tori.config.backend.respond_to?(sym, include_private)
59
+ backend.respond_to?(sym, include_private)
45
60
  end
46
61
 
47
62
  def method_missing(sym, *args)
48
63
  if respond_to_missing?(sym, false)
49
- Tori.config.backend.__send__ sym, name, *args
64
+ backend.__send__ sym, name, *args
50
65
  else
51
66
  fail NameError, "undefined method `#{sym}' for #{Tori.config.backend.inspect}"
52
67
  end
data/lib/tori/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tori
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -28,6 +28,19 @@ class TestToriBackendS3 < Test::Unit::TestCase
28
28
  @backend.delete("testfile")
29
29
  end
30
30
 
31
+ test "auto content_type" do
32
+ Tempfile.create(["test", ".jpeg"]) do |f|
33
+ file = Tori::File.new(:dummy, from: f, to: @backend){ |model| "test-key" }
34
+ begin
35
+ file.write
36
+ content_type = file.get.content_type
37
+ assert { 'image/jpeg' == content_type }
38
+ ensure
39
+ file.delete
40
+ end
41
+ end
42
+ end
43
+
31
44
  test "#initialize" do
32
45
  assert_instance_of Tori::Backend::S3, @backend
33
46
  assert { ENV["TORI_AWS_ACCESS_KEY_ID"] == @backend.client.config.access_key_id }
@@ -1,6 +1,12 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class TestToriDefine < Test::Unit::TestCase
4
+ class CustomBackend
5
+ def read(filename)
6
+ "read:#{filename}"
7
+ end
8
+ end
9
+
4
10
  class Dammy
5
11
  extend Tori::Define
6
12
  tori :test_image
@@ -8,6 +14,9 @@ class TestToriDefine < Test::Unit::TestCase
8
14
  tori :def_image do |model|
9
15
  "foo/#{__tori__}/bar/#{bird}"
10
16
  end
17
+ tori :custom, to: CustomBackend.new do |model|
18
+ "#{__tori__}/baz"
19
+ end
11
20
  end
12
21
 
13
22
  class Uploader
@@ -18,6 +27,8 @@ class TestToriDefine < Test::Unit::TestCase
18
27
  assert_respond_to Dammy.new, :test_image=
19
28
  assert_respond_to Dammy.new, :def_image
20
29
  assert_respond_to Dammy.new, :def_image=
30
+ assert_respond_to Dammy.new, :custom
31
+ assert_respond_to Dammy.new, :custom=
21
32
  end
22
33
 
23
34
  test "defined methods" do
@@ -25,11 +36,20 @@ class TestToriDefine < Test::Unit::TestCase
25
36
  assert_instance_of Tori::File, dammy.test_image
26
37
  assert_instance_of Uploader, dammy.test_image = Uploader.new
27
38
  assert_instance_of Tori::File, dammy.test_image
39
+ assert_instance_of Tori::Backend::FileSystem, dammy.test_image.backend
40
+ assert_instance_of CustomBackend, dammy.custom.backend
41
+ end
42
+
43
+ test "defined method" do
44
+ dammy = Dammy.new
45
+ assert { false == dammy.def_image.exist? }
46
+ assert { "read:custom/baz" == dammy.custom.read }
28
47
  end
29
48
 
30
49
  test "define name" do
31
50
  dammy = Dammy.new
32
51
  assert_instance_of Tori::File, dammy.def_image
33
52
  assert { "foo/def_image/bar/piyo" == dammy.def_image.name }
53
+ assert { "custom/baz" == dammy.custom.name }
34
54
  end
35
55
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tori
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ksss
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-08 00:00:00.000000000 Z
11
+ date: 2015-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-core
@@ -130,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
130
  version: '0'
131
131
  requirements: []
132
132
  rubyforge_project:
133
- rubygems_version: 2.5.0
133
+ rubygems_version: 2.4.5.1
134
134
  signing_key:
135
135
  specification_version: 4
136
136
  summary: Simple file uploader