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



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

#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


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

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

#store_document(root_folder, store_path, file) ⇒ Object



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

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