Class: SharefileConnect::Data
- Inherits:
-
Object
- Object
- SharefileConnect::Data
- Includes:
- HTTParty
- Defined in:
- lib/sharefile_connect/data.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
- #folder_access_info(id) ⇒ Object
-
#folder_exists?(name, parent_id = nil) ⇒ Boolean
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)) end end.
- #folder_in_parent(name, parent_id) ⇒ Object
-
#initialize(config = nil) ⇒ Data
constructor
A new instance of Data.
- #item(id) ⇒ Object
- #items_by_path(paths) ⇒ Object
- #items_by_path_id(paths) ⇒ Object
- #root(id = nil) ⇒ Object
- #upload_file(folder_id, file, file_name) ⇒ Object
- #upload_file_from_path(folder_id, file_path) ⇒ Object
- #zone_id ⇒ Object
- #zones ⇒ Object
Constructor Details
#initialize(config = nil) ⇒ Data
Returns a new instance of Data.
7 8 9 |
# 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 |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
3 4 5 |
# File 'lib/sharefile_connect/data.rb', line 3 def config @config end |
Instance Method Details
#folder_access_info(id) ⇒ Object
15 16 17 |
# 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) ⇒ Boolean
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: })
else
item(folder_in_parent(name, parent_id)['Id'])
end
end
35 36 37 |
# 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) ⇒ Object
39 40 41 |
# 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) ⇒ Object
19 20 21 |
# File 'lib/sharefile_connect/data.rb', line 19 def item(id) get("/Items(#{id})") end |
#items_by_path(paths) ⇒ Object
43 44 45 |
# 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) ⇒ Object
47 48 49 50 |
# 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) ⇒ Object
11 12 13 |
# 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) ⇒ Object
65 66 67 68 69 70 |
# 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) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# 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 ⇒ Object
76 77 78 |
# 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 ⇒ Object
72 73 74 |
# File 'lib/sharefile_connect/data.rb', line 72 def zones get("/Zones") end |