pf 0.1.2 → 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: ac25542d0dfea368c2f01fc10a8c0cee6d60c26b
4
- data.tar.gz: 21933fda0cf4f0fb1577e37a94b6a000898de300
3
+ metadata.gz: b541be039f8bf03e6c46f15b98e79c0973fdf696
4
+ data.tar.gz: 54253357359323077c5c11c41e11a16764a3706c
5
5
  SHA512:
6
- metadata.gz: c0be5b08998eaca1c4ff4259ed8ca15ab38b7b5512602cd3c4d4d6ac546bfb1011cf9765473bbb99f423101a4c53ebfed131319850135b6af78833c17bf636ba
7
- data.tar.gz: 104c37ce9cee968624b2e8a0cc64bbcfa29e683726abf201b227b1ce6d0ab8765ebb5a8b4c4ceb7f97493d5447b8191125a9691786a486cae6c83525c011ae95
6
+ metadata.gz: e0d404d6d214fdd8c9ba1456fc31e83b8b03d23a0586e974ca6e8faad19e3a44e50de31b8edb3132aed047ada28d2b9cdcc85ddd88e035c30da6dea16e087cd8
7
+ data.tar.gz: a5b37fdabf4bd7e0a4a2345cfc64212573cbda0551b0d299ffb8e80983399c7f5c28c37e1ffc611534b16dd7d3a3eb929bc74f103af3774535011eca150408da
@@ -0,0 +1,82 @@
1
+ require 'boxr'
2
+ require 'pf/profile/profile'
3
+
4
+ module PF
5
+ class BoxAction
6
+ def self.push(file, folder: "/")
7
+ box = Profile.box
8
+ if box.account.access_token.nil?
9
+ refresh box.default_account
10
+ end
11
+ client = client(box.default_account)
12
+ box_folder = client.folder_from_path(folder)
13
+ box_file = client.upload_file(file, box_folder)
14
+ updated_file = client.create_shared_link_for_file(box_file, access: :open)
15
+ puts "Shared Link: #{updated_file.shared_link.url}"
16
+ end
17
+
18
+ def self.add_account(name, client_id, client_secret)
19
+ box = Profile.box
20
+ account = OAuth2Account.new(name, client_id, client_secret)
21
+ updated = box.add_account(account)
22
+ box.save
23
+ if updated
24
+ refresh(name)
25
+ end
26
+ end
27
+
28
+ def self.remove_account(name)
29
+ box = Profile.box
30
+ box.remove_account(name)
31
+ box.save
32
+ end
33
+
34
+ def self.refresh(name)
35
+ box = Profile.box
36
+ account = box.account(name)
37
+ if account.access_token.nil?
38
+ oauth_url = Boxr::oauth_url('Dig-that-hole-forget-the-sun', client_id: account.client_id)
39
+ puts "Your authorization url is: "
40
+ puts
41
+ puts oauth_url
42
+ puts
43
+ puts <<-EOF
44
+ copy the url above and paste it into your browser,
45
+ press enter, and click 'Grant access to Box' button.
46
+ After the broswer jumps to the redirect url of your app,
47
+ copy the url in your browser's address bar and paste it here.
48
+ EOF
49
+
50
+ print "The url in your browser's address bar is: "
51
+ code = STDIN.gets.chomp.split('=').last
52
+ tokens = Boxr::get_tokens(code, client_id: account.client_id, client_secret: account.client_secret)
53
+ account.refresh_token = tokens.refresh_token
54
+ account.access_token = tokens.access_token
55
+ box.save
56
+ end
57
+ end
58
+
59
+ def self.make_account_refresh_callback(name)
60
+ lambda do |access, refresh, identifier|
61
+ puts "update access_token(#{access}) and refresh_token(#{refresh})"
62
+ box = Profile.box
63
+ account = box.account(name)
64
+ account.access_token = access
65
+ account.refresh_token = refresh
66
+ box.save
67
+ end
68
+ end
69
+
70
+ def self.client(name)
71
+ box = Profile.box
72
+ account = box.account(name)
73
+ callback = make_account_refresh_callback(name)
74
+ Boxr::Client.new(account.access_token,
75
+ refresh_token: account.refresh_token,
76
+ client_id: account.client_id,
77
+ client_secret: account.client_secret,
78
+ &callback)
79
+
80
+ end
81
+ end
82
+ end
@@ -1,10 +1,34 @@
1
1
  require "thor"
