Setting Up
First, create the application folders structure:- your_app/public – for CSS files.
- your_app/samples – for sample files.
- your_app/views – for view files, we will use Haml for these.
Writing the Application
Lets start our application. Create app.rb file with following content: https://gist.github.com/averjr/4739208- Lines 1-3 requires the Sinatra framework, GroupDocs SDK and haml for our views.
- Lines 5-7 creates our first root with a view named index.haml. By default, Sinatra looks for view files in views directory.
- Line 13 includes all Ruby files from the samples directory.
The Starting Point
Before creating the envelope sample, lets look at what we have. To avoid problems with starting the application, use rvm. (This article explains how to install rvm.) Now install a local bundle by typing this command in a terminal, while in the app directory:gem install bundler
bundle install --path vendor/bundle
bundle exec ruby app.rb

Terminal - Terminal massages after starting Sinatra server

Index page - Index page of our app without CSS

Index page with CSS - After adding CSS the page looks like this
Creating the Envelope Example
Lets create envelope_sample.haml in your views directory with the following content:https://gist.github.com/averjr/4739266 We have created a form that sends the ClientID, PrivateKey and the file to be signed. Now write the Ruby code that handles the get and post request as the file envelope-sample.rb in samples directory: https://gist.github.com/averjr/4739271 We have created one GET and one POST request.Extending the POST Request
Lets extend post request and use the GroupDocs API: 1. Configuring GroupDocs SDK.GroupDocs.configure do |groupdocs| groupdocs.client_id = params[:client_id] groupdocs.private_key = params[:private_key] groupdocs.api_server = 'https://stage-api.groupdocs.com' end
filepath = "#{Dir.tmpdir}/#{params[:file][:filename]}"
File.open(filepath, 'wb') { |f| f.write(params[:file][:tempfile].read) }
file = GroupDocs::Storage::File.upload!(filepath, {}, client_id: settings.client_id,
private_key: settings.private_key)
document = file.to_documentenvelope = GroupDocs::Signature::Envelope.new envelope.name = "Envelope" envelope.email_subject = "Sing this!" envelope.create!
envelope.add_document! document
document = envelope.documents!.first
roles = GroupDocs::Signature::Role.get!
recipient = GroupDocs::Signature::Recipient.new
recipient.email = 'john@smith.com'
recipient.first_name = 'John'
recipient.last_name = 'Smith'
recipient.role_id = roles.detect { |role| role.name == "Signer" }.id
envelope.add_recipient! recipientrecipient = envelope.recipients!.first
# add city field to envelope
field = GroupDocs::Signature::Field.get!.detect { |f| f.type == :single_line }
field.name = 'City' field.location = { location_x: 0.3, location_y: 0.2, page: 1 }
envelope.add_field! field, document, recipient
# add signature field to envelope
field = GroupDocs::Signature::Field.get!.detect { |f| f.type == :signature }
field.location = { location_x: 0.3, location_y: 0.3, page: 1 }
envelope.add_field! field, document, recipient webhook = 'http://groupdocs-ruby-samples.herokuapp.com/envelope-sample/sign'
envelope.send! webhook url = "https://stage-apps.groupdocs.com/signature/signembed/#{envelope.id}/#{recipient.id}"
iframe = "<iframe src='#{url}' frameborder='0' width='720' height='600'></iframe>"haml :envelope_sample, :locals => { :client_id => settings.client_id, :private_key => settings.private_key,
:err => err, :iframe => iframe} begin
raise "Please enter all required parameters" if settings.client_id.empty? or settings.private_key.empty?
....
rescue Exception => e
err = e.message
endHandling a Callback
Lets handle the callback: 1. First, wait for callback and create a text file with parameters that the server sent to us:post '/envelope-sample/sign' do
# Content Type of callback is application/json
data = JSON.parse(request.body.read)
begin
raise "Empty params!" if data.empty?
#create empty file and write data as "key: value" to it
outFile = File.new("signed", "w")
data.each do |key, value|
outFile.write("#{key}: #{value} \n")
end
outFile.close
rescue Exception => e
err = e.message
end
endpost '/envelope-sample/sign-and-download' do
data = JSON.parse(request.body.read)
begin
raise "Empty params!" if data.empty?
GroupDocs.configure do |groupdocs|
groupdocs.client_id = '' # Your client Client ID here
groupdocs.private_key = '' # Your API Key here
groupdocs.api_server = 'https://api.groupdocs.com'
end
data.each do |key, value|
if key == 'SourceId'
# Create envelop with id and name as SourceId parameter from callback
envelope = GroupDocs::Signature::Envelope.new id: value,
name: value
# download signed documents as archive
envelope.signed_documents! '.'
end
end
rescue Exception => e
err = e.message
end
endget '/envelope-sample/check' do
if File.exist?('signed')
File.readlines('signed').each do |line|
end
else
'Have not signed yet'
end
end

