Class: BanalDropboxApi

Inherits:
Object
  • Object
show all
Defined in:
app/models/banal_dropbox_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBanalDropboxApi

Returns a new instance of BanalDropboxApi.



4
5
6
7
8
9
10
11
12
13
14
# File 'app/models/banal_dropbox_api.rb', line 4

def initialize
  self.oauth_token = Rails.application.credentials.banal_dropbox_oauth_token

  # After oauth_token initialilzation, because the oauth_token is required 
  # when enabling dynamicly fetched (via api) values for the folllowing to instance variables
  @team_folder_id = "6139129136" # hardcoded to banal team folder for now
  @team_member_id = "dbmid:AABXMhNAuNmdg79aaoHQwu9PuuZeJM7yjFU" # hardcoded to my team member id for now

  self.instantiate_client
  self.inject_middleware # Dropbox Business API feature: https://github.com/Jesus/dropbox_api/issues/29
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



2
3
4
# File 'app/models/banal_dropbox_api.rb', line 2

def client
  @client
end

#oauth_tokenObject

Returns the value of attribute oauth_token.



2
3
4
# File 'app/models/banal_dropbox_api.rb', line 2

def oauth_token
  @oauth_token
end

Instance Method Details

#combined_path(path) ⇒ Object



24
25
26
# File 'app/models/banal_dropbox_api.rb', line 24

def combined_path(path)
  team_folder_path + path
end

#download(path, &block) ⇒ Object

Doesn’t work, although it is expected to work by me www.dropbox.com/developers/documentation/http/documentation#sharing-create_shared_link_with_settings If you can get this to work, you’l get a cookie 🍪def get_link(path)

client.create_shared_link_with_settings(combined_path(path))['url']

end



35
36
37
# File 'app/models/banal_dropbox_api.rb', line 35

def download(path, &block)
  client.download(combined_path(path), &block)
end

#fresh_clientObject



61
62
63
# File 'app/models/banal_dropbox_api.rb', line 61

def fresh_client
  DropboxApi::Client.new(self.oauth_token)
end

#get_file_path_for_id(dropbox_id) ⇒ Object



16
17
18
# File 'app/models/banal_dropbox_api.rb', line 16

def get_file_path_for_id(dropbox_id)
  client.(dropbox_id).path_lower
end

#hacky__business_api_team_foldersObject



48
49
50
51
52
53
54
55
# File 'app/models/banal_dropbox_api.rb', line 48

def hacky__business_api_team_folders
  `
    curl -X POST https://api.dropboxapi.com/2/team/team_folder/list \
      --header "Authorization: Bearer #{self.oauth_token}" \
      --header "Content-Type: application/json" \
      --data "{\\"limit\\": 100}"
  `
end

#hacky__business_api_team_membersObject



39
40
41
42
43
44
45
46
# File 'app/models/banal_dropbox_api.rb', line 39

def hacky__business_api_team_members
  `
    curl -X POST https://api.dropboxapi.com/2/team/members/list \
      --header "Authorization: Bearer #{self.oauth_token}" \
      --header "Content-Type: application/json" \
      --data "{\\"limit\\": 100,\\"include_removed\\": false}"
  `
end

#inject_middleware(middleware_injactable = self.client, select = :admin, team_member_id = @team_member_id) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/banal_dropbox_api.rb', line 65

def inject_middleware(middleware_injactable = self.client, select = :admin, team_member_id = @team_member_id)
  middleware_injactable.middleware.append do |connection|
    case select
    when :admin
      connection.headers['Dropbox-API-Select-Admin'] = team_member_id
    when :user
      connection.headers['Dropbox-API-Select-User'] = team_member_id
    end

    # This is essential in order to get a value for "path_lower" (#get_file_path_for_id)
    # it's a speciality because of the Business API and team folder
    connection.headers['Dropbox-API-Path-Root'] = '{".tag": "root", "root": "6139129136"}'
  end
end

#instantiate_clientObject



57
58
59
# File 'app/models/banal_dropbox_api.rb', line 57

def instantiate_client
  @client = self.fresh_client
end

#team_folder_pathObject



20
21
22
# File 'app/models/banal_dropbox_api.rb', line 20

def team_folder_path
  "ns:#{@team_folder_id}"
end