fallcli 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,92 @@
1
+ require 'fallcli/browser_helper'
2
+ require 'dispel'
3
+
4
+ module FallCli
5
+ module Middleware
6
+ # Check if the client has set-up configuration yet.
7
+ class Browser < Base
8
+
9
+ def show_ui filelist_obj
10
+ ["\n", filelist_obj.show_files, "\nCurrent position: #{filelist_obj.position + 1} "].join("\n")
11
+ end
12
+
13
+ SPLASH = %{
14
+
15
+
16
+
17
+ '; .'
18
+ ,':'' :',''
19
+ '',,.:' '',,,:',
20
+ '',,,,..,' '',,,,,.''
21
+ .':,,,,,....'` ';,,,,,,,..''
22
+ '',,,,,,,.....', ':,,,,,,,,...:'.
23
+ ;',,,,,,,,,......''',,,,,,,,,,.....''
24
+ `';,,,,,,,,,,.......',,,,,,,,,,,.......';
25
+ ':,,,,,,,,,,,......```,,,,,,,,,,........'
26
+ '',,,,,,,,,,.....`````,,,,,,,,,......,'`
27
+ '',,,,,,,,,...`````````,,,,,,,.....:'
28
+ :',,,,,,,,..```````````.,,,,,....''
29
+ `',,,,,,,```````````````,,,,...';
30
+ ':,,,.``````````````````,,..'.
31
+ '',`````````````````````.,'`
32
+ '::`````````````````````,.',
33
+ `',,::,``````````````````:,..''
34
+ :',,,::::```````````````:::,...''
35
+ '',,,,::::,,```````````,::::,....:'
36
+ ';,,,,,::::,,,.````````::::::,......'`
37
+ ':,,,,,,::::,,,,,`` ``::::::::,.......':
38
+ ':,,,,,,,::::,,,,,,. .;;:::::::,........'
39
+ '',,,,,,::::,,,,,,.`;;::::::::,......;'`
40
+ .';,,,,::::,,,,,,``,:::::::::,.....';
41
+ '',,,::::,,,,,```,,::::::::,...''
42
+ `';,:::,,,,,````,,,,::::::,.,':
43
+ ;':::,,,..````,,,,,::::,,''
44
+ '..:,,...````,,,,,,:::,.'
45
+ '...,....````,,,,,,.:...'
46
+ ;'.......````,,,,,,....''
47
+ ''.....````,,,,,,..;'`
48
+ :',...````,,,,,,,';
49
+ ''`.````,,,,,''
50
+ .':````,,,:',
51
+ ''```,,''
52
+ `';`;'.
53
+ :''
54
+
55
+ ______ ___ _ _ _____ _ _____
56
+ | ___/ _ \\ | | | | / __ \\| | |_ _|
57
+ | |_ / /_\\ \\| | | | | / \\/| | | |
58
+ | _|| _ || | | | | | | | | |
59
+ | | | | | || |____| |___| \\__/\\| |_____| |_
60
+ \\_| \\_| |_/\\_____/\\_____/\\____/\\_____/\\___/
61
+ V.#{FallCli::VERSION}
62
+ }
63
+
64
+ def call(env)
65
+ config = env["config"]
66
+
67
+ say SPLASH
68
+
69
+ sleep(2)
70
+
71
+ songs = env['dropbox-client'].ls
72
+
73
+ browser = FallCli::BrowserHelper.new(songs)
74
+
75
+ Dispel::Screen.open do |screen|
76
+ screen.draw show_ui(browser)
77
+
78
+ Dispel::Keyboard.output do |key|
79
+ case key
80
+ when :up then browser.position_up
81
+ when :down then browser.position_down
82
+ when "q" then break
83
+ end
84
+ screen.draw show_ui(browser)
85
+ end
86
+ end
87
+
88
+ @app.call(env)
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,18 @@
1
+ module FallCli
2
+ module Middleware
3
+ # Check if the client has set-up configuration yet.
4
+ class CheckConfiguration < Base
5
+ def call(env)
6
+ config = env["config"]
7
+
8
+ if !config || !config.data || !config.app_key || !config.secret_key
9
+ say "You must run `fallcli authorize` in order to connect to Dropbox", :red
10
+ exit 1
11
+ end
12
+
13
+ @app.call(env)
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,27 @@
1
+ require 'thor'
2
+ require 'dropbox-api'
3
+ require 'cgi'
4
+
5
+ module FallCli
6
+ module Middleware
7
+ class CheckCredentials < Base
8
+ def call(env)
9
+
10
+ say "Checking credentials with Dropbox..."
11
+
12
+ begin
13
+ env['dropbox-client'].ls
14
+ rescue Dropbox::API::Error => e
15
+ say "Connection to Dropbox failed (#{e})", :red
16
+ say "Check your ~/.fallcli/config file, and double check your credentials are correct", :yellow
17
+ exit 1
18
+ end
19
+
20
+ say "Connection to dropbox successful!", :green
21
+
22
+ @app.call(env)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,27 @@
1
+ require 'thor'
2
+ require "dropbox-api"
3
+
4
+ module FallCli
5
+ module Middleware
6
+ class InjectClient < Base
7
+ def call(env)
8
+
9
+ Dropbox::API::Config.app_key = env['config'].app_key
10
+ Dropbox::API::Config.app_secret = env['config'].secret_key
11
+ Dropbox::API::Config.mode = 'dropbox'
12
+
13
+ begin
14
+ client = Dropbox::API::Client.new(:token => env['config'].app_token, :secret => env['config'].app_secret)
15
+ rescue Dropbox::API::Error => e
16
+ say "Connection to Dropbox failed (#{e})", :red
17
+ exit 1
18
+ end
19
+
20
+ env['dropbox-client'] = client
21
+
22
+ @app.call(env)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,15 @@
1
+ module FallCli
2
+ module Middleware
3
+ # Check if the client has set-up configuration yet.
4
+ class InjectConfiguration < Base
5
+ def call(env)
6
+ config = FallCli::Configuration.instance
7
+
8
+ env["config"] = config
9
+
10
+ @app.call(env)
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,128 @@
1
+ require 'fallcli/uploader_browser_helper'
2
+ require 'ruby-progressbar'
3
+ require 'dispel'
4
+ require 'pathname'
5
+
6
+ module FallCli
7
+ module Middleware
8
+ # Check if the client has set-up configuration yet.
9
+ class UploaderBrowser < Base
10
+
11
+ def show_ui filelist_obj
12
+ ["\n", filelist_obj.show_files, "\nCurrent position: #{filelist_obj.position + 1} "].join("\n")
13
+ end
14
+
15
+ def get_files
16
+ require 'find'
17
+ files = []
18
+ Find.find('.') do |e|
19
+ if !File.directory?(e)
20
+ files << e unless e.match /.git/
21
+ end
22
+ end
23
+ files
24
+ end
25
+
26
+ SPLASH = %{
27
+
28
+
29
+
30
+ '; .'
31
+ ,':'' :',''
32
+ '',,.:' '',,,:',
33
+ '',,,,..,' '',,,,,.''
34
+ .':,,,,,....'` ';,,,,,,,..''
35
+ '',,,,,,,.....', ':,,,,,,,,...:'.
36
+ ;',,,,,,,,,......''',,,,,,,,,,.....''
37
+ `';,,,,,,,,,,.......',,,,,,,,,,,.......';
38
+ ':,,,,,,,,,,,......```,,,,,,,,,,........'
39
+ '',,,,,,,,,,.....`````,,,,,,,,,......,'`
40
+ '',,,,,,,,,...`````````,,,,,,,.....:'
41
+ :',,,,,,,,..```````````.,,,,,....''
42
+ `',,,,,,,```````````````,,,,...';
43
+ ':,,,.``````````````````,,..'.
44
+ '',`````````````````````.,'`
45
+ '::`````````````````````,.',
46
+ `',,::,``````````````````:,..''
47
+ :',,,::::```````````````:::,...''
48
+ '',,,,::::,,```````````,::::,....:'
49
+ ';,,,,,::::,,,.````````::::::,......'`
50
+ ':,,,,,,::::,,,,,`` ``::::::::,.......':
51
+ ':,,,,,,,::::,,,,,,. .;;:::::::,........'
52
+ '',,,,,,::::,,,,,,.`;;::::::::,......;'`
53
+ .';,,,,::::,,,,,,``,:::::::::,.....';
54
+ '',,,::::,,,,,```,,::::::::,...''
55
+ `';,:::,,,,,````,,,,::::::,.,':
56
+ ;':::,,,..````,,,,,::::,,''
57
+ '..:,,...````,,,,,,:::,.'
58
+ '...,....````,,,,,,.:...'
59
+ ;'.......````,,,,,,....''
60
+ ''.....````,,,,,,..;'`
61
+ :',...````,,,,,,,';
62
+ ''`.````,,,,,''
63
+ .':````,,,:',
64
+ ''```,,''
65
+ `';`;'.
66
+ :''
67
+
68
+ ______ ___ _ _ _____ _ _____
69
+ | ___/ _ \\ | | | | / __ \\| | |_ _|
70
+ | |_ / /_\\ \\| | | | | / \\/| | | |
71
+ | _|| _ || | | | | | | | | |
72
+ | | | | | || |____| |___| \\__/\\| |_____| |_
73
+ \\_| \\_| |_/\\_____/\\_____/\\____/\\_____/\\___/
74
+ V.#{FallCli::VERSION}
75
+ }
76
+
77
+ def call(env)
78
+ config = env["config"]
79
+
80
+ say SPLASH
81
+
82
+ sleep(2)
83
+
84
+ files = get_files
85
+
86
+ upload_browser = FallCli::UploaderBrowserHelper.new(files)
87
+
88
+ Dispel::Screen.open do |screen|
89
+ screen.draw show_ui(upload_browser)
90
+
91
+ Dispel::Keyboard.output do |key|
92
+ case key
93
+ when :up then upload_browser.position_up
94
+ when :down then upload_browser.position_down
95
+ when :enter then
96
+ file = upload_browser.get_current_file
97
+ file_name = File.basename(file)
98
+ filepath = Pathname.new(file).realpath.to_s
99
+ total_size = File.size(filepath)
100
+ contents = File.read(filepath)
101
+
102
+ if total_size < 5*1024*1024
103
+ env['dropbox-client'].upload file_name, contents
104
+ else
105
+ say "Larger than 5MB: Progress Upload"
106
+
107
+ upload_progress_bar = ::ProgressBar.create(:title => "Upload progress",
108
+ :format => '%a <%B> %p%% %t',
109
+ :starting_at => 0,
110
+ :total => total_size)
111
+
112
+ response = env['dropbox-client'].chunked_upload file_name, File.open(filepath), :chunk_size => 0.1*1024*1024 do |offset, upload|
113
+ upload_progress_bar.progress = offset
114
+ end
115
+ end
116
+
117
+ say "File uploaded successfully!"
118
+ when "q" then break
119
+ end
120
+ screen.draw show_ui(upload_browser)
121
+ end
122
+ end
123
+
124
+ @app.call(env)
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,29 @@
1
+ module FallCli
2
+ class UploaderBrowserHelper
3
+ attr_accessor :position
4
+
5
+ def initialize items
6
+ @files = Array.new
7
+ items.each { |item| @files << item }
8
+ @position = 0
9
+ end
10
+
11
+ def get_current_file
12
+ @files[@position]
13
+ end
14
+
15
+ def show_files
16
+ @files.each_with_index.map do |item, index|
17
+ position == index ? "[#{item}]" : " #{item} "
18
+ end
19
+ end
20
+
21
+ def position_up
22
+ @position -= 1 unless @position < 1
23
+ end
24
+
25
+ def position_down
26
+ @position += 1 unless @position == (@files.size - 1)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module FallCli
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe FallCli::CLI do
4
+ include_context "spec"
5
+
6
+ describe "account" do
7
+ it "returns account info" do
8
+
9
+ stub_request(:get, "https://api.dropbox.com/1/account/info?").
10
+ with(:headers => {'Accept'=>'*/*', 'Authorization'=>/OAuth oauth_consumer_key="#{app_key}", oauth_nonce="\S+", oauth_signature="\S+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\S+", oauth_token="#{app_token}", oauth_version="1.0"/, 'User-Agent'=>'OAuth gem v0.4.7'}).
11
+ to_return(:status => 200, :body => fixture('account'))
12
+
13
+ @cli.account
14
+ expect($stdout.string).to eq <<-eos
15
+ REFERRAL_LINK | DISPLAY_NAME | UID | COUNTRY | QUOTA_INFO
16
+ -------------------------------|--------------|----------|---------|-------------------------------
17
+ https://www.dropbox.com/ref... | John P. User | 12345678 | US | #<Dropbox::API::Object norm...
18
+ eos
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe FallCli::CLI do
4
+ include_context "spec"
5
+
6
+ describe "authorize" do
7
+ before do
8
+ stub_request(:post, "https://www.dropbox.com/1/oauth/request_token").
9
+ with(:headers => {'Accept'=>'*/*', 'Authorization'=>/OAuth oauth_body_hash="\S+", oauth_callback="oob", oauth_consumer_key="foo", oauth_nonce="\S+", oauth_signature="\S+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\S+", oauth_version="1.0"/, 'Content-Length'=>'0', 'User-Agent'=>'OAuth gem v0.4.7'}).
10
+ to_return(:status => 200, :body => "oauth_token_secret=b9q1n5il4lcc&oauth_token=mh7an9dkrg59")
11
+
12
+ stub_request(:post, "https://www.dropbox.com/1/oauth/access_token").
13
+ with(:headers => {'Accept'=>'*/*', 'Authorization'=>/OAuth oauth_body_hash="\S+", oauth_consumer_key="#{app_key}", oauth_nonce="\S+", oauth_signature="\S+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\S+", oauth_token="\S+", oauth_verifier="\S+", oauth_version="1.0"/, 'Content-Length'=>'0', 'User-Agent'=>'OAuth gem v0.4.7'}).
14
+ to_return(:status => 200, :body => "oauth_token_secret=95grkd9na7hm&oauth_token=ccl4li5n1q9b&uid=100")
15
+
16
+ stub_request(:get, "https://api.dropbox.com/1/metadata/dropbox/?").
17
+ with(:headers => {'Accept'=>'*/*', 'Authorization'=>/OAuth oauth_consumer_key="#{app_key}", oauth_nonce="\S+", oauth_signature="\S+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\S+", oauth_token="\S+", oauth_version="1.0"/, 'User-Agent'=>'OAuth gem v0.4.7'}).
18
+ to_return(:status => 200, :body => fixture("verify_success"))
19
+ end
20
+
21
+ it "asks the right questions and saves then checks credentials" do
22
+
23
+ $stdout.should_receive(:print).exactly(6).times
24
+ $stdout.should_receive(:print).with("Enter your App key: ")
25
+ $stdin.should_receive(:gets).and_return(app_key)
26
+ $stdout.should_receive(:print).with("Enter your Secret key: ")
27
+ $stdin.should_receive(:gets).and_return(secret_key)
28
+ $stdout.should_receive(:print).with("Once you authorize the app on Dropbox, type yes... ")
29
+ $stdin.should_receive(:gets).and_return('yes')
30
+
31
+ @cli.authorize
32
+
33
+ expect($stdout.string).to eq <<-eos
34
+ If you dont know your app and secret key, try `fallcli get-keys`
35
+ Go to this url and click 'Authorize' to get the token:
36
+ https://www.dropbox.com/1/oauth/authorize?oauth_token=mh7an9dkrg59
37
+ Credentials saved to ~/.fallcli
38
+ Checking credentials with Dropbox...
39
+ Connection to dropbox successful!
40
+ eos
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe FallCli::CLI do
4
+ include_context "spec"
5
+
6
+ describe "help" do
7
+ it "shows a help message" do
8
+ @cli.help
9
+ expect($stdout.string).to match("Commands:")
10
+ end
11
+
12
+ it "shows a help message for specific commands" do
13
+ @cli.help "authorize"
14
+ expect($stdout.string).to match("Usage:")
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe FallCli::CLI do
4
+ include_context "spec"
5
+
6
+ describe "verify" do
7
+ it "returns confirmation text when verify passes" do
8
+ stub_request(:get, "https://api.dropbox.com/1/metadata/dropbox/?").
9
+ with(:headers => {'Accept'=>'*/*', 'Authorization'=> /OAuth oauth_consumer_key="#{app_key}", oauth_nonce="\S+", oauth_signature="\S+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\S+", oauth_token="#{app_token}", oauth_version="1.0"/, 'User-Agent'=>'OAuth gem v0.4.7'}).
10
+ to_return(:status => 200, :body => fixture("verify_success"))
11
+ @cli.verify
12
+ expect($stdout.string).to eq <<-eos
13
+ Checking credentials with Dropbox...
14
+ Connection to dropbox successful!
15
+ eos
16
+ end
17
+
18
+ it "returns error string when verify fails" do
19
+ stub_request(:get, "https://api.dropbox.com/1/metadata/dropbox/?").
20
+ with(:headers => {'Accept'=>'*/*', 'Authorization'=> /OAuth oauth_consumer_key="#{app_key}", oauth_nonce="\S+", oauth_signature="\S+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\S+", oauth_token="#{app_token}", oauth_version="1.0"/, 'User-Agent'=>'OAuth gem v0.4.7'}).
21
+ to_return(:status => 401, :body => "", :headers => {})
22
+ expect { @cli.verify }.to raise_error(SystemExit)
23
+ expect($stdout.string).to eq <<-eos
24
+ Checking credentials with Dropbox...
25
+ Connection to Dropbox failed (401 - Bad or expired token)
26
+ Check your ~/.fallcli/config file, and double check your credentials are correct
27
+ eos
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+