breadbox 0.0.1 → 0.9.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: 24eaaf7bf16cc0ee063a61ab043a41080fc035e4
4
- data.tar.gz: 8a43b4a5b7d283c7b9732b41426454ac8faecaa2
3
+ metadata.gz: 90f41d62df8c945f0e5daed62fd13f426ee8df0a
4
+ data.tar.gz: 359a0337bb8ce4943693637b4029ae56c8852e48
5
5
  SHA512:
6
- metadata.gz: f6264b060ed5e71b9734f616c8619bc601eb1fe62b81acdf30adb8547b3afa27d565573926b49386f021676f8ce92c81b19041b623165d94e6bda11ec1b52c42
7
- data.tar.gz: 1b26f6a994e1def0705739b89968b29ec8b199c3b7a263ae6f76ea54013c873013414cbb07ed2c1f291616a47d15d809cbd2f13bd149f4494d6b431b0446cff2
6
+ metadata.gz: cad5ed20ce72a87876fa9b4deacb15462b5c7102fe9569318e02eca64911873b27f98483ddfc7acbf1182fb93636f52df25e8d16e5353c087525489f79e66ea9
7
+ data.tar.gz: 3c2b44758c1eca95f72b2046f929d4c04aebcfa7dff8db994bcb9e2a1d81b0f7889a387e3a8665719a4ef38cebd17c2e1a6269ee3f087cba2aa4cd44235cdc6c
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/README.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # Breadbox
2
2
 
