notebook 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -0
- data/lib/notebook.rb +11 -1
- data/lib/notebook/attachment.rb +1 -1
- data/lib/notebook/storage_adapters.rb +1 -0
- data/lib/notebook/storage_adapters/s3.rb +59 -0
- data/lib/notebook/version.rb +1 -1
- data/notebook.gemspec +1 -0
- metadata +16 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93d99b18119b52915e0cf4e3537fc47802cd19c4
|
4
|
+
data.tar.gz: 32b733d24e46eb657cc31b04055dd51f49b9927d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c53b194da80483921fa0595629de5d8a91b2befd0244f5fde87ffb7312c881a0fe9ded8215c275a25813b5abdc2fc4760877bc0fc0affe048f5eaf6dd2783146
|
7
|
+
data.tar.gz: fbe03a05fc9c7633b27d41e0a6ed4fd7de8055c8c500b017d0b1ce261fb4b58e27c60d2a2a2328563604d4d48ceb1c1d7f2ab67d1f778622eb3335d0d9dba263
|
data/README.md
CHANGED
@@ -79,6 +79,17 @@ To use a different storage location than /public, create a
|
|
79
79
|
Notebook.public_directory = 'hi'
|
80
80
|
```
|
81
81
|
|
82
|
+
To store your upload files in a different place than the filesystem,
|
83
|
+
create a `config/initializers/notebook.rb`, then set the following:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
# these are the two out of the box storage adapters
|
87
|
+
# to use one from another source, just place the constant of a
|
88
|
+
compliant adapter here.
|
89
|
+
Notebook.adapter = Notebook::StorageAdapters::Filesystem
|
90
|
+
Notebook.adapter = Notebook::StorageAdapters::S3
|
91
|
+
```
|
92
|
+
|
82
93
|
## Plain Old Ruby
|
83
94
|
|
84
95
|
It's awesome you want to use plain Ruby without Rails with Notebook!
|
data/lib/notebook.rb
CHANGED
@@ -7,12 +7,22 @@ module Notebook
|
|
7
7
|
autoload :Attachment, 'notebook/attachment'
|
8
8
|
autoload :StorageAdapters, 'notebook/storage_adapters'
|
9
9
|
|
10
|
+
# Return the current storage adapter in use
|
11
|
+
def self.adapter
|
12
|
+
@adapter || Notebook::StorageAdapters::Filesystem
|
13
|
+
end
|
14
|
+
|
15
|
+
# Set the storage adapter constant (ex: Notebook::StorageAdapters::Filesystem) to use
|
16
|
+
def self.adapter=(adapter)
|
17
|
+
@adapter = adapter
|
18
|
+
end
|
19
|
+
|
10
20
|
# Return the current filesystem storage directory in use
|
11
21
|
def self.public_directory
|
12
22
|
@public_directory || (Pathname.pwd + 'public')
|
13
23
|
end
|
14
24
|
|
15
|
-
# Set the
|
25
|
+
# Set the storage filesystem directory to use
|
16
26
|
def self.public_directory=(directory)
|
17
27
|
@public_directory = directory
|
18
28
|
end
|
data/lib/notebook/attachment.rb
CHANGED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'aws-sdk'
|
2
|
+
require 'securerandom'
|
3
|
+
|
4
|
+
module Notebook
|
5
|
+
# Return the current S3 options
|
6
|
+
def self.s3_options
|
7
|
+
@s3_options || {}
|
8
|
+
end
|
9
|
+
|
10
|
+
# Allow the user to specify S3 options
|
11
|
+
def self.s3_options=(s3_options)
|
12
|
+
@s3_options = s3_options
|
13
|
+
end
|
14
|
+
|
15
|
+
module StorageAdapters
|
16
|
+
class S3 < Base
|
17
|
+
attr_reader :s3_file_key
|
18
|
+
|
19
|
+
def initialize(attachment, options = {})
|
20
|
+
super
|
21
|
+
#--
|
22
|
+
check_valid_credentials
|
23
|
+
setup_s3
|
24
|
+
end
|
25
|
+
|
26
|
+
def delete
|
27
|
+
@bucket.object(@s3_file_key).delete
|
28
|
+
end
|
29
|
+
|
30
|
+
def upload
|
31
|
+
@s3_file_key = "#{SecureRandom.uuid}/#{File.basename(attachment.file)}"
|
32
|
+
@file_object = @bucket.object(@s3_file_key)
|
33
|
+
@file_object.upload_file(attachment.file.path)
|
34
|
+
end
|
35
|
+
|
36
|
+
def url
|
37
|
+
@bucket.object(@s3_file_key).presigned_url(:get)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def check_valid_credentials
|
43
|
+
credentials = Notebook.s3_options
|
44
|
+
required_credential_keys = [:access_key_id, :bucket, :region, :secret_access_key]
|
45
|
+
contains_credentials = required_credential_keys.all? { |k| credentials.key?(k) }
|
46
|
+
|
47
|
+
return if contains_credentials
|
48
|
+
fail 'Proper credentials are not defined'
|
49
|
+
end
|
50
|
+
|
51
|
+
def setup_s3
|
52
|
+
@s3_options = Notebook.s3_options
|
53
|
+
@credentials = Aws::Credentials.new(@s3_options[:access_key_id], @s3_options[:secret_access_key])
|
54
|
+
@s3 = Aws::S3::Resource.new(credentials: @credentials, region: @s3_options[:region])
|
55
|
+
@bucket = @s3.bucket(@s3_options[:bucket])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/lib/notebook/version.rb
CHANGED
data/notebook.gemspec
CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
20
|
spec.require_paths = ['lib']
|
21
21
|
|
22
|
+
spec.add_development_dependency 'aws-sdk'
|
22
23
|
spec.add_development_dependency 'bundler', '~> 1.10'
|
23
24
|
spec.add_development_dependency 'rails'
|
24
25
|
spec.add_development_dependency 'rake', '~> 10.0'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: notebook
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jon Moss
|
@@ -10,6 +10,20 @@ bindir: exe
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2015-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: aws-sdk
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -119,6 +133,7 @@ files:
|
|
119
133
|
- lib/notebook/storage_adapters.rb
|
120
134
|
- lib/notebook/storage_adapters/base.rb
|
121
135
|
- lib/notebook/storage_adapters/filesystem.rb
|
136
|
+
- lib/notebook/storage_adapters/s3.rb
|
122
137
|
- lib/notebook/version.rb
|
123
138
|
- notebook.gemspec
|
124
139
|
- roadmap.txt
|