class SharefileConnect::Data

Attributes

config[RW]

Public Class Methods

new(config = nil) click to toggle source
# File lib/sharefile_connect/data.rb, line 7
def initialize(config = nil)
  @config = config || SharefileConnect::Config.new(ENV['SHAREFILE_KEY'], ENV['SHAREFILE_SECRET'], ENV['SHAREFILE_USER_NAME'], ENV['SHAREFILE_USER_PASS'], ENV['API_ENDPOINT_DOMAIN'])
end

Public Instance Methods

folder_access_info(id) click to toggle source
# File lib/sharefile_connect/data.rb, line 15
def folder_access_info(id)
  parse_get("/Items(#{id})/Info").body
end
folder_exists?(name, parent_id = nil) click to toggle source

def create_folder parent_id, name, description = ''

unless folder_exists?(name, parent_id)
  body = {
      "Name"        => name,
      "Description" => description || name
  }
  HTTParty.post(full("Items#{parent_id}/Folder?overwrite=false&passthrough=false"), { body: body.to_json, headers: authorization_header})
else
  item(folder_in_parent(name, parent_id)['Id'])
end

end

# File lib/sharefile_connect/data.rb, line 35
def folder_exists?(name, parent_id = nil)
  folder_in_parent(name, parent_id).any?
end
folder_in_parent(name, parent_id) click to toggle source
# File lib/sharefile_connect/data.rb, line 39
def folder_in_parent(name, parent_id)
  JSON.parse(root(parent_id))['Children'].select { |f| f['Name'] == name }
end
item(id) click to toggle source
# File lib/sharefile_connect/data.rb, line 19
def item(id)
  get("/Items(#{id})")
end
items_by_path(paths) click to toggle source
# File lib/sharefile_connect/data.rb, line 43
def items_by_path(paths)
  parse_get("/Items/ByPath?path=/#{paths.join('/')}/&$expand=Children&$select=Id,Name,Children/Id,Children/Name")
end
items_by_path_id(paths) click to toggle source
# File lib/sharefile_connect/data.rb, line 47
def items_by_path_id(paths)
  r = items_by_path(paths).response
  JSON.parse(r.body)['Id'] if r.kind_of?(Net::HTTPOK)
end
root(id = nil) click to toggle source
# File lib/sharefile_connect/data.rb, line 11
def root(id = nil)
  parse_get("/Items(#{id || 'allshared'})?$expand=Children&$select=Id,Name,Children/Id,Children/Name").body
end
upload_file(folder_id, file, file_name) click to toggle source
# File lib/sharefile_connect/data.rb, line 65
def upload_file(folder_id, file, file_name)
  path          = "/Items(#{folder_id})/Upload"
  response      = get(path)
  upload_config = JSON.parse response.body
  multipart_form_post upload_config['ChunkUri'], file, file_name
end
upload_file_from_path(folder_id, file_path) click to toggle source
# File lib/sharefile_connect/data.rb, line 52
def upload_file_from_path(folder_id, file_path)
  path          = "/Items(#{folder_id})/Upload"
  response      = get(path)
  upload_config = JSON.parse response.body
  multipart_form_post upload_config['ChunkUri'], File.read(file_path), File.basename(file_path)
  # HTTMultiParty.post(upload_config["ChunkUri"], body: { file1: File.new(file_path) } )
  # File.open(file_path) do |transfile|
  #   # HTTMultiParty.post(upload_config["ChunkUri"], query: { file1: File.read(transfile) })
  #   # HTTMultiParty.post(upload_config["ChunkUri"], query: { file1: UploadIO.new(transfile, "multipart/formdata", File.basename(file_path)) })
  #   # HTTMultiParty.post(upload_config["ChunkUri"], query: { file1: UploadIO.new(File.open(file_path), "multipart/formdata") })
  # end
end
zone_id() click to toggle source
# File lib/sharefile_connect/data.rb, line 76
def zone_id
  JSON.parse(zones.response.body)['value'].map { |x| x['Id'] if x['ZoneType'] == 'CitrixManaged' }.compact.first
end
zones() click to toggle source
# File lib/sharefile_connect/data.rb, line 72
def zones
  get("/Zones")
end

Private Instance Methods

authorization_header() click to toggle source
# File lib/sharefile_connect/data.rb, line 126
def authorization_header
  return { "Authorization" => "Bearer #{connection['access_token']}" }
end
connection() click to toggle source
# File lib/sharefile_connect/data.rb, line 110
def connection
  @connection ||= SharefileConnect::Connection.new(config).token
end
full(path) click to toggle source
# File lib/sharefile_connect/data.rb, line 114
def full path
  "https://#{config.domain}.sf-api.com/sf/v3#{path}"
end
get(path) click to toggle source
# File lib/sharefile_connect/data.rb, line 122
def get(path)
  HTTParty.get(full(path), headers: authorization_header)
end
multipart_form_post(url, file, file_name) click to toggle source
# File lib/sharefile_connect/data.rb, line 82
def multipart_form_post(url, file, file_name)
  newline  = "\r\n"
  filename = file_name
  boundary = "----------#{Time.now.nsec}"

  uri = URI.parse(url)

  post_body = []
  post_body << "--#{boundary}#{newline}"
  post_body << "Content-Disposition: form-data; name=\"File1\"; filename=\"#{filename}\"#{newline}"
  post_body << "Content-Type: application/octet-stream#{newline}"
  post_body << "#{newline}"
  post_body << file
  post_body << "#{newline}--#{boundary}--#{newline}"

  request                   = Net::HTTP::Post.new(uri.request_uri)
  request.body              = post_body.join
  request['Content-Type']   = "multipart/form-data, boundary=#{boundary}"
  request['Content-Length'] = request.body().length

  http             = Net::HTTP.new uri.host, uri.port
  http.use_ssl     = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  http.request request
end
parse_get(path) click to toggle source
# File lib/sharefile_connect/data.rb, line 118
def parse_get(path)
  get(path)
end