Class: PF::BoxAction
- Inherits:
-
Object
- Object
- PF::BoxAction
- Defined in:
- lib/pf/action/box_action.rb
Class Method Summary collapse
- .add_account(name, client_id, client_secret) ⇒ Object
- .client(name) ⇒ Object
- .make_account_refresh_callback(name) ⇒ Object
- .push(file, folder: "/") ⇒ Object
- .refresh(name) ⇒ Object
- .remove_account(name) ⇒ Object
Class Method Details
.add_account(name, client_id, client_secret) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/pf/action/box_action.rb', line 18 def self.add_account(name, client_id, client_secret) box = Profile.box account = OAuth2Account.new(name, client_id, client_secret) updated = box.add_account(account) box.save if updated refresh(name) end end |
.client(name) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/pf/action/box_action.rb', line 70 def self.client(name) box = Profile.box account = box.account(name) callback = make_account_refresh_callback(name) Boxr::Client.new(account.access_token, refresh_token: account.refresh_token, client_id: account.client_id, client_secret: account.client_secret, &callback) end |
.make_account_refresh_callback(name) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/pf/action/box_action.rb', line 59 def self.make_account_refresh_callback(name) lambda do |access, refresh, identifier| puts "update access_token(#{access}) and refresh_token(#{refresh})" box = Profile.box account = box.account(name) account.access_token = access account.refresh_token = refresh box.save end end |
.push(file, folder: "/") ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/pf/action/box_action.rb', line 6 def self.push(file, folder: "/") box = Profile.box if box.account.access_token.nil? refresh box.default_account end client = client(box.default_account) box_folder = client.folder_from_path(folder) box_file = client.upload_file(file, box_folder) updated_file = client.create_shared_link_for_file(box_file, access: :open) puts "Shared Link: #{updated_file.shared_link.url}" end |
.refresh(name) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/pf/action/box_action.rb', line 34 def self.refresh(name) box = Profile.box account = box.account(name) if account.access_token.nil? oauth_url = Boxr::oauth_url('Dig-that-hole-forget-the-sun', client_id: account.client_id) puts "Your authorization url is: " puts puts oauth_url puts puts "copy the url above and paste it into your browser,\npress enter, and click 'Grant access to Box' button.\nAfter the broswer jumps to the redirect url of your app,\ncopy the url in your browser's address bar and paste it here.\n EOF\n\n print \"The url in your browser's address bar is: \"\n code = STDIN.gets.chomp.split('=').last\n tokens = Boxr::get_tokens(code, client_id: account.client_id, client_secret: account.client_secret)\n account.refresh_token = tokens.refresh_token\n account.access_token = tokens.access_token\n box.save\n end\nend\n" |
.remove_account(name) ⇒ Object
28 29 30 31 32 |
# File 'lib/pf/action/box_action.rb', line 28 def self.remove_account(name) box = Profile.box box.remove_account(name) box.save end |