Class: PF::BoxAction

Inherits:
Object
  • Object
show all
Defined in:
lib/pf/action/box_action.rb

Class Method Summary collapse

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.(name, client_id, client_secret)
  box = Profile.box
   = OAuth2Account.new(name, client_id, client_secret)
  updated = box.()
  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
   = box.(name)
  callback = (name)
  Boxr::Client.new(.access_token,
                            refresh_token: .refresh_token,
                            client_id: .client_id,
                            client_secret: .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.(name)
  lambda  do |access, refresh, identifier|
    puts "update access_token(#{access}) and refresh_token(#{refresh})"
    box = Profile.box
     = box.(name)
    .access_token = access
    .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..access_token.nil?
    refresh box.
  end
  client = client(box.)
  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
   = box.(name)
  if .access_token.nil?
    oauth_url = Boxr::oauth_url('Dig-that-hole-forget-the-sun', client_id: .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.(name)
  box = Profile.box
  box.(name)
  box.save
end