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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b3b29fcabdf77d11419e6d32a2de2d043a477dee
4
- data.tar.gz: b858be60c3f58783f527a18f97505cd316406445
3
+ metadata.gz: 93d99b18119b52915e0cf4e3537fc47802cd19c4
4
+ data.tar.gz: 32b733d24e46eb657cc31b04055dd51f49b9927d
5
5
  SHA512:
6
- metadata.gz: 245f639d4f1a9e72c6b1f91a7b62424ba54d3b68103bbe042ae675bc8dc0e53c7415ebdfb80973435a9863a9fd45cd1acda8c05a000a31e53823e571d32dfe07
7
- data.tar.gz: 4b2dacc69de34a410005c9ce54a9636e1f964581154aa193ba2c17cedbfbdbe661af8efd48309807053259570baf787b6908dd253f16ed5d915bf26e0bd7df8b
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!
@@ -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 storage filesystem directory to use
25
+ # Set the storage filesystem directory to use
16
26
  def self.public_directory=(directory)
17
27
  @public_directory = directory
18
28
  end
@@ -21,7 +21,7 @@ module Notebook
21
21
  private
22
22
 
23
23
  def adapter
24
- StorageAdapters::Filesystem.new(self)
24
+ Notebook.adapter.new(self)
25
25
  end
26
26
  end
27
27
  end
@@ -2,5 +2,6 @@ module Notebook
2
2
  module StorageAdapters
3
3
  autoload :Base, 'notebook/storage_adapters/base'
4
4
  autoload :Filesystem, 'notebook/storage_adapters/filesystem'
5
+ autoload :S3, 'notebook/storage_adapters/s3'
5
6
  end
6
7
  end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Notebook
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -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.1.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