2
2
  require "yaml"
3
3
  require "pf/cli/qiniu"
4
+ require "pf/cli/box"
5
+ require "tty-table"
4
6
 
5
7
  module PF
6
8
  class CLI < Thor
7
9
  desc "qiniu <command> [<args>]", "using qiniu service to manage files"
8
10
  subcommand 'qiniu', QiniuCommand
11
+
12
+ desc "box <command> [<args>]", "using box service to manage files"
13
+ subcommand 'box', BoxCommand
14
+
15
+ desc "service <command> [<args>]", "using box service to manage files"
16
+ def services
17
+ services = [
18
+ {
19
+ :name => "box",
20
+ :home => "https://box.com/home",
21
+ :desc => "A cloud content management and file sharing service, based in Redwood City, California"
22
+ },
23
+ {
24
+ :name => "qiniu",
25
+ :home => "https://qiniu.com",
26
+ :desc => "A cloud-based storage solutions provider, based in Shanghai"
27
+ }
28
+ ]
29
+ puts "pf supports following cloud storage services currently: "
30
+ table = TTY::Table.new services.map{|service|["", "- " + service[:name], service[:home], service[:desc]]}
31
+ puts table.render(:basic, multiline: true, column_widths: [1, 10, 20, 40], padding: 1)
32
+ end
9
33
  end
10
34
  end
@@ -0,0 +1,27 @@
1
+ require "thor"
2
+ require "pf/profile/profile"
3
+ require "pf/action/action"
4
+ require "pf/cli/box_account"
5
+ require "pf/cli/command_base"
6
+
7
+ module PF
8
+ class BoxCommand < CommandBase
9
+
10
+ @@myself = "box"
11
+
12
+ desc "push <filename> [<folder_name>]", "upload file to box service to <folder_name>"
13
+ def push(filepath, folder="/")
14
+ if Profile.box().account().nil?
15
+ puts "You haven't add any box accounts. Please add an box account before push"
16
+ return
17
+ end
18
+
19
+ BoxAction.push(filepath, folder: folder)
20
+ end
21
+
22
+ desc "account <subcommand> [argv]", "manage box accounts"
23
+ subcommand "account", BoxAccountCommand
24
+ end
25
+ end
26
+
27
+
@@ -0,0 +1,40 @@
1
+ require "thor"
2
+ require "pf/profile/profile"
3
+ require "pf/action/box_action"
4
+ require "pf/cli/command_base"
5
+
6
+ module PF
7
+ class BoxAccountCommand < CommandBase
8
+
9
+ @@myself = "account"
10
+
11
+ desc "account add <account_name> <client_id> <client_secret>", "add box account"
12
+ option :default, :type => :boolean
13
+ def add(name, client_id, client_secret)
14
+ BoxAction.add_account(name, client_id, client_secret)
15
+ end
16
+
17
+ default_task :list
18
+
19
+ desc "account list", "list all box accounts"
20
+ def list()
21
+ box = Profile.box
22
+ default_account = box.default_account
23
+ puts "box accounts(#{box.accounts.size}):"
24
+ puts
25
+ box.accounts.each do |account|
26
+ if account.name == default_account
27
+ print " * "
28
+ else
29
+ print " "
30
+ end
31
+ puts account.name
32
+ end
33
+ end
34
+
35
+ desc "account rm <account_name>", "remove specified account"
36
+ def rm(account_name)
37
+ BoxAction.remove_account(account_name)
38
+ end
39
+ end
40
+ end
@@ -3,10 +3,11 @@ require "yaml"
3
3
  module PF
4
4
  class Profile
5
5
 
6
- attr_accessor :qiniu
6
+ attr_accessor :qiniu, :box
7
7
 
8
8
  def initialize
9
9
  @qiniu = QiniuProfile.new(self)
10
+ @box = BoxProfile.new(self)
10
11
  end
11
12
 
12
13
  def save
@@ -44,6 +45,15 @@ module PF
44
45
  def self.qiniu
45
46
  profile.qiniu
46
47
  end
48
+
49
+ def self.box
50
+ p = profile
51
+ if p.box.nil?
52
+ p.box = BoxProfile.new(p)
53
+ end
54
+ p.save
55
+ p.box
56
+ end
47
57
  end