3
- TODO: Write a gem description
3
+ A simple wrapper interface for the [DropBox SDK gem](https://github.com/dropbox/dropbox-sdk-ruby).
4
+
5
+ ## Disclaimer
6
+
7
+ - This is a simple and fast implementation - Issues and PRs welcome.
8
+ - Currently tested on Ruby 2.1.2
4
9
 
5
10
  ## Installation
6
11
 
@@ -18,7 +23,52 @@ Or install it yourself as:
18
23
 
19
24
  ## Usage
20
25
 
21
- TODO: Write usage instructions here
26
+ ### 1. Get a [Dropbox Access Token](https://www.dropbox.com/developers/blog/94/generate-an-access-token-for-your-own-account)
27
+ ### 2. Add to your initializers:
28
+
29
+ ```ruby
30
+ # config/initializers/breadbox.rb
31
+
32
+ Breadbox.configure do |config|
33
+ config.dropbox_access_token = xxxxxxx # THIS IS REQUIRED
34
+ end
35
+ ```
36
+
37
+ ### 3. Configure your root directory for uploading files (Optional)
38
+
39
+ > By default - the root path will be the root directory of your DropBox folder.
40
+ You can, however, change the root path to be anything you want (inside of Dropbox).
41
+
42
+ **Note: You have to prefix the folder you wnt with a `/`, ex: `/uploads/my-files`**
43
+
44
+ ```ruby
45
+ # config/initializers/breadbox.rb
46
+
47
+ Breadbox.configure do |config|
48
+ config.dropbox_access_token = xxxxxxx # THIS IS REQUIRED
49
+ config.root_path = "/uploads/my-files"
50
+ end
51
+ ```
52
+
53
+ ### 4. Use it! :)
54
+
55
+ #### Parameters:
56
+
57
+ - `path`: defaults to `nil`, but this is where you put a custom folder if you so wish (in relation
58
+ to your `root_path`, which if you didn't configure in your initializer, will be your root Dropbox
59
+ folder `/`.
60
+ - `file`: The file object that you are uploading, ex: `file = File.open('./path-to-local-file').
61
+ - `cleanup`: defaults to `false`, but if you pass `true` - it will remove the local file after uploading
62
+ to DropBox
63
+
64
+ ```ruby
65
+ # to upload a file to Dropbox/uploads/my-cool-file.jpg
66
+ # and remove it after upload
67
+
68
+ file = File.open("./tmp/my-cool-file.jpg")
69
+ Breadbox.upload(path: "uploads", file: file, cleanup: true)
70
+ ```
71
+
22
72
 
23
73
  ## Contributing
24
74
 
data/Rakefile CHANGED
@@ -1,2 +1,10 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
2
3
 
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ task :console do
9
+ exec "pry -r breadbox -I ./lib"
10
+ end
data/breadbox.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Nathaniel Watts"]
10
10
  spec.email = ["reg@nathanielwatts.com"]
11
11
  spec.summary = "A simple wrapper interface for the DropBox SDK gem."
12
- spec.description = "This gem is just to provide some niceties for interfacing with the DropBox SDK [https://github.com/dropbox/dropbox-sdk-ruby](https://github.com/dropbox/dropbox-sdk-ruby)."
12
+ spec.description = "This gem is just to provide some niceties for interfacing with the DropBox SDK."
13
13
  spec.homepage = "https://github.com/thewatts/breadbox"
14
14
  spec.license = "MIT"
15
15
 
@@ -18,6 +18,10 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "dropbox-sdk"
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.6"
22
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "pry"
23
27
  end
@@ -0,0 +1,35 @@
1
+ require "dropbox_sdk"
2
+
3
+ module Breadbox
4
+ class Client
5
+ attr_reader :client, :root_path, :token
6
+
7
+ def initialize(options = {})
8
+ @token = options[:token]
9
+ @root_path = options[:root_path]
10
+ @client = new_client_from_token
11
+ end
12
+
13
+ def upload(path: nil, file: nil)
14
+ filepath = filepath_from_paths_and_file(root_path, path, file)
15
+ client.put_file(filepath, file)
16
+ end
17
+
18
+ protected
19
+
20
+ def filepath_from_paths_and_file(root_path, path, file)
21
+ filename = File.basename(file)
22
+ [root_path, path, filename].join("/").gsub(/\/{2,}/, '/')
23
+ end
24
+
25
+ def new_client_from_token
26
+ if token.nil? || token.empty?
27
+ raise MissingAccessToken, "You need a Dropbox Access Token."
28
+ else
29
+ DropboxClient.new(token)
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ class MissingAccessToken < Exception; end
@@ -0,0 +1,15 @@
1
+ module Breadbox
2
+ class Configuration
3
+ attr_accessor :dropbox_access_token, :root_path
4
+
5
+ def initialize
6
+ @root_path = default_root_path
7
+ end
8
+
9
+ protected
10
+
11
+ def default_root_path
12
+ "/"
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module Breadbox
2
- VERSION = "0.0.1"
2
+ VERSION = "0.9.0"
3
3
  end
data/lib/breadbox.rb CHANGED
@@ -1,5 +1,46 @@
1
1
  require "breadbox/version"
2
+ require "breadbox/configuration"
3
+ require "breadbox/client"
4
+
5
+ begin
6
+ require "pry"
7
+ rescue LoadError
8
+ end
2
9
 
3
10
  module Breadbox
4
- # Your code goes here...
11
+ class << self
12
+ attr_writer :configuration
13
+ end
14
+
15
+ def self.cleanup(file: nil, cleanup: true)
16
+ if cleanup && File.exists?(file)
17
+ File.delete(file)
18
+ end
19
+ end
20
+
21
+ def self.client
22
+ @client ||= Client.new(
23
+ root_path: configuration.root_path,
24
+ token: configuration.dropbox_access_token,
25
+ )
26
+ end
27
+
28
+ def self.configuration
29
+ @configuration ||= Configuration.new
30
+ end
31
+
32
+ def self.configure
33
+ yield(configuration)
34
+ end
35
+
36
+ def self.reset
37
+ @client = nil
38
+ @configuration = Configuration.new
39
+ end
40
+
41
+ def self.upload(path: nil, file: nil, cleanup: false)
42
+ if client.upload(path: path, file: file)
43
+ cleanup(file: file, cleanup: cleanup)
44
+ end
45
+ end
5
46
  end
@@ -0,0 +1,43 @@
1
+ require "spec_helper"
2
+
3
+ module Breadbox
4
+ describe Client do
5
+ it "should be a Dropbox client" do
6
+ client = Client.new(token: "12345").client
7
+ expect(client).to be_kind_of DropboxClient
8
+ end
9
+
10
+ it "should raise an exception if token is missing" do
11
+ expect{ Client.new }.to raise_exception(MissingAccessToken)
12
+ end
13
+
14
+ it "should raise an exception if the token is empty" do
15
+ expect{ Client.new(token: "") }.to raise_exception(MissingAccessToken)
16
+ end
17
+
18
+ describe "#upload" do
19
+ before { FileUtils.touch("./tmp/new-file.jpg") }
20
+ after { File.delete("./tmp/new-file.jpg") }
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)
30
+ end
31
+
32
+ it "tells the client to put_file from a full_filepath and file" do
33
+ file = File.open("./tmp/new-file.jpg")
34
+ client = Client.new(token: "12345", root_path: "/")
35
+ dropbox_client = instance_double(DropboxClient)
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)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,37 @@
1
+ require "spec_helper"
2
+
3
+ module Breadbox
4
+ describe Configuration do
5
+ describe "#root_path" do
6
+ it "defaults to /breadbox" do
7
+ config = Configuration.new
8
+ expect(config.root_path).to eq "/"
9
+ end
10
+ end
11
+
12
+ describe "#root_path=" do
13
+ it "assigns a root directory" do
14
+ config = Configuration.new
15
+ new_dir = "/my-favorite-directory"
16
+ config.root_path = new_dir
17
+ expect(config.root_path).to eq new_dir
18
+ end
19
+ end
20
+
21
+ describe "#dropbox_access_token" do
22
+ it "defaults to nil" do
23
+ config = Configuration.new
24
+ expect(config.dropbox_access_token).to eq nil
25
+ end
26
+ end
27
+
28
+ describe "#dropbox_access_token=" do
29
+ it "assigns the access token" do
30
+ config = Configuration.new
31
+ access_token = "12345"
32
+ config.dropbox_access_token = access_token
33
+ expect(config.dropbox_access_token).to eq access_token
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,102 @@
1
+ require "spec_helper"
2
+
3
+ describe Breadbox do
4
+ describe "::configure" do
5
+ before do
6
+ Breadbox.configure do |config|
7
+ config.dropbox_access_token = "12345"
8
+ config.root_path = "/my-new-root"
9
+ end
10
+ end
11
+
12
+ after { Breadbox.reset }
13
+
14
+ it "returns the newly assigned token" do
15
+ expect(Breadbox.configuration.dropbox_access_token).to eq "12345"
16
+ end
17
+
18
+ it "returns the newly assigned root" do
19
+ expect(Breadbox.configuration.root_path).to eq "/my-new-root"
20
+ end
21
+ end
22
+
23
+ describe "::client" do
24
+ before do
25
+ Breadbox.configure do |config|
26
+ config.dropbox_access_token = "12345"
27
+ end
28
+ end
29
+
30
+ after { Breadbox.reset }
31
+
32
+ it "assigns the token to a new Client" do
33
+ client = Breadbox.client
34
+ expect(client).to be_kind_of Breadbox::Client
35
+ end
36
+ end
37
+
38
+ describe "::upload" do
39
+ let!(:filepath) { "./tmp/my-new-file.jpg" }
40
+ let(:client) { instance_double(Breadbox::Client) }
41
+
42
+ before(:each) do
43
+ FileUtils.touch(filepath)
44
+ allow(Breadbox).to receive(:client).and_return(client)
45
+ end
46
+
47
+ after { File.delete(filepath) }
48
+
49
+ it "tells the client the #upload" do
50
+ file = File.open(filepath)
51
+ params = {
52
+ path: "/my-folder",
53
+ file: file,
54
+ }
55
+
56
+ allow(Breadbox).to receive(:client).and_return(client)
57
+ expect(client).to receive(:upload).with(params)
58
+
59
+ Breadbox.upload(params)
60
+ end
61
+
62
+ it "calls cleanup after upload" do
63
+ file = File.open(filepath)
64
+
65
+ allow(Breadbox).to receive(:client).and_return(client)
66
+ allow(client).to receive(:upload).and_return(true)
67
+ expect(Breadbox).to receive(:cleanup).with(file: file, cleanup: true)
68
+
69
+ Breadbox.upload(file: file, cleanup: true)
70
+ end
71
+ end
72
+
73
+ describe "::cleanup" do
74
+ let!(:filepath) { "./tmp/my-new-file.jpg" }
75
+
76
+ before(:each) do
77
+ FileUtils.touch(filepath)
78
+ end
79
+
80
+ after { File.delete(filepath) if File.exists?(filepath) }
81
+
82
+ context "with cleanup flag as false" do
83
+ it "doesn't delete the file" do
84
+ file = File.open(filepath)
85
+ expect(File.exists?(filepath)).to be true
86
+ Breadbox.cleanup(file: file, cleanup: false)
87
+
88
+ expect(File.exists?(filepath)).to be true
89
+ end
90
+ end
91
+
92
+ context "with cleanup as true" do
93
+ it "deletes the file" do
94
+ file = File.open(filepath)
95
+ expect(File.exists?(filepath)).to be true
96
+ Breadbox.cleanup(file: file, cleanup: true)
97
+
98
+ expect(File.exists?(filepath)).to be false
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,79 @@
1
+ require "breadbox"
2
+ # This file was generated by the `rspec --init` command. Conventionally, all
3
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
4
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
5
+ # file to always be loaded, without a need to explicitly require it in any files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, make a
11
+ # separate helper file that requires this one and then use it only in the specs
12
+ # that actually need it.
13
+ #
14
+ # The `.rspec` file also contains a few flags that are not defaults but that
15
+ # users commonly want.
16
+ #
17
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
18
+ RSpec.configure do |config|
19
+ # The settings below are suggested to provide a good initial experience
20
+ # with RSpec, but feel free to customize to your heart's content.
21
+ =begin
22
+ # These two settings work together to allow you to limit a spec run
23
+ # to individual examples or groups you care about by tagging them with
24
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
25
+ # get run.
26
+ config.filter_run :focus
27
+ config.run_all_when_everything_filtered = true
28
+
29
+ # Many RSpec users commonly either run the entire suite or an individual
30
+ # file, and it's useful to allow more verbose output when running an
31
+ # individual spec file.
32
+ if config.files_to_run.one?
33
+ # Use the documentation formatter for detailed output,
34
+ # unless a formatter has already been configured
35
+ # (e.g. via a command-line flag).
36
+ config.default_formatter = 'doc'
37
+ end
38
+
39
+ # Print the 10 slowest examples and example groups at the
40
+ # end of the spec run, to help surface which specs are running
41
+ # particularly slow.
42
+ config.profile_examples = 10
43
+
44
+ # Run specs in random order to surface order dependencies. If you find an
45
+ # order dependency and want to debug it, you can fix the order by providing
46
+ # the seed, which is printed after each run.
47
+ # --seed 1234
48
+ config.order = :random
49
+
50
+ # Seed global randomization in this process using the `--seed` CLI option.
51
+ # Setting this allows you to use `--seed` to deterministically reproduce
52
+ # test failures related to randomization by passing the same `--seed` value
53
+ # as the one that triggered the failure.
54
+ Kernel.srand config.seed
55
+
56
+ # rspec-expectations config goes here. You can use an alternate
57
+ # assertion/expectation library such as wrong or the stdlib/minitest
58
+ # assertions if you prefer.
59
+ config.expect_with :rspec do |expectations|
60
+ # Enable only the newer, non-monkey-patching expect syntax.
61
+ # For more details, see:
62
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
63
+ expectations.syntax = :expect
64
+ end
65
+
66
+ # rspec-mocks config goes here. You can use an alternate test double
67
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
68
+ config.mock_with :rspec do |mocks|
69
+ # Enable only the newer, non-monkey-patching expect syntax.
70
+ # For more details, see:
71
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
72
+ mocks.syntax = :expect
73
+
74
+ # Prevents you from mocking or stubbing a method that does not exist on
75
+ # a real object. This is generally recommended.
76
+ mocks.verify_partial_doubles = true
77
+ end
78
+ =end
79
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: breadbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.9.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-08-06 00:00:00.000000000 Z
11
+ date: 2014-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dropbox-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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
@@ -38,8 +52,36 @@ dependencies:
38
52
  - - ">="
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
41
83
  description: This gem is just to provide some niceties for interfacing with the DropBox
42
- SDK [https://github.com/dropbox/dropbox-sdk-ruby](https://github.com/dropbox/dropbox-sdk-ruby).
84
+ SDK.
43
85
  email:
44
86
  - reg@nathanielwatts.com
45
87
  executables: []
@@ -47,13 +89,20 @@ extensions: []
47
89
  extra_rdoc_files: []
48
90
  files:
49
91
  - ".gitignore"
92
+ - ".rspec"
50
93
  - Gemfile
51
94
  - LICENSE.txt
52
95
  - README.md
53
96
  - Rakefile
54
97
  - breadbox.gemspec
55
98
  - lib/breadbox.rb
99
+ - lib/breadbox/client.rb
100
+ - lib/breadbox/configuration.rb
56
101
  - lib/breadbox/version.rb
102
+ - spec/breadbox/client_spec.rb
103
+ - spec/breadbox/configuration_spec.rb
104
+ - spec/breadbox_spec.rb
105
+ - spec/spec_helper.rb
57
106
  homepage: https://github.com/thewatts/breadbox
58
107
  licenses:
59
108
  - MIT
@@ -78,5 +127,9 @@ rubygems_version: 2.2.2
78
127
  signing_key:
79
128
  specification_version: 4
80
129
  summary: A simple wrapper interface for the DropBox SDK gem.
81
- test_files: []
130
+ test_files:
131
+ - spec/breadbox/client_spec.rb
132
+ - spec/breadbox/configuration_spec.rb
133
+ - spec/breadbox_spec.rb
134
+ - spec/spec_helper.rb
82
135
  has_rdoc: