dropbox-explorer 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c6e9e1effd3121eb938c264564b7f5d2fc76cbcd
4
+ data.tar.gz: 7b0335a5f3d9c7005499bb286f100f231c4b5f8a
5
+ SHA512:
6
+ metadata.gz: a7aef77d6abbf224d3760c665e2bd47008c82745f448ee4387b395ae1f147f87ba628ec375a732237ace2585ffcefd8f03c72e491a5cbc2573fb35477bf794d0
7
+ data.tar.gz: 8e8759dd0ff4b736eafe2e9440e7390058c094bc5aeacdbca848810c183a6836e9c5138232cd3a6f0265ebf4de86550d8961cbbbf587d5c2f933baeb4885f4b7
@@ -0,0 +1,7 @@
1
+ .bundle
2
+ Gemfile.lock
3
+ pkg/
4
+ .rspec
5
+ spec/config/dropbox.yml
6
+ spec/config/app_folder.yml
7
+ spec/fixtures/vcr_cassettes
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dropbox-explorer.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem "pry"
8
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 davidcunha
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,68 @@
1
+ # Dropbox Explorer
2
+
3
+ Explore Dropbox folders and download files
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'dropbox-explorer'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install dropbox-explorer
20
+
21
+ ## Usage
22
+
23
+ ### Authorize Access to Dropbox
24
+
25
+ After creating a Dropbox app you must get an authorization. For this process you should have a "App key" and "App secret", and send the type of app (There are two types of Dropbox apps: **App folder** or **Full Dropbox** [dropbox apps](https://www.dropbox.com/developers/reference/devguide)). The rake task will use `"dropbox"` as default.
26
+
27
+ ```sh
28
+ $ rake dropbox:authorize APP_KEY=your_app_key APP_SECRET=your_app_secret ACCESS_TYPE=dropbox|app_folder
29
+ ```
30
+
31
+ This rake task will return an URL which you must visit to trigger the authorization process. Follow the instructions and you will have access to the following information:
32
+
33
+ ```yaml
34
+ access_token: #{session.access_token.key}
35
+ access_token_secret: #{session.access_token.secret}
36
+ user_id: #{user_id}
37
+ ```
38
+
39
+ This info should be added to the `config/dropbox.yml` file:
40
+
41
+ ```yaml
42
+ app_key: "..."
43
+ app_secret: "..."
44
+ access_token: "..."
45
+ access_token_secret: "..."
46
+ user_id: "..."
47
+ access_type: "dropbox|app_folder"
48
+ ```
49
+
50
+ ### Explore files and folders
51
+
52
+ - `:config_path` – String, the config file path
53
+ - `:only_files` – Boolean, for returning only files (optional - default returns files and folders)
54
+
55
+ ```ruby
56
+ client = Dropbox::Explorer::Client.new(config_path: File.join(Rails.root, '/config/dropbox.yml'))
57
+ files = client.contents_in_path('/', only_files: true)
58
+ ```
59
+
60
+ ### Get public URL for file
61
+
62
+ ```ruby
63
+ client.get_file_url(file_path)
64
+ ```
65
+
66
+ ## License
67
+
68
+ MIT
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ Bundler.setup
4
+
5
+ load "dropbox/explorer/rake/tasks.rake"
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "dropbox-explorer"
7
+ spec.version = "1.0.0"
8
+ spec.authors = ["David Cunha"]
9
+ spec.email = ["davidgoncalvescunha@gmail.com"]
10
+ spec.summary = %q{Explore dropbox folders}
11
+ spec.description = spec.summary
12
+ spec.homepage = "https://github.com/imaginary-cloud/dropbox-explorer"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.required_ruby_version = ">= 1.9.3"
21
+
22
+ spec.add_dependency "dropbox-sdk", "~> 1.6"
23
+ spec.add_dependency "activesupport", "~> 4.1"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.7"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0.0"
28
+ spec.add_development_dependency "vcr", "~> 2.9"
29
+ spec.add_development_dependency "webmock", "~> 1.20"
30
+ end
@@ -0,0 +1 @@
1
+ require "dropbox/explorer"
@@ -0,0 +1,4 @@
1
+ require "dropbox/explorer/client"
2
+ require "dropbox/explorer/credentials"
3
+ require "dropbox/explorer/folder"
4
+ require "dropbox/explorer/rake/railtie" if defined?(Rails)
@@ -0,0 +1,42 @@
1
+ require "dropbox_sdk"
2
+
3
+ module Dropbox
4
+ module Explorer
5
+ class Client
6
+ def initialize(options = {})
7
+ @options = options
8
+ @options[:credentials] = fetch_credentials
9
+
10
+ @dropbox_client = init_dropbox_client
11
+ end
12
+
13
+ def contents_in_path(path = '/', options = {})
14
+ folder_metadata = @dropbox_client.metadata(path)
15
+ Folder.new(folder_metadata).get_contents_paths(only_files: options[:only_files])
16
+ end
17
+
18
+ def get_file_content(file_path)
19
+ content, metadata = @dropbox_client.get_file_and_metadata(file_path)
20
+ content
21
+ end
22
+
23
+ def get_file_url(file_path)
24
+ generated_url = @dropbox_client.media(file_path)
25
+ generated_url['url']
26
+ end
27
+
28
+ private
29
+
30
+ def init_dropbox_client
31
+ credentials = @options[:credentials]
32
+ session = DropboxSession.new(credentials[:app_key], credentials[:app_secret])
33
+ session.set_access_token(credentials[:access_token], credentials[:access_token_secret])
34
+ DropboxClient.new(session, credentials[:access_type])
35
+ end
36
+
37
+ def fetch_credentials
38
+ Dropbox::Explorer::Credentials.new(config_path: @options[:config_path]).parse!
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,20 @@
1
+ require "yaml"
2
+ require "active_support/core_ext/hash/keys"
3
+
4
+ module Dropbox
5
+ module Explorer
6
+ class Credentials
7
+ def initialize(credentials)
8
+ @credentials = credentials
9
+ end
10
+
11
+ def parse!
12
+ if @credentials && @credentials[:config_path]
13
+ @credentials = YAML.load(File.read(@credentials[:config_path])).symbolize_keys
14
+ else
15
+ raise ArgumentError, "There is no path for the credentials config file"
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,23 @@
1
+ require "active_support/core_ext/hash/keys"
2
+
3
+ module Dropbox
4
+ module Explorer
5
+ class Folder
6
+ def initialize(folder_metadata)
7
+ @folder_metadata = folder_metadata.symbolize_keys
8
+ end
9
+
10
+ def get_contents_paths(options = {})
11
+ if @folder_metadata[:contents]
12
+ if options[:only_files]
13
+ @folder_metadata[:contents].map { |content| content unless content['is_dir'] }.compact
14
+ else
15
+ @folder_metadata[:contents]
16
+ end
17
+ else
18
+ raise ArgumentError, "There is no contents for this folder metadata"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,9 @@
1
+ module Dropbox
2
+ module Explorer
3
+ class Railtie < Rails::Railtie
4
+ rake_tasks do
5
+ load "dropbox/explorer/rake/tasks.rake"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,44 @@
1
+ require "dropbox_sdk"
2
+
3
+ module Dropbox
4
+ module Explorer
5
+ module Rake
6
+ extend self
7
+
8
+ def authorize(app_key, app_secret, access_type)
9
+ session = create_new_session(app_key, app_secret)
10
+
11
+ puts "Visit this URL: #{session.get_authorize_url}"
12
+ print "And after you approved the authorization confirm it here (y/n): "
13
+
14
+ assert_answer!
15
+ session.get_access_token
16
+ dropbox_client = DropboxClient.new(session, access_type)
17
+ account_info = dropbox_client.account_info
18
+
19
+ puts <<-MESSAGE
20
+
21
+ Authorization was successful. Here you go:
22
+
23
+ access_token: #{session.access_token.key}
24
+ access_token_secret: #{session.access_token.secret}
25
+ user_id: #{account_info["uid"]}
26
+ MESSAGE
27
+
28
+ rescue DropboxAuthError => e
29
+ puts e.message
30
+ puts "Please don't forget to approve the authorization"
31
+ exit
32
+ end
33
+
34
+ def create_new_session(app_key, app_secret)
35
+ DropboxSession.new(app_key, app_secret)
36
+ end
37
+
38
+ def assert_answer!
39
+ answer = STDIN.gets.strip
40
+ exit if answer == "n"
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,13 @@
1
+ require "dropbox/explorer/rake/rake"
2
+
3
+ namespace :dropbox do
4
+ desc "Get Authorization from Dropbox"
5
+ task :authorize do
6
+ if ENV["APP_KEY"].nil? or ENV["APP_SECRET"].nil?
7
+ puts "USAGE: `rake dropbox:authorize APP_KEY=your_app_key APP_SECRET=your_app_secret` ACCESS_TYPE=your_access_type"
8
+ exit
9
+ end
10
+
11
+ Dropbox::Explorer::Rake.authorize(ENV["APP_KEY"], ENV["APP_SECRET"], ENV["ACCESS_TYPE"] || "dropbox")
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ app_key: ""
2
+ app_secret: ""
3
+ access_token: ""
4
+ access_token_secret: ""
5
+ user_id: ""
6
+ access_type: "app_folder"
@@ -0,0 +1,6 @@
1
+ app_key: ""
2
+ app_secret: ""
3
+ access_token: ""
4
+ access_token_secret: ""
5
+ user_id: ""
6
+ access_type: "dropbox"
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+
3
+ describe Dropbox::Explorer::Client, vcr: true do
4
+ let(:path) { File.join(Bundler.root, "spec/config/dropbox.yml") }
5
+ let(:client) { Dropbox::Explorer::Client.new(config_path: path) }
6
+ let(:files) { client.contents_in_path('/', only_files: true) }
7
+
8
+ describe "#list_files" do
9
+ it "returns list of files in folder" do
10
+ expect(files).to be_a Array
11
+ end
12
+
13
+ it "returns list of files and folders in folder" do
14
+ files = client.contents_in_path('/')
15
+ expect(files).to be_a Array
16
+ end
17
+ end
18
+
19
+ describe "#get_file_url" do
20
+ it "get url from file" do
21
+ expect(client.get_file_url(files.first['path'])).to match(/dl.dropboxusercontent.com/)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ require "spec_helper"
2
+
3
+ describe Dropbox::Explorer::Credentials do
4
+ describe "#parse!" do
5
+ let(:access_type) { :dropbox }
6
+ let(:path) { File.join(Bundler.root, "spec/config/#{access_type}.yml") }
7
+
8
+ it "parses dropbox credentials" do
9
+ credentials = Dropbox::Explorer::Credentials.new(access_type: access_type, config_path: path)
10
+ expect(credentials.parse!).to eq CREDENTIALS[:dropbox]
11
+ end
12
+
13
+ it "fails to parse dropbox credentials" do
14
+ credentials = Dropbox::Explorer::Credentials.new(access_type: access_type)
15
+ expect{credentials.parse!}.to raise_error(ArgumentError)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ require "spec_helper"
2
+ require "yaml"
3
+ require "json"
4
+ require "active_support/core_ext/hash/keys"
5
+
6
+ describe Dropbox::Explorer::Folder do
7
+ describe "#get_contents_paths" do
8
+ let(:path) { File.join(Bundler.root, "spec/fixtures/vcr_cassettes/Dropbox_Explorer_Client/_list_files/returns_list_of_files_in_folder.yml") }
9
+ let(:folder_metadata) { YAML.load(File.read(path))['http_interactions'][0]['response']['body']['string'] }
10
+
11
+ it "returns list of files from folder metadata" do
12
+ files = Dropbox::Explorer::Folder.new(JSON.parse(folder_metadata)).get_contents_paths(only_files: true)
13
+ expect(files).to be_a Array
14
+ end
15
+
16
+ it "returns list of files and folders from folder metadata" do
17
+ files = Dropbox::Explorer::Folder.new(JSON.parse(folder_metadata)).get_contents_paths
18
+ expect(files).to be_a Array
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require "dropbox-explorer"
5
+ require "vcr"
6
+
7
+ Dir[File.join(Bundler.root, "spec/support/*.rb")].each do |file|
8
+ require file
9
+ end
@@ -0,0 +1,27 @@
1
+ require "yaml"
2
+ require "active_support/core_ext/string/strip"
3
+ require "active_support/core_ext/hash/except"
4
+ require "active_support/core_ext/hash/keys"
5
+
6
+ CREDENTIAL_FILES = [:dropbox, :app_folder].inject({}) do |hash, access_type|
7
+ hash.update(access_type => File.join(Bundler.root, "spec/config/#{access_type}.yml"))
8
+ end
9
+
10
+ CREDENTIALS = [:dropbox, :app_folder].inject({}) do |hash, access_type|
11
+ begin
12
+ content = File.read(CREDENTIAL_FILES[access_type])
13
+ credentials = YAML.load(content).symbolize_keys
14
+ hash.update(access_type => credentials)
15
+ rescue Errno::ENOENT
16
+ $stderr.puts <<-EOS.strip_heredoc
17
+
18
+ Some of the credential files were not found. Please, copy spec/config/dropbox.yml.example
19
+ into spec/config/dropbox.yml, and fill it with credentials of your Dropbox app with "full dropbox"
20
+ access you intend to use. And the same for spec/config/app_folder.yml and "app folder" access.
21
+
22
+ Make use of the "dropbox:authorize" Rake task.
23
+
24
+ EOS
25
+ exit
26
+ end
27
+ end
@@ -0,0 +1,6 @@
1
+ VCR.configure do |config|
2
+ config.cassette_library_dir = "spec/fixtures/vcr_cassettes"
3
+ config.hook_into :webmock
4
+ config.default_cassette_options = {record: :new_episodes}
5
+ config.configure_rspec_metadata!
6
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dropbox-explorer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - David Cunha
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-07 00:00:00.000000000 Z
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: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '4.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '4.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 3.0.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 3.0.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: vcr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '2.9'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '2.9'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '1.20'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '1.20'
111
+ description: Explore dropbox folders
112
+ email:
113
+ - davidgoncalvescunha@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - .rspec
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - dropbox-explorer.gemspec
125
+ - lib/dropbox-explorer.rb
126
+ - lib/dropbox/explorer.rb
127
+ - lib/dropbox/explorer/client.rb
128
+ - lib/dropbox/explorer/credentials.rb
129
+ - lib/dropbox/explorer/folder.rb
130
+ - lib/dropbox/explorer/rake/railtie.rb
131
+ - lib/dropbox/explorer/rake/rake.rb
132
+ - lib/dropbox/explorer/rake/tasks.rake
133
+ - spec/config/app_folder.yml.example
134
+ - spec/config/dropbox.yml.example
135
+ - spec/lib/dropbox/explorer/client_spec.rb
136
+ - spec/lib/dropbox/explorer/credentials_spec.rb
137
+ - spec/lib/dropbox/explorer/folder_spec.rb
138
+ - spec/spec_helper.rb
139
+ - spec/support/credentials.rb
140
+ - spec/support/vcr.rb
141
+ homepage: https://github.com/imaginary-cloud/dropbox-explorer
142
+ licenses:
143
+ - MIT
144
+ metadata: {}
145
+ post_install_message:
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - '>='
152
+ - !ruby/object:Gem::Version
153
+ version: 1.9.3
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - '>='
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ requirements: []
160
+ rubyforge_project:
161
+ rubygems_version: 2.0.14
162
+ signing_key:
163
+ specification_version: 4
164
+ summary: Explore dropbox folders
165
+ test_files:
166
+ - spec/config/app_folder.yml.example
167
+ - spec/config/dropbox.yml.example
168
+ - spec/lib/dropbox/explorer/client_spec.rb
169
+ - spec/lib/dropbox/explorer/credentials_spec.rb
170
+ - spec/lib/dropbox/explorer/folder_spec.rb
171
+ - spec/spec_helper.rb
172
+ - spec/support/credentials.rb
173
+ - spec/support/vcr.rb