48
58
 
49
59
  class QiniuProfile
@@ -63,11 +73,68 @@ module PF
63
73
  @accounts.find { |account| account.name == name}
64
74
  end
65
75
 
76
+ def remove_account(name)
77
+ @accounts.delete_if{|account| account.name == name}
78
+ update_default
79
+ end
80
+
81
+ def update_default(name: nil)
82
+ if name.nil?
83
+ if @default_account.nil?
84
+ unless @accounts.empty?
85
+ @default_account = @accounts.first.name
86
+ end
87
+ else
88
+ if @accounts.empty?
89
+ @default_account = nil
90
+ else
91
+ if account.nil?
92
+ @default_account = @accounts.first.name
93
+ end
94
+ end
95
+ end
96
+ else
97
+ if exist_account? name
98
+ @default_account = name
99
+ else
100
+ update_default
101
+ end
102
+ end
103
+ end
104
+
105
+ def add_account(new_account, default: true)
106
+ updated = true
107
+ account = account(new_account.name)
108
+
109
+ if account.nil?
110
+ @accounts.push(new_account)
111
+ else
112
+ if account.equal? new_account
113
+ updated = false
114
+ else
115
+ # remove_account(new_account.name)
116
+ @accounts.delete_if{|a| a.name == new_account}
117
+ @accounts.unshift(new_account)
118
+ end
119
+ end
120
+
121
+ if default
122
+ @default_account = new_account.name
123
+ end
124
+
125
+ update_default
126
+ updated
127
+ end
128
+
66
129
  def save
67
130
  @parent.save
68
131
  end
69
132
  end
70
133
 
134
+ class BoxProfile < QiniuProfile
135
+
136
+ end
137
+
71
138
  class SecretKeyAccount
72
139
  attr_accessor :name, :access_key, :secret_key
73
140
  def initialize(name, access_key, secret_key)
@@ -75,5 +142,25 @@ module PF
75
142
  @access_key = access_key
76
143
  @secret_key = secret_key
77
144
  end
145
+
146
+ def equal?(other)
147
+ @name == other.name and @access_key == other.access_key and @secret_key = other.secret_key
148
+ end
149
+ end
150
+
151
+ class OAuth2Account
152
+ attr_accessor :name, :client_id, :client_secret, :access_token, :refresh_token
153
+
154
+ def initialize(name, client_id, client_secret, access_token: nil, refresh_token: nil)
155
+ @name = name
156
+ @client_id = client_id
157
+ @client_secret = client_secret
158
+ @access_token = access_token
159
+ @refresh_token = refresh_token
160
+ end
161
+
162
+ def equal?(other)
163
+ @name == other.name and @client_id = other.client_id and @client_secret = other.client_secret
164
+ end
78
165
  end
79
166
  end
@@ -1,3 +1,3 @@
1
1
  module PF
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
data/pf.gemspec CHANGED
@@ -35,4 +35,6 @@ Gem::Specification.new do |spec|
35
35
  spec.add_development_dependency "minitest", "~> 5.0"
36
36
  spec.add_runtime_dependency "qiniu", ">= 6.8.1"
37
37
  spec.add_runtime_dependency "thor", ">= 0.19.4"
38
+ spec.add_runtime_dependency "boxr", ">= 1.4.0"
39
+ spec.add_runtime_dependency "tty-table", ">= 0.8.0"
38
40
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zhu Ran
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-03 00:00:00.000000000 Z
11
+ date: 2017-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,34 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: 0.19.4
83
+ - !ruby/object:Gem::Dependency
84
+ name: boxr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 1.4.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 1.4.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: tty-table
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 0.8.0
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 0.8.0
83
111
  description: You can view, download or upload files via your public cloud storage
84
112
  service accounts
85
113
  email:
@@ -101,7 +129,10 @@ files:
101
129
  - bin/setup
102
130
  - lib/pf.rb
103
131
  - lib/pf/action/action.rb
132
+ - lib/pf/action/box_action.rb
104
133
  - lib/pf/cli.rb
134
+ - lib/pf/cli/box.rb
135
+ - lib/pf/cli/box_account.rb
105
136
  - lib/pf/cli/command_base.rb
106
137
  - lib/pf/cli/qiniu.rb
107
138
  - lib/pf/cli/qiniu_account.rb