tori 0.0.8 → 0.0.9

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: 2b19023a541895766bcd6cbc231febb4d6a5102b
4
- data.tar.gz: ee94e3e57dcee888c4eb09bdaee736e0277054b0
3
+ metadata.gz: 3e50f43aa37d6ed820e9cc05f6109098afa10a24
4
+ data.tar.gz: eee5148dcf9c70b91ade06ea3590d4b11aa54c8f
5
5
  SHA512:
6
- metadata.gz: 810f914e2fb306d90c8cee6f4c15eee2cd263c711b8f6faa584ff2dbd4aeccb3ccf084126f3b0be1ddfa5e6c2c0c01620a46e5d8f5da3a147053b7a49f4ba80c
7
- data.tar.gz: 049cd8f285bc2017d10a7aa3f0aaf5120973822cad8de5c780f0fb9cb0935b0890f8435df6fd3e10c047de627b00c6a02a363dc0a81100dd5915f2ab61f87eb6
6
+ metadata.gz: 62e61605a2a75da736cac9e063463ad95709fe01011677f3910ba371508f674ce4f822c463df7f1f0d4a3acdca446a7a317f31f1be6a1e4aafff92671649d02f
7
+ data.tar.gz: 5bf1624b4371d24b319a4f7b7ca9897a04d9f89cd37e4c2c08dff6be52fb6400984fe77c33f5a6648ee14c7298042c7231cdd75e60779921a788139724e76acd
data/README.md CHANGED
@@ -74,6 +74,48 @@ photo.image.exist? #=> exist check
74
74
  photo.image.name #=> filename
75
75
  ```
76
76
 
77
+ # Attach example
78
+
79
+ Two image file upload to backend example.
80
+ defined method by `tori` method can define a key name for each by block.
81
+
82
+ ```ruby
83
+ class Photo < ActiveRecord::Base
84
+ tori :original_image do |model|
85
+ "#{model.class}/original/#{model.original_filename}"
86
+ end
87
+
88
+ tori :striped_image do |model|
89
+ "#{model.class}/striped/#{model.striped_filename}"
90
+ end
91
+ end
92
+
93
+ class PhotoController < ApplicationController
94
+ def create
95
+ original = params[:file]
96
+ Tempfile.open("striped") { |striped|
97
+ # image processing example
98
+ MiniMagick::Tool::Convert.new { |c|
99
+ c.strip
100
+ c << original.path
101
+ c << striped.path
102
+ }
103
+
104
+ # create record
105
+ photo = Photo.create
106
+
107
+ # set image file to model
108
+ photo.original_image = original
109
+ photo.striped_image = striped
110
+
111
+ # write image file to backend
112
+ photo.original_image.write
113
+ photo.striped_image.write
114
+ }
115
+ end
116
+ ```
117
+
118
+
77
119
  # Custom configure example
78
120
 
79
121
  ```ruby
@@ -14,6 +14,7 @@ module Tori
14
14
  when Pathname
15
15
  # see also https://bugs.ruby-lang.org/issues/11199
16
16
  ::File.open(resource) { |src|
17
+ FileUtils.mkdir_p path(filename).dirname
17
18
  ::File.open(path(filename), 'w'){ |dst|
18
19
  ::IO.copy_stream src, dst
19
20
  }
@@ -1,8 +1,10 @@
1
1
  require 'aws-sdk-core'
2
+ require 'mime/types'
2
3
 
3
4
  module Tori
4
5
  module Backend
5
6
  class S3
7
+ DEFAULT_CONTENT_TYPE = 'text/plain'.freeze
6
8
  attr_accessor :bucket
7
9
  # Must be set bucket name.
8
10
  # And it use aws-sdk-core >= 2.0
@@ -17,12 +19,16 @@ module Tori
17
19
 
18
20
  def write(filename, resource)
19
21
  case resource
20
- when IO
21
- put filename, f
22
22
  when String
23
- put filename, resource
23
+ put_object key: filename, body: resource, content_type: DEFAULT_CONTENT_TYPE
24
+ when File, Pathname
25
+ path = resource.to_path
26
+ content_type = MIME::Types.type_for(path).first || DEFAULT_CONTENT_TYPE
27
+ ::File.open(path) { |f|
28
+ put_object key: filename, body: f, content_type: content_type.to_s, content_length: f.size
29
+ }
24
30
  else
25
- ::File.open(resource.to_path) { |f| put filename, f }
31
+ put_object key: filename, body: resource
26
32
  end
27
33
  end
28
34
 
@@ -70,20 +76,18 @@ module Tori
70
76
  signer.presigned_url(method, bucket: @bucket, key: filename)
71
77
  end
72
78
 
73
- private
74
-
75
79
  def client
76
80
  @client ||= if ENV["TORI_AWS_ACCESS_KEY_ID"] && ENV["TORI_AWS_SECRET_ACCESS_KEY"]
77
- Aws::S3::Client.new(
78
- access_key_id: ENV["TORI_AWS_ACCESS_KEY_ID"],
79
- secret_access_key: ENV["TORI_AWS_SECRET_ACCESS_KEY"],
80
- region: ENV["TORI_AWS_REGION"] || ENV['AWS_REGION'] || Aws.config[:region],
81
- )
82
- else
83
- Aws::S3::Client.new(
84
- region: ENV["TORI_AWS_REGION"] || ENV['AWS_REGION'] || Aws.config[:region]
85
- )
86
- end
81
+ Aws::S3::Client.new(
82
+ access_key_id: ENV["TORI_AWS_ACCESS_KEY_ID"],
83
+ secret_access_key: ENV["TORI_AWS_SECRET_ACCESS_KEY"],
84
+ region: ENV["TORI_AWS_REGION"] || ENV['AWS_REGION'] || Aws.config[:region],
85
+ )
86
+ else
87
+ Aws::S3::Client.new(
88
+ region: ENV["TORI_AWS_REGION"] || ENV['AWS_REGION'] || Aws.config[:region]
89
+ )
90
+ end
87
91
  end
88
92
 
89
93
  def get_object(key:)
@@ -98,8 +102,8 @@ module Tori
98
102
  client.head_bucket bucket: @bucket
99
103
  end
100
104
 
101
- def put_object(key:, body:)
102
- client.put_object bucket: @bucket, key: key, body: body
105
+ def put_object(opts = {})
106
+ client.put_object({bucket: @bucket}.merge(opts))
103
107
  end
104
108
 
105
109
  def delete_object(key:)
data/lib/tori/define.rb CHANGED
@@ -1,15 +1,15 @@
1
1
  module Tori
2
2
  module Define
3
- def tori(name)
3
+ def tori(name, &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))
8
+ ivar || instance_variable_set(name_ivar, File.new(self, &block))
9
9
  end
