examples/api-samples/samples/sample11.rb in groupdocs-1.5.7 vs examples/api-samples/samples/sample11.rb in groupdocs-1.5.8
- old
+ new
@@ -3,84 +3,136 @@
haml :sample11
end
# POST request
post '/sample11' do
- # set variables
- set :client_id, params[:client_id]
- set :private_key, params[:private_key]
+ # Set variables
+ set :client_id, params[:clientId]
+ set :private_key, params[:privateKey]
set :file_id, params[:fileId]
- set :annotation_type, params[:annotation_type]
+ set :annotation_type, params[:annotationType]
+ set :annotation_id, params[:annotationId]
+ 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? or settings.file_id.empty? or settings.annotation_type.empty?
- # annotation types
- types = {:text => 0, :area => 1, :point => 2}
+ if settings.base_path.empty? then settings.base_path = 'https://api.groupdocs.com' end
- # required parameters
- all_params = all_params = ['annotation_type', 'box_x', 'box_y', 'text']
+ # 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
- # added required parameters depends on annotation type ['text' or 'area']
+ if settings.annotation_id != ''
+
+ file = GroupDocs::Storage::File.new({:guid => settings.file_id}).to_document
+ annotation = file.annotations!()
+
+ # Remove annotation from document
+ remove = annotation.last.remove!()
+ message = "You delete the annotation id = #{remove[:guid]} "
+ else
+ # Annotation types
+ types = {:text => "0", :area => "1", :point => "2"}
+
+
+ # Required parameters
+ all_params = all_params = ['annotationType', 'boxX', 'boxY', 'text']
+
+ # Added required parameters depends on annotation type ['text' or 'area']
if settings.annotation_type == 'text'
- all_params = all_params | ['box_width', 'box_height', 'annotationPosition_x', 'annotationPosition_y']
+ all_params = all_params | ['boxWidth', 'boxHeight', 'annotationPositionX', 'annotationPositionY', 'rangePosition', 'rangeLength']
elsif settings.annotation_type == 'area'
- all_params = all_params | ['box_width', 'box_height']
+ all_params = all_params | ['boxWidth', 'boxHeight']
end
- # checking required parameters
+ # Checking required parameters
all_params.each do |param|
raise 'Please enter all required parameters' if params[param].empty?
end
- # make a request to API using client_id and private_key
- files_list = GroupDocs::Storage::Folder.list!('/', {}, {:client_id => settings.client_id, :private_key => settings.private_key})
+ # Make a request to API using client_id and private_key
+ files_list = GroupDocs::Storage::Folder.list!('/', {})
+
document = ''
- # get document by file ID
+ # Get document by file ID
files_list.each do |element|
if element.respond_to?('guid') == true and element.guid == settings.file_id
document = element
end
end
unless document.instance_of? String
- # start create new annotation
+ # Start create new annotation
annotation = GroupDocs::Document::Annotation.new(document: document.to_document)
- annotation.type = types[settings.annotation_type]
- # construct requestBody depends on annotation type
- # text annotation
- if settings.annotation_type == 'text'
- annotation.box = {x: params['box_x'], y: params['box_y'], width: params['box_width'], height: params['box_height']}
- annotation.annotationPosition = {x: params['annotationPosition_x'], y: params['annotationPosition_y']}
- # area annotation
+ info = nil
+ # Construct requestBody depends on annotation type
+ # Text annotation
+ if settings.annotation_type == 'text'
+ annotation.box = GroupDocs::Document::Rectangle.new ({x: params['boxX'], y: params['boxY'], width: params['boxWidth'], height: params['boxHeight']})
+ annotation.annotationPosition = {x: params['annotationPositionX'], y: params['annotationPositionY']}
+ range = {position: params['rangePosition'], length: params['rangeLength']}
+ info = {:box => annotation_box, :annotationPosition => annotation_annotationPosition, :range => range, :type => types[settings.annotation_type.to_sym], :replies => [{:text => params['text']}]}
+ # Area annotation
elsif settings.annotation_type == 'area'
- annotation.box = {x: params['box_x'], y: params['box_y'], width: params['box_width'], height: params['box_height']}
- annotation.annotationPosition = {x: 0, y: 0}
-
- # point annotation
+ annotation_box = {x: params['boxX'], y: params['boxY'], width: params['boxWidth'], height: params['boxHeight']}
+ annotation_annotationPosition = {x: 0, y: 0}
+ info = {:box => annotation_box, :annotationPosition => annotation_annotationPosition, :type => types[settings.annotation_type.to_sym], :replies => [{:text => params['text']}]}
+ # Point annotation
elsif settings.annotation_type == 'point'
- annotation.box = {x: params['box_x'], y: params['box_y'], width: 0, height: 0}
- annotation.annotationPosition = {x: 0, y: 0}
+ annotation_box = {x: params['boxX'], y: params['boxY'], width: 0, height: 0}
+ annotation_annotationPosition = {x: 0, y: 0}
+
+ info = {:box => annotation_box, :annotationPosition => annotation_annotationPosition, :type => types[settings.annotation_type.to_sym], :replies => [{:text => params['text']}] }
end
- # call create method
- annotation.create!({:client_id => settings.client_id, :private_key => settings.private_key})
- # add annotation reply
- reply = GroupDocs::Document::Annotation::Reply.new(annotation: annotation)
- reply.text = params['text']
- reply.create!({:client_id => settings.client_id, :private_key => settings.private_key})
+ # Call create method
+ annotation.create!(info)
+ id = annotation.document.file.id
+ # Get document guid
+ guid = annotation.document.file.guid
+ case settings.base_path
+
+ when 'https://stage-api-groupdocs.dynabic.com'
+ url = "http://stage-apps-groupdocs.dynabic.com/document-annotation2/embed/#{guid}"
+ when 'https://dev-api-groupdocs.dynabic.com'
+ url = "http://dev-apps-groupdocs.dynabic.com/document-annotation2/embed/#{guid}"
+ else
+ url = "http://apps.groupdocs.com/document-annotation2/embed/#{guid}"
+ end
+
+ #Add the signature in url
+ url = GroupDocs::Api::Request.new(:path => url).prepare_and_sign_url
+ # Set iframe with document GUID
+ iframe = "<iframe src='#{url}' frameborder='0' width='720' height='600'></iframe>"
+
end
+ end
+
rescue Exception => e
err = e.message
end
# set variables for template
- haml :sample11, :locals => {:userId => settings.client_id, :privateKey => settings.private_key, :fileId => settings.file_id, :annotationType => settings.annotation_type, :annotationId => (defined? annotation.id) ? annotation.id : nil, :annotationText => params['text'], :err => err}
+ haml :sample11, :locals => {:clientId => settings.client_id,
+ :privateKey => settings.private_key,
+ :fileId => settings.file_id,
+ :annotationType => settings.annotation_type,
+ :annotationId => id,
+ :annotationText => params['text'],
+ :err => err,
+ :iframe => iframe,
+ :message => message}
end
\ No newline at end of file