examples/api-samples/samples/sample35.rb in groupdocs-1.5.8 vs examples/api-samples/samples/sample35.rb in groupdocs-1.5.9
- old
+ new
@@ -1,68 +1,73 @@
-# GET request
+#GET request
get '/sample35' do
haml :sample35
end
-# POST request
+#POST request
post '/sample35' do
- # Set variables
+ #Set variables
set :client_id, params[:clientId]
set :private_key, params[:privateKey]
set :source, params[:source]
set :file_id, params[:fileId]
set :url, params[:url]
- set :path, params[:basePath]
+ set :base_path, params[:basePath]
begin
- # Check required variables
+ #Check required variables
raise 'Please enter all required parameters' if settings.client_id.empty? or settings.private_key.empty?
- if settings.path.empty? then settings.path == 'https://api.groupdocs.com' end
- # clean version if it contains
- path = settings.path.gsub(/\/v2.0/, '')
+ #Prepare base path
+ if settings.base_path.empty?
+ base_path = 'https://api.groupdocs.com'
+ elsif settings.base_path.match('/v2.0')
+ base_path = settings.base_path.split('/v2.0')[0]
+ else
+ base_path = settings.base_path
+ end
- # Configure your access to API server.
+ #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 = path # default is 'https://api.groupdocs.com' https://dev-api-groupdocs.dynabic.com/v2.0
+ #Optionally specify API server and version
+ groupdocs.api_server = base_path # default is 'https://api.groupdocs.com'
end
- # Get document by file GUID
+ #Get document by file GUID
case settings.source
when 'guid'
- # Create instance of File
+ #Create instance of File
file = GroupDocs::Storage::File.new({:guid => settings.file_id})
when 'local'
- # Construct path
+ #Construct path
file_path = "#{Dir.tmpdir}/#{params[:file][:filename]}"
- # Open file
+ #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
+ #Make a request to API using client_id and private_key
file = GroupDocs::Storage::File.upload!(file_path, {})
when 'url'
- # Upload file from defined url
+ #Upload file from defined url
file = GroupDocs::Storage::File.upload_web!(settings.url)
else
raise 'Wrong GUID source.'
end
- # Raise exception if something went wrong
+ #Raise exception if something went wrong
raise 'No such file' unless file.is_a?(GroupDocs::Storage::File)
- # Make GroupDocs::Storage::Document instance
+ #Make GroupDocs::Storage::Document instance
document = file.to_document
- # Get array of document's fields
+ #Get array of document's fields
fields = document.fields!
- # Create the fields for form
+ #Create the fields for form
html = ''
fields.map do |e|
case e.type
when 'Text'
@@ -88,66 +93,68 @@
html << listbox
end
end
-
rescue Exception => e
err = e.message
end
- # Set variables for template
- haml :sample35, :locals => {:userId => settings.client_id, :privateKey => settings.private_key, :base_path => settings.path, :fileId => document.file.guid, :html => html, :err => err}
+ #Set variables for template
+ haml :sample35, :locals => {:userId => settings.client_id, :privateKey => settings.private_key, :fileId => document.file.guid, :base_path => settings.base_path, :html => html, :err => err}
end
-# GET request
+#GET request
get '/sample35/check' do
haml :sample35
end
-# POST request
+#POST request
post '/sample35/check' do
set :client_id, params[:clientId]
set :private_key, params[:privateKey]
- set :path, params[:basePath]
+ set :base_path, params[:basePath]
set :file_id, params[:fileId]
begin
- if settings.path.empty? then settings.path == 'https://api.groupdocs.com' end
+ #Prepare base path
+ if settings.base_path.empty?
+ base_path = 'https://api.groupdocs.com'
+ elsif settings.base_path.match('/v2.0')
+ base_path = settings.base_path.split('/v2.0')[0]
+ else
+ base_path = settings.base_path
+ end
- #clean version if it contains
- path = settings.path.gsub(/\/v2.0/, '')
-
- # Get document by file GUID
+ #Get document by file GUID
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 = path # default is 'https://api.groupdocs.com'
- groupdocs.api_version = '2.0' # default is '2.0'
+ #Optionally specify API server and version
+ groupdocs.api_server = base_path # default is 'https://api.groupdocs.com'
end
#TODO:
#Merge template PDF FIle with the data provided via dynamically created HTML form.
- # Create instance of File
+ #Create instance of File
document = GroupDocs::Storage::File.new({:guid => settings.file_id}).to_document
- # Create datasource with fields
+ #Create datasource with fields
datasource = GroupDocs::DataSource.new
- # Get array of document's fields
+ #Get array of document's fields
fields = document.fields!()
- # Get unique fields
+ #Get unique fields
fields = fields.uniq{ |f| f.name }
datasource.fields = []
- # Create Field instance and fill the fields
+ #Create Field instance and fill the fields
fields.each do |field|
if field.type == 'Text'
datasource.fields << GroupDocs::DataSource::Field.new(name: field.name, type: :text, values: [params[field.name.to_sym]])
end
@@ -182,40 +189,46 @@
end
end
- # Adds datasource.
+ #Adds datasource.
datasource.add!()
- # Creates new job to merge datasource into document.
+ #Creates new job to merge datasource into document.
job = document.datasource!(datasource, {:new_type => 'pdf'})
i = 0
- # Checks status of Job.
+ #Checks status of Job.
while i<5 do
sleep(5)
job_status = GroupDocs::Job.get!(job.id)
break if job_status.status == :archived
i += 1
end
- # Returns an hash of input and output documents associated to job.
+ #Returns an hash of input and output documents associated to job.
document = job.documents!()
- # Set converted document GUID
+ #Set converted document GUID
guid = document[:inputs][0].outputs[0].guid
- case path
-
- when 'https://stage-api-groupdocs.dynabic.com'
- iframe = "<iframe width='100%' height='600' frameborder='0' src='http://stage-apps-groupdocs.dynabic.com/document-viewer/embed/#{guid}'></iframe>"
- when 'https://dev-api-groupdocs.dynabic.com'
- iframe = "<iframe width='100%' height='600' frameborder='0' src='http://dev-apps-groupdocs.dynabic.com/document-viewer/embed/#{guid}'></iframe>"
- else
- iframe = "<iframe width='100%' height='600' frameborder='0' src='https://apps.groupdocs.com/document-viewer/embed/#{guid}'></iframe>"
+ #Prepare to sign url
+ iframe = "/document-viewer/embed/#{guid}"
+ # Construct result string
+ url = GroupDocs::Api::Request.new(:path => iframe).prepare_and_sign_url
+ #Generate iframe URL
+ case base_path
+ when 'https://stage-api-groupdocs.dynabic.com'
+ iframe = "https://stage-apps-groupdocs.dynabic.com/#{url}"
+ when 'https://dev-api-groupdocs.dynabic.com'
+ iframe = "https://dev-apps-groupdocs.dynabic.com#{url}"
+ else
+ iframe = "https://apps.groupdocs.com#{url}"
end
+ #Make iframe
+ iframe = "<iframe id='downloadframe' src='#{iframe}' width='800' height='1000'></iframe>"
rescue Exception => e
err = e.message
end
\ No newline at end of file