examples/api-samples/samples/sample18.rb in groupdocs-1.5.7 vs examples/api-samples/samples/sample18.rb in groupdocs-1.5.8

- old
+ new

@@ -71,17 +71,18 @@ # POST request post '/sample18' do # set variables - set :client_id, params[:client_id] - set :private_key, params[:private_key] + set :client_id, params[:clientId] + set :private_key, params[:privateKey] set :source, params[:source] set :file_id, params[:fileId] set :url, params[:url] - set :convert_type, params[:convert_type] + set :convert_type, params[:convertType] set :callback, params[:callback] + set :base_path, params[:basePath] # Set download path downloads_path = "#{File.dirname(__FILE__)}/../public/downloads" # Remove all files from download directory or create folder if it not there @@ -93,10 +94,22 @@ begin # check required variables raise 'Please enter all required parameters' if settings.client_id.empty? or settings.private_key.empty? + if settings.base_path.empty? then + settings.base_path = 'https://api.groupdocs.com' + end + + # Configure your access to API server + GroupDocs.configure do |groupdocs| + groupdocs.client_id = settings.client_id + groupdocs.private_key = settings.private_key + # Optionally specify API server and version + groupdocs.api_server = settings.base_path # default is 'https://api.groupdocs.com' + end + # Write client and private key to the file for callback job if settings.callback[0] out_file = File.new("#{File.dirname(__FILE__)}/../public/user_info.txt", 'w') # white space is required out_file.write("#{settings.client_id} ") @@ -115,36 +128,51 @@ # construct path file_path = "#{Dir.tmpdir}/#{params[:file][:filename]}" # open file File.open(file_path, 'wb') { |f| f.write(params[:file][:tempfile].read) } # make a request to API using client_id and private_key - file = GroupDocs::Storage::File.upload!(file_path, {}, {:client_id => settings.client_id, :private_key => settings.private_key}) + file = GroupDocs::Storage::File.upload!(file_path, {}) when 'url' # Upload file from defined url - file = GroupDocs::Storage::File.upload_web!(settings.url, {:client_id => settings.client_id, :private_key => settings.private_key}) + file = GroupDocs::Storage::File.upload_web!(settings.url) else raise 'Wrong GUID source.' end # Raise exception if something went wrong raise 'No such file' unless file.is_a?(GroupDocs::Storage::File) # Make document from file document = file.to_document # convert document - convert = document.convert!(settings.convert_type, {:callback => settings.callback}, {:client_id => settings.client_id, :private_key => settings.private_key}) + convert = document.convert!(settings.convert_type, {:callback => settings.callback}) # waite 10 seconds for while file converting sleep(10) # Get array of changes in document from job - original_document = convert.documents!({:client_id => settings.client_id, :private_key => settings.private_key}) + original_document = convert.documents!() # Get converted document GUID guid = original_document[:inputs].first.outputs.first.guid + #Get url from request + case settings.base_path + + when 'https://stage-api-groupdocs.dynabic.com' + url = "http://stage-apps-groupdocs.dynabic.com/document-viewer/embed/#{guid}" + when 'https://dev-api-groupdocs.dynabic.com' + url = "http://dev-apps-groupdocs.dynabic.com/document-viewer/embed/#{guid}" + else + url = "https://apps.groupdocs.com/document-viewer/embed/#{guid}" + end + + # Set iframe with document GUID or raise an error if guid - iframe = "<iframe width='100%' height='600' frameborder='0' src='https://apps.groupdocs.com/document-viewer/embed/#{guid}'></iframe>" + + # Add the signature to url request + iframe = GroupDocs::Api::Request.new(:path => url).prepare_and_sign_url + iframe = "<iframe width='100%' height='600' frameborder='0' src='#{iframe}'></iframe>" else raise 'File was not converted' end rescue Exception => e