breadbox 0.9.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +30 -9
- data/breadbox.gemspec +4 -3
- data/lib/breadbox/client.rb +25 -18
- data/lib/breadbox/client_builder.rb +34 -0
- data/lib/breadbox/configuration.rb +24 -1
- data/lib/breadbox/dropbox_client.rb +37 -0
- data/lib/breadbox/null_configuration.rb +11 -0
- data/lib/breadbox/s3_client.rb +57 -0
- data/lib/breadbox/version.rb +1 -1
- data/lib/breadbox.rb +3 -4
- data/spec/breadbox/client_builder_spec.rb +61 -0
- data/spec/breadbox/client_spec.rb +20 -29
- data/spec/breadbox/configuration_spec.rb +68 -4
- data/spec/breadbox/dropbox_client_spec.rb +67 -0
- data/spec/breadbox/s3_client_spec.rb +86 -0
- data/spec/breadbox_spec.rb +3 -0
- data/spec/spec_helper.rb +2 -1
- metadata +30 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2fc8f406d6187e2707ac993a2fd6d5d4e7ad8d56
|
4
|
+
data.tar.gz: 6c05dbdaa8a962b0489350230aa445765913e188
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1104d3d4ea17ccba451682038a59041c9e566960733f52cfae89c41e252250f6019f56fb836aabb7fca7bcb0508c17b227d619c5d1a8fe499290afc3f1cc9232
|
7
|
+
data.tar.gz: 9b64da8f50ac9cb7d6bede77baec4dc1d9000ffe26efd9c018694968ea9ce115a1129a266fabfa56eae59ef54945bd867d3103bf5e8e0f9a62e4c3bdf73e8b03
|
data/README.md
CHANGED
@@ -21,7 +21,8 @@ Or install it yourself as:
|
|
21
21
|
|
22
22
|
$ gem install breadbox
|
23
23
|
|
24
|
-
|
24
|
+
---
|
25
|
+
## Setup for Dropbox
|
25
26
|
|
26
27
|
### 1. Get a [Dropbox Access Token](https://www.dropbox.com/developers/blog/94/generate-an-access-token-for-your-own-account)
|
27
28
|
### 2. Add to your initializers:
|
@@ -31,28 +32,48 @@ Or install it yourself as:
|
|
31
32
|
|
32
33
|
Breadbox.configure do |config|
|
33
34
|
config.dropbox_access_token = xxxxxxx # THIS IS REQUIRED
|
35
|
+
config.provider = :dropbox # THIS IS REQUIRED
|
34
36
|
end
|
35
37
|
```
|
36
38
|
|
37
|
-
|
39
|
+
--
|
40
|
+
## Setup for S3
|
41
|
+
|
42
|
+
### 1. Get your [AWS Credentials](http://infinitewp.com/knowledge-base/where-are-my-amazon-s3-credentials/)
|
43
|
+
### 2. [Create or find a bucket](http://docs.aws.amazon.com/AmazonS3/latest/gsg/CreatingABucket.html)
|
44
|
+
### 3. Add to your initializers
|
38
45
|
|
39
|
-
|
40
|
-
|
46
|
+
```ruby
|
47
|
+
# config/initializers/breadbox.rb
|
41
48
|
|
42
|
-
|
49
|
+
Breadbox.configure do |config|
|
50
|
+
config.s3_bucket = "name of the bucket" # THIS IS REQUIRED
|
51
|
+
config.s3_secret_access_key = xxxxxx # THIS IS REQUIRED
|
52
|
+
config.s3_access_key_id = xxxxxxx # THIS IS REQUIRED
|
53
|
+
config.provider = :s3 # THIS IS REQUIRED
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
### (Optional) Configure your root directory for uploading files
|
58
|
+
|
59
|
+
> By default - the root path will be the root directory of your [DropBox folder or S3 Bucket].
|
60
|
+
You can, however, change the root path to be anything you want.
|
61
|
+
|
62
|
+
**Note: You have to prefix the folder you want with a `/`, ex: `/uploads/my-files`**
|
43
63
|
|
44
64
|
```ruby
|
45
65
|
# config/initializers/breadbox.rb
|
46
66
|
|
47
67
|
Breadbox.configure do |config|
|
48
|
-
config.dropbox_access_token = xxxxxxx # THIS IS REQUIRED
|
49
68
|
config.root_path = "/uploads/my-files"
|
69
|
+
|
70
|
+
# ... more configurations ...
|
50
71
|
end
|
51
72
|
```
|
52
73
|
|
53
|
-
|
74
|
+
## Usage
|
54
75
|
|
55
|
-
|
76
|
+
### Parameters:
|
56
77
|
|
57
78
|
- `path`: defaults to `nil`, but this is where you put a custom folder if you so wish (in relation
|
58
79
|
to your `root_path`, which if you didn't configure in your initializer, will be your root Dropbox
|
@@ -62,7 +83,7 @@ end
|
|
62
83
|
to DropBox
|
63
84
|
|
64
85
|
```ruby
|
65
|
-
# to upload a file to Dropbox/uploads/my-cool-file.jpg
|
86
|
+
# to upload a file to [Dropbox Folder or S3 Bucket]/uploads/my-cool-file.jpg
|
66
87
|
# and remove it after upload
|
67
88
|
|
68
89
|
file = File.open("./tmp/my-cool-file.jpg")
|
data/breadbox.gemspec
CHANGED
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = Breadbox::VERSION
|
9
9
|
spec.authors = ["Nathaniel Watts"]
|
10
10
|
spec.email = ["reg@nathanielwatts.com"]
|
11
|
-
spec.summary = "
|
12
|
-
spec.description = "This gem is just to provide some niceties for
|
13
|
-
spec.homepage = "https://github.com/ovenbits"
|
11
|
+
spec.summary = "An interface for uploading to Dropbox or Amazon S3."
|
12
|
+
spec.description = "This gem is just to provide some niceties for uploading with the DropBox and Amazon SDKs."
|
13
|
+
spec.homepage = "https://github.com/ovenbits/breadbox"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_dependency "dropbox-sdk", "~> 1.6"
|
22
|
+
spec.add_dependency "aws-sdk"
|
22
23
|
|
23
24
|
spec.add_development_dependency "bundler", "~> 1.6"
|
24
25
|
spec.add_development_dependency "rake"
|
data/lib/breadbox/client.rb
CHANGED
@@ -1,35 +1,42 @@
|
|
1
|
-
require "
|
1
|
+
require "breadbox/null_configuration"
|
2
2
|
|
3
3
|
module Breadbox
|
4
4
|
class Client
|
5
|
-
attr_reader :
|
5
|
+
attr_reader :configuration
|
6
6
|
|
7
|
-
def initialize(
|
8
|
-
@
|
9
|
-
|
10
|
-
@client = new_client_from_token
|
7
|
+
def initialize(configuration = nil)
|
8
|
+
@configuration = configuration || NullConfiguration.new
|
9
|
+
post_initialize
|
11
10
|
end
|
12
11
|
|
13
|
-
def
|
14
|
-
|
15
|
-
|
12
|
+
def client
|
13
|
+
@client ||= new_client_from_configuration
|
14
|
+
end
|
15
|
+
|
16
|
+
def root_path
|
17
|
+
configuration.root_path
|
18
|
+
end
|
19
|
+
|
20
|
+
def upload(*)
|
21
|
+
not_implemented
|
16
22
|
end
|
17
23
|
|
18
24
|
protected
|
19
25
|
|
20
26
|
def filepath_from_paths_and_file(root_path, path, file)
|
21
27
|
filename = File.basename(file)
|
22
|
-
[root_path, path, filename].join("/").gsub(/\/{2,}/,
|
28
|
+
[root_path, path, filename].join("/").gsub(/\/{2,}/, "/")
|
23
29
|
end
|
24
30
|
|
25
|
-
def
|
26
|
-
|
27
|
-
raise MissingAccessToken, "You need a Dropbox Access Token."
|
28
|
-
else
|
29
|
-
DropboxClient.new(token)
|
30
|
-
end
|
31
|
+
def new_client_from_configuration
|
32
|
+
not_implemented
|
31
33
|
end
|
34
|
+
|
35
|
+
def not_implemented
|
36
|
+
raise NotImplementedError,
|
37
|
+
"has not been implemented on #{ self.class }"
|
38
|
+
end
|
39
|
+
|
40
|
+
def post_initialize; end
|
32
41
|
end
|
33
42
|
end
|
34
|
-
|
35
|
-
class MissingAccessToken < Exception; end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "breadbox/dropbox_client"
|
2
|
+
require "breadbox/s3_client"
|
3
|
+
require "breadbox/null_configuration"
|
4
|
+
|
5
|
+
module Breadbox
|
6
|
+
class ClientBuilder
|
7
|
+
attr_reader :configuration
|
8
|
+
|
9
|
+
def initialize(configuration = nil)
|
10
|
+
@configuration = configuration || null_configuration
|
11
|
+
end
|
12
|
+
|
13
|
+
def build
|
14
|
+
case configuration.provider
|
15
|
+
when :dropbox
|
16
|
+
DropboxClient.new(configuration)
|
17
|
+
when :s3
|
18
|
+
S3Client.new(configuration)
|
19
|
+
else
|
20
|
+
null_configuration.provider
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.build(configuration)
|
25
|
+
new(configuration).build
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def null_configuration
|
31
|
+
NullConfiguration.new
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -1,15 +1,38 @@
|
|
1
1
|
module Breadbox
|
2
2
|
class Configuration
|
3
|
-
attr_accessor :
|
3
|
+
attr_accessor :s3_bucket,
|
4
|
+
:s3_access_key_id,
|
5
|
+
:s3_secret_access_key,
|
6
|
+
:dropbox_access_token,
|
7
|
+
:provider,
|
8
|
+
:root_path
|
4
9
|
|
5
10
|
def initialize
|
6
11
|
@root_path = default_root_path
|
7
12
|
end
|
8
13
|
|
14
|
+
def provider=(candidate)
|
15
|
+
if valid_provider?(candidate)
|
16
|
+
@provider = candidate
|
17
|
+
else
|
18
|
+
raise InvalidBreadboxProvider, "must use a valid provider."
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
9
22
|
protected
|
10
23
|
|
11
24
|
def default_root_path
|
12
25
|
"/"
|
13
26
|
end
|
27
|
+
|
28
|
+
def valid_provider?(candidate)
|
29
|
+
valid_providers.include?(candidate.to_sym)
|
30
|
+
end
|
31
|
+
|
32
|
+
def valid_providers
|
33
|
+
[:s3, :dropbox]
|
34
|
+
end
|
14
35
|
end
|
15
36
|
end
|
37
|
+
|
38
|
+
class InvalidBreadboxProvider < Exception; end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "dropbox_sdk"
|
2
|
+
require "breadbox/client"
|
3
|
+
|
4
|
+
module Breadbox
|
5
|
+
class DropboxClient < Client
|
6
|
+
def access_token
|
7
|
+
configuration.dropbox_access_token
|
8
|
+
end
|
9
|
+
|
10
|
+
def upload(path: nil, file: nil)
|
11
|
+
filepath = filepath_from_paths_and_file(root_path, path, file)
|
12
|
+
client.put_file(filepath, file)
|
13
|
+
end
|
14
|
+
|
15
|
+
protected
|
16
|
+
|
17
|
+
def missing_access_token
|
18
|
+
raise MissingDropboxAccessToken, "You need a Dropbox Access Token."
|
19
|
+
end
|
20
|
+
|
21
|
+
def new_client_from_configuration
|
22
|
+
::DropboxClient.new(access_token)
|
23
|
+
end
|
24
|
+
|
25
|
+
def post_initialize
|
26
|
+
validate_access_token
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate_access_token
|
30
|
+
if configuration.dropbox_access_token.to_s.empty?
|
31
|
+
missing_access_token
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class MissingDropboxAccessToken < Exception; end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "aws"
|
2
|
+
require "breadbox/client"
|
3
|
+
|
4
|
+
module Breadbox
|
5
|
+
class S3Client < Client
|
6
|
+
def bucket
|
7
|
+
configuration.s3_bucket
|
8
|
+
end
|
9
|
+
|
10
|
+
def s3_bucket_object
|
11
|
+
@bucket ||= AWS::S3.new.buckets[bucket]
|
12
|
+
end
|
13
|
+
|
14
|
+
def upload(path: nil, file: nil)
|
15
|
+
filepath = filepath_from_paths_and_file(root_path, path, file)[1..-1]
|
16
|
+
s3_bucket_object.objects[filepath].write(file)
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def post_initialize
|
22
|
+
validate_bucket
|
23
|
+
validate_tokens
|
24
|
+
setup_s3
|
25
|
+
end
|
26
|
+
|
27
|
+
def setup_s3
|
28
|
+
AWS.config(
|
29
|
+
access_key_id: configuration.s3_access_key_id,
|
30
|
+
secret_access_key: configuration.s3_secret_access_key
|
31
|
+
)
|
32
|
+
end
|
33
|
+
|
34
|
+
def validate_bucket
|
35
|
+
if configuration.s3_bucket.to_s.empty?
|
36
|
+
raise MissingS3Bucket,
|
37
|
+
"You need to provide an AWS::S3 Bucket"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def validate_tokens
|
42
|
+
if configuration.s3_access_key_id.to_s.empty?
|
43
|
+
raise MissingS3AccessKeyId,
|
44
|
+
"You need to provide an AWS::S3 Access Key ID"
|
45
|
+
end
|
46
|
+
|
47
|
+
if configuration.s3_secret_access_key.to_s.empty?
|
48
|
+
raise MissingS3SecretAccessKey,
|
49
|
+
"You need to provide an AWS::S3 Secret Access Key"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class MissingS3AccessKeyId < Exception; end
|
56
|
+
class MissingS3Bucket < Exception; end
|
57
|
+
class MissingS3SecretAccessKey < Exception; end
|
data/lib/breadbox/version.rb
CHANGED
data/lib/breadbox.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "breadbox/version"
|
2
2
|
require "breadbox/configuration"
|
3
3
|
require "breadbox/client"
|
4
|
+
require "breadbox/client_builder"
|
4
5
|
|
5
6
|
begin
|
6
7
|
require "pry"
|
@@ -19,10 +20,7 @@ module Breadbox
|
|
19
20
|
end
|
20
21
|
|
21
22
|
def self.client
|
22
|
-
@client ||=
|
23
|
-
root_path: configuration.root_path,
|
24
|
-
token: configuration.dropbox_access_token,
|
25
|
-
)
|
23
|
+
@client ||= ClientBuilder.build(configuration)
|
26
24
|
end
|
27
25
|
|
28
26
|
def self.configuration
|
@@ -41,6 +39,7 @@ module Breadbox
|
|
41
39
|
def self.upload(path: nil, file: nil, cleanup: false)
|
42
40
|
if client.upload(path: path, file: file)
|
43
41
|
cleanup(file: file, cleanup: cleanup)
|
42
|
+
true
|
44
43
|
end
|
45
44
|
end
|
46
45
|
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "./lib/breadbox/client_builder"
|
3
|
+
require "./lib/breadbox/configuration"
|
4
|
+
|
5
|
+
module Breadbox
|
6
|
+
describe ClientBuilder do
|
7
|
+
let(:builder) { ClientBuilder.new(configuration) }
|
8
|
+
let(:configuration) { Breadbox::Configuration.new }
|
9
|
+
|
10
|
+
it "initializes with a configuration object" do
|
11
|
+
expect(builder.configuration).to eq configuration
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#build" do
|
15
|
+
let(:configuration) { Breadbox::Configuration.new }
|
16
|
+
|
17
|
+
context "for a dropbox client" do
|
18
|
+
before { configuration.provider = :dropbox }
|
19
|
+
|
20
|
+
it "will delegate to a Breadbox::DropBoxClient" do
|
21
|
+
expect(Breadbox::DropboxClient).to receive(:new).with(configuration)
|
22
|
+
builder.build
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "for an s3 client" do
|
27
|
+
before { configuration.provider = :s3 }
|
28
|
+
|
29
|
+
it "will delegate to a Breadbox::AwsClient" do
|
30
|
+
expect(Breadbox::S3Client).to receive(:new).with(configuration)
|
31
|
+
builder.build
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "configuration provider is not valid (nil, empty, etc)" do
|
36
|
+
it "will raise a MissingBreadboxProvider error" do
|
37
|
+
expect { builder.build }.to raise_error MissingBreadboxProvider
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "without a configuration" do
|
42
|
+
let(:configuration) { nil }
|
43
|
+
|
44
|
+
it "will raise a MissingBreadboxProvider error" do
|
45
|
+
expect { builder.build }.to raise_error MissingBreadboxProvider
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "::build" do
|
51
|
+
it "delegates to an instance of itself, and calls #build" do
|
52
|
+
builder = instance_double(ClientBuilder)
|
53
|
+
allow(ClientBuilder).to receive(:new).with(configuration)
|
54
|
+
.and_return(builder)
|
55
|
+
expect(builder).to receive(:build)
|
56
|
+
|
57
|
+
ClientBuilder.build(configuration)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -1,42 +1,33 @@
|
|
1
1
|
require "spec_helper"
|
2
|
+
require "./lib/breadbox/client"
|
2
3
|
|
3
4
|
module Breadbox
|
4
5
|
describe Client do
|
5
|
-
|
6
|
-
client = Client.new(token: "12345").client
|
7
|
-
expect(client).to be_kind_of DropboxClient
|
8
|
-
end
|
6
|
+
let(:configuration) { double }
|
9
7
|
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
describe "#initialize" do
|
9
|
+
it "initializes with a configuration" do
|
10
|
+
client = Client.new(configuration)
|
11
|
+
expect(client.configuration).to eq configuration
|
12
|
+
end
|
13
13
|
|
14
|
-
|
15
|
-
|
14
|
+
it "configuration defaults to a NullConfiguration" do
|
15
|
+
client = Client.new
|
16
|
+
expect(client.configuration).to be_kind_of NullConfiguration
|
17
|
+
end
|
16
18
|
end
|
17
19
|
|
18
|
-
describe "#
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
it "tells the client to put_file from a full_filepath and file" do
|
23
|
-
file = File.open("./tmp/new-file.jpg")
|
24
|
-
client = Client.new(token: "12345", root_path: "/")
|
25
|
-
dropbox_client = instance_double(DropboxClient)
|
26
|
-
allow(client).to receive(:client).and_return(dropbox_client)
|
27
|
-
expect(dropbox_client).to receive(:put_file).with("/new-file.jpg", file)
|
28
|
-
|
29
|
-
client.upload(path: "/", file: file)
|
20
|
+
describe "#client" do
|
21
|
+
it "is not defined on this class" do
|
22
|
+
client = Client.new
|
23
|
+
expect { client.client }.to raise_error NotImplementedError
|
30
24
|
end
|
25
|
+
end
|
31
26
|
|
32
|
-
|
33
|
-
|
34
|
-
client
|
35
|
-
|
36
|
-
allow(client).to receive(:client).and_return(dropbox_client)
|
37
|
-
expect(dropbox_client).to receive(:put_file).with("/images/new-file.jpg", file)
|
38
|
-
|
39
|
-
client.upload(path: "images", file: file)
|
27
|
+
describe "#upload" do
|
28
|
+
it "is not defined on this class" do
|
29
|
+
client = Client.new
|
30
|
+
expect { client.upload }.to raise_error NotImplementedError
|
40
31
|
end
|
41
32
|
end
|
42
33
|
end
|
@@ -1,37 +1,101 @@
|
|
1
1
|
require "spec_helper"
|
2
|
+
require "./lib/breadbox/configuration"
|
2
3
|
|
3
4
|
module Breadbox
|
4
5
|
describe Configuration do
|
6
|
+
let(:config) { Configuration.new }
|
7
|
+
|
5
8
|
describe "#root_path" do
|
6
9
|
it "defaults to /breadbox" do
|
7
|
-
config = Configuration.new
|
8
10
|
expect(config.root_path).to eq "/"
|
9
11
|
end
|
10
12
|
end
|
11
13
|
|
12
14
|
describe "#root_path=" do
|
13
15
|
it "assigns a root directory" do
|
14
|
-
config = Configuration.new
|
15
16
|
new_dir = "/my-favorite-directory"
|
16
17
|
config.root_path = new_dir
|
17
18
|
expect(config.root_path).to eq new_dir
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
22
|
+
describe "#s3_bucket" do
|
23
|
+
it "defaults to nil" do
|
24
|
+
expect(config.s3_bucket).to be nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#s3_access_key_id" do
|
29
|
+
it "defaults to nil" do
|
30
|
+
expect(config.s3_access_key_id).to be nil
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#s3_access_key_id=" do
|
35
|
+
it "assigns the bucket" do
|
36
|
+
access_key_id = "my-access_key_id"
|
37
|
+
config.s3_access_key_id = access_key_id
|
38
|
+
expect(config.s3_access_key_id).to eq access_key_id
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#s3_secret_access_key" do
|
43
|
+
it "defaults to nil" do
|
44
|
+
expect(config.s3_secret_access_key).to be nil
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "#s3_secret_access_key=" do
|
49
|
+
it "assigns the bucket" do
|
50
|
+
secret_access_key = "my-secret_access_key"
|
51
|
+
config.s3_secret_access_key = secret_access_key
|
52
|
+
expect(config.s3_secret_access_key).to eq secret_access_key
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#s3_bucket=" do
|
57
|
+
it "assigns the bucket" do
|
58
|
+
bucket = "my-bucket"
|
59
|
+
config.s3_bucket = bucket
|
60
|
+
expect(config.s3_bucket).to eq bucket
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
21
64
|
describe "#dropbox_access_token" do
|
22
65
|
it "defaults to nil" do
|
23
|
-
config = Configuration.new
|
24
66
|
expect(config.dropbox_access_token).to eq nil
|
25
67
|
end
|
26
68
|
end
|
27
69
|
|
28
70
|
describe "#dropbox_access_token=" do
|
29
71
|
it "assigns the access token" do
|
30
|
-
config = Configuration.new
|
31
72
|
access_token = "12345"
|
32
73
|
config.dropbox_access_token = access_token
|
33
74
|
expect(config.dropbox_access_token).to eq access_token
|
34
75
|
end
|
35
76
|
end
|
77
|
+
|
78
|
+
describe "#provider" do
|
79
|
+
it "defaults to nil" do
|
80
|
+
expect(config.provider).to eq nil
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "#provider=" do
|
85
|
+
it "assigns the provider" do
|
86
|
+
provider = :dropbox
|
87
|
+
config.provider = provider
|
88
|
+
expect(config.provider).to eq provider
|
89
|
+
end
|
90
|
+
|
91
|
+
it "requires the provider to be in the available providers" do
|
92
|
+
providers = [:aws, :dropbox]
|
93
|
+
allow(config).to receive(:available_providers).and_return(providers)
|
94
|
+
|
95
|
+
expect {
|
96
|
+
config.provider = :not_in_the_list
|
97
|
+
}.to raise_error InvalidBreadboxProvider
|
98
|
+
end
|
99
|
+
end
|
36
100
|
end
|
37
101
|
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "./lib/breadbox/configuration"
|
3
|
+
require "./lib/breadbox/dropbox_client"
|
4
|
+
|
5
|
+
module Breadbox
|
6
|
+
describe DropboxClient do
|
7
|
+
let(:configuration) { Breadbox::Configuration.new }
|
8
|
+
|
9
|
+
before { configuration.dropbox_access_token = "12345" }
|
10
|
+
|
11
|
+
it "is a Breadbox::Client" do
|
12
|
+
expect(DropboxClient).to be < Breadbox::Client
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "without valid configuration settings" do
|
16
|
+
it "raises a MissingDropboxAccessToken error without a token" do
|
17
|
+
configuration.dropbox_access_token = nil
|
18
|
+
expect {
|
19
|
+
DropboxClient.new(configuration)
|
20
|
+
}.to raise_error MissingDropboxAccessToken
|
21
|
+
end
|
22
|
+
|
23
|
+
it "raises a MissingDropboxAccessToken if configuration is nil" do
|
24
|
+
expect {
|
25
|
+
DropboxClient.new(nil)
|
26
|
+
}.to raise_error MissingDropboxAccessToken
|
27
|
+
end
|
28
|
+
|
29
|
+
it "raises a MissingDropboxAccessToken if not given a configuration" do
|
30
|
+
expect {
|
31
|
+
DropboxClient.new
|
32
|
+
}.to raise_error MissingDropboxAccessToken
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#client" do
|
37
|
+
it "is a ::DropboxClient (from the Dropbox SDK) client" do
|
38
|
+
client = DropboxClient.new(configuration).client
|
39
|
+
expect(client).to be_kind_of ::DropboxClient
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "#update" do
|
44
|
+
before { FileUtils.touch("./tmp/new-file.jpg") }
|
45
|
+
after { File.delete("./tmp/new-file.jpg") }
|
46
|
+
|
47
|
+
let(:access_token) { "12345" }
|
48
|
+
let(:configuration) {
|
49
|
+
config = Breadbox::Configuration.new
|
50
|
+
config.provider = :dropbox
|
51
|
+
config.dropbox_access_token = access_token
|
52
|
+
config.root_path = "/"
|
53
|
+
config
|
54
|
+
}
|
55
|
+
|
56
|
+
it "tells the client to put_file from a full_filepath and file" do
|
57
|
+
file = File.open("./tmp/new-file.jpg")
|
58
|
+
client = DropboxClient.new(configuration)
|
59
|
+
dropbox_client = instance_double(::DropboxClient)
|
60
|
+
allow(client).to receive(:client).and_return(dropbox_client)
|
61
|
+
expect(dropbox_client).to receive(:put_file).with("/new-file.jpg", file)
|
62
|
+
|
63
|
+
client.upload(path: "/", file: file)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "./lib/breadbox/s3_client"
|
3
|
+
require "./lib/breadbox/configuration"
|
4
|
+
|
5
|
+
module Breadbox
|
6
|
+
describe S3Client do
|
7
|
+
let(:configuration) { Breadbox::Configuration.new }
|
8
|
+
|
9
|
+
before {
|
10
|
+
configuration.s3_bucket = "my-bucket"
|
11
|
+
configuration.s3_access_key_id = "12345"
|
12
|
+
configuration.s3_secret_access_key = "abc123"
|
13
|
+
}
|
14
|
+
|
15
|
+
it "is a Breadbox::Client" do
|
16
|
+
expect(S3Client).to be < Breadbox::Client
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "without valid configuration settings" do
|
20
|
+
it "raises a MissingS3Bucket error without a bucket" do
|
21
|
+
configuration.s3_bucket = nil
|
22
|
+
expect {
|
23
|
+
S3Client.new(configuration)
|
24
|
+
}.to raise_error MissingS3Bucket
|
25
|
+
end
|
26
|
+
|
27
|
+
it "raises a MissingS3AccessKeyId error without a bucket" do
|
28
|
+
configuration.s3_access_key_id = nil
|
29
|
+
expect {
|
30
|
+
S3Client.new(configuration)
|
31
|
+
}.to raise_error MissingS3AccessKeyId
|
32
|
+
end
|
33
|
+
|
34
|
+
it "raises a MissingS3SecretAccessKey error without a bucket" do
|
35
|
+
configuration.s3_secret_access_key = nil
|
36
|
+
expect {
|
37
|
+
S3Client.new(configuration)
|
38
|
+
}.to raise_error MissingS3SecretAccessKey
|
39
|
+
end
|
40
|
+
|
41
|
+
it "raises a MissingS3Bucket if configuration is nil" do
|
42
|
+
expect {
|
43
|
+
S3Client.new(nil)
|
44
|
+
}.to raise_error MissingS3Bucket
|
45
|
+
end
|
46
|
+
|
47
|
+
it "raises a MissingS3Bucket if no configuration" do
|
48
|
+
expect {
|
49
|
+
S3Client.new
|
50
|
+
}.to raise_error MissingS3Bucket
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "with valid configuration settings" do
|
55
|
+
it "assigns the AccessKeyId and SecretAccessKey to AWS::S3" do
|
56
|
+
options = {
|
57
|
+
access_key_id: configuration.s3_access_key_id,
|
58
|
+
secret_access_key: configuration.s3_secret_access_key,
|
59
|
+
}
|
60
|
+
expect(AWS).to receive(:config).with(options)
|
61
|
+
S3Client.new(configuration)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#s3_bucket_object" do
|
66
|
+
it "returns a new bucket object from AWS::S3" do
|
67
|
+
client = S3Client.new(configuration)
|
68
|
+
bucket_object = client.s3_bucket_object
|
69
|
+
expect(bucket_object).to be_kind_of AWS::S3::Bucket
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "#upload" do
|
74
|
+
before { FileUtils.touch("./tmp/new-file.jpg") }
|
75
|
+
after { File.delete("./tmp/new-file.jpg") }
|
76
|
+
|
77
|
+
let(:client) { S3Client.new(configuration) }
|
78
|
+
|
79
|
+
it "writes a file to an S3 Bucket Object" do
|
80
|
+
file = File.open("./tmp/new-file.jpg")
|
81
|
+
expect_any_instance_of(AWS::S3::S3Object).to receive(:write).with(file)
|
82
|
+
client.upload(path: "/", file: file)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/spec/breadbox_spec.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "spec_helper"
|
2
|
+
require "./lib/breadbox"
|
2
3
|
|
3
4
|
describe Breadbox do
|
4
5
|
describe "::configure" do
|
@@ -6,6 +7,7 @@ describe Breadbox do
|
|
6
7
|
Breadbox.configure do |config|
|
7
8
|
config.dropbox_access_token = "12345"
|
8
9
|
config.root_path = "/my-new-root"
|
10
|
+
config.provider = :dropbox
|
9
11
|
end
|
10
12
|
end
|
11
13
|
|
@@ -24,6 +26,7 @@ describe Breadbox do
|
|
24
26
|
before do
|
25
27
|
Breadbox.configure do |config|
|
26
28
|
config.dropbox_access_token = "12345"
|
29
|
+
config.provider = :dropbox
|
27
30
|
end
|
28
31
|
end
|
29
32
|
|
data/spec/spec_helper.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
require "
|
1
|
+
require "pry"
|
2
|
+
|
2
3
|
# This file was generated by the `rspec --init` command. Conventionally, all
|
3
4
|
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
4
5
|
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: breadbox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathaniel Watts
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dropbox-sdk
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: aws-sdk
|
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'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,8 +94,8 @@ dependencies:
|
|
80
94
|
- - ">="
|
81
95
|
- !ruby/object:Gem::Version
|
82
96
|
version: '0'
|
83
|
-
description: This gem is just to provide some niceties for
|
84
|
-
|
97
|
+
description: This gem is just to provide some niceties for uploading with the DropBox
|
98
|
+
and Amazon SDKs.
|
85
99
|
email:
|
86
100
|
- reg@nathanielwatts.com
|
87
101
|
executables: []
|
@@ -97,13 +111,20 @@ files:
|
|
97
111
|
- breadbox.gemspec
|
98
112
|
- lib/breadbox.rb
|
99
113
|
- lib/breadbox/client.rb
|
114
|
+
- lib/breadbox/client_builder.rb
|
100
115
|
- lib/breadbox/configuration.rb
|
116
|
+
- lib/breadbox/dropbox_client.rb
|
117
|
+
- lib/breadbox/null_configuration.rb
|
118
|
+
- lib/breadbox/s3_client.rb
|
101
119
|
- lib/breadbox/version.rb
|
120
|
+
- spec/breadbox/client_builder_spec.rb
|
102
121
|
- spec/breadbox/client_spec.rb
|
103
122
|
- spec/breadbox/configuration_spec.rb
|
123
|
+
- spec/breadbox/dropbox_client_spec.rb
|
124
|
+
- spec/breadbox/s3_client_spec.rb
|
104
125
|
- spec/breadbox_spec.rb
|
105
126
|
- spec/spec_helper.rb
|
106
|
-
homepage: https://github.com/ovenbits
|
127
|
+
homepage: https://github.com/ovenbits/breadbox
|
107
128
|
licenses:
|
108
129
|
- MIT
|
109
130
|
metadata: {}
|
@@ -126,10 +147,13 @@ rubyforge_project:
|
|
126
147
|
rubygems_version: 2.2.2
|
127
148
|
signing_key:
|
128
149
|
specification_version: 4
|
129
|
-
summary:
|
150
|
+
summary: An interface for uploading to Dropbox or Amazon S3.
|
130
151
|
test_files:
|
152
|
+
- spec/breadbox/client_builder_spec.rb
|
131
153
|
- spec/breadbox/client_spec.rb
|
132
154
|
- spec/breadbox/configuration_spec.rb
|
155
|
+
- spec/breadbox/dropbox_client_spec.rb
|
156
|
+
- spec/breadbox/s3_client_spec.rb
|
133
157
|
- spec/breadbox_spec.rb
|
134
158
|
- spec/spec_helper.rb
|
135
159
|
has_rdoc:
|