Class: BanalDropboxApi

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

Overview

typed: true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBanalDropboxApi

Returns a new instance of BanalDropboxApi.



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

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.



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

def client
  @client
end

#oauth_tokenObject

Returns the value of attribute oauth_token.



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

def oauth_token
  @oauth_token
end

Instance Method Details

#combined_path(path) ⇒ Object



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

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



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

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

#fresh_clientObject



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

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

#get_file_path_for_id(dropbox_id) ⇒ Object



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

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

#hacky__business_api_team_foldersObject



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

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



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

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



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

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



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

def instantiate_client
  @client = self.fresh_client
end

#team_folder_pathObject



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

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