10
10
 
11
11
  define_method("#{name}=") do |uploader|
12
- file = File.new(self, from: uploader)
12
+ file = File.new(self, from: uploader, &block)
13
13
  instance_variable_set name_ivar, file
14
14
  end
15
15
  end
data/lib/tori/file.rb CHANGED
@@ -1,12 +1,17 @@
1
1
  module Tori
2
2
  class File
3
- def initialize(model, from: nil)
3
+ def initialize(model, from: nil, &block)
4
4
  @model = model
5
5
  @from = from
6
+ @filename_callback = block
6
7
  end
7
8
 
8
9
  def name
9
- Tori.config.filename_callback.call(@model)
10
+ if @filename_callback
11
+ @filename_callback.call(@model)
12
+ else
13
+ Tori.config.filename_callback.call(@model)
14
+ end
10
15
  end
11
16
  alias to_s name
12
17
 
data/lib/tori/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tori
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -33,6 +33,10 @@ class TestToriBackendS3 < Test::Unit::TestCase
33
33
 
34
34
  test "#write" do
35
35
  assert_nothing_raised { @backend.write("testfile", @testfile_path) }
36
+ testfile = @backend.get_object(key: "testfile")
37
+ assert { "plain/text" == testfile.content_type }
38
+ assert { 4 == testfile.content_length }
39
+ assert { "text" == testfile[:body].read }
36
40
  end
37
41
 
38
42
  test "#read" do
@@ -23,10 +23,12 @@ class TestToriFile < Test::Unit::TestCase
23
23
  test "#initialize" do
24
24
  assert_instance_of Tori::File, Tori::File.new(nil)
25
25
  assert_instance_of Tori::File, Tori::File.new(nil, from: nil)
26
+ assert_instance_of Tori::File, Tori::File.new(nil, from: nil) { }
26
27
  end
27
28
 
28
29
  test "#name" do
29
30
  assert { "test" == Tori::File.new("test").name }
31
+ assert { "String/test/sub" == Tori::File.new("test"){ |m| "#{m.class}/#{m}/sub"}.name }
30
32
  end
31
33
 
32
34
  test "#exist?" do
data/tori.gemspec CHANGED
@@ -17,8 +17,9 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
+ spec.add_runtime_dependency "aws-sdk-core"
21
+ spec.add_runtime_dependency "mime-types"
20
22
  spec.add_development_dependency "bundler"
21
23
  spec.add_development_dependency "rake"
22
24
  spec.add_development_dependency "test-unit"
23
- spec.add_development_dependency "aws-sdk-core", ">= 2.0"
24
25
  end
metadata CHANGED
@@ -1,23 +1,23 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tori
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - ksss
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-30 00:00:00.000000000 Z
11
+ date: 2015-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: aws-sdk-core
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
- type: :development
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
@@ -25,7 +25,21 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: mime-types
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
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - ">="
@@ -39,7 +53,7 @@ dependencies:
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
- name: test-unit
56
+ name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
59
  - - ">="
@@ -53,19 +67,19 @@ dependencies:
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
- name: aws-sdk-core
70
+ name: test-unit
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
73
  - - ">="
60
74
  - !ruby/object:Gem::Version
61
- version: '2.0'
75
+ version: '0'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
80
  - - ">="
67
81
  - !ruby/object:Gem::Version
68
- version: '2.0'
82
+ version: '0'
69
83
  description: Simple file uploader
70
84
  email:
71
85
  - co000ri@gmail.com