Class: CarrierWave::Sharefile::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/carrierwave/sharefile/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, username, password, subdomain) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/carrierwave/sharefile/client.rb', line 13

def initialize(client_id, client_secret, username, password, subdomain)
  @client_id = client_id
  @client_secret = client_secret
  @username = username
  @password = password
  @subdomain = subdomain
  instance_variables.each do |variable|
    raise ArgumentError, "#{variable} should not be nil or blank" if instance_variable_get(variable.to_sym).to_s == ""
  end
  access_token
end

Instance Method Details

#access_tokenObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/carrierwave/sharefile/client.rb', line 25

def access_token
  params = {
    :grant_type => :password,
    :client_id => @client_id,
    :client_secret => @client_secret,
    :username => @username,
    :password => @password
  }
  response = connection("sharefile").post 'oauth/token', params
  @access_token = response.body['access_token']
  @refresh_token = response.body['refresh_token']
end

#delete_document(path) ⇒ Object



43
44
45
46
47
# File 'lib/carrierwave/sharefile/client.rb', line 43

def delete_document(path)
  res = get_item_by_path(path)
  id = res.body["Id"]
  response = delete_item_by_id(id)
end

#get_document(identifier) ⇒ Object



39
40
41
# File 'lib/carrierwave/sharefile/client.rb', line 39

def get_document(identifier)
  response = get_item_by_id(identifier)
end


49
50
51
52
53
54
55
56
57
# File 'lib/carrierwave/sharefile/client.rb', line 49

def get_download_link(path)
  headers = {"Authorization" => "Bearer #{@access_token}"}
  res = get_item_by_path(path)
  id = res.body["Id"]
  response = connection.get "sf/v3/Items(#{id})/Download", {}, headers
  if response.headers['location']
    return response.headers['location']
  end
end


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/carrierwave/sharefile/client.rb', line 59

def get_multi_download_link(path, ids)
  headers = {"Authorization" => "Bearer #{@access_token}"}
  res = get_item_by_path(path)
  puts res
  id = res.body["Id"]
  puts id
  puts '------'
  puts ids
  response = connection.post "sf/v3/Items(#{id})/BulkDownload", {ids: ids}, headers
  puts '+++++++'
  puts response.inspect
  if response.headers['location']
    return response.headers['location']
  end
end

#store_document(root_folder, store_path, file) ⇒ Object



75
76
77
78
79
# File 'lib/carrierwave/sharefile/client.rb', line 75

def store_document(root_folder, store_path, file)
  folder = get_item_by_path(root_folder)
  upload_config = upload_file_to_folder(folder)
  res = upload_media(upload_config.body['ChunkUri'], file)
end