groupdocs 1.5.8 → 1.5.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. data/CHANGELOG.md +9 -0
  2. data/examples/api-samples/public/docs/sample01.html +34 -15
  3. data/examples/api-samples/public/docs/sample02.html +36 -15
  4. data/examples/api-samples/public/docs/sample03.html +55 -34
  5. data/examples/api-samples/public/docs/sample04.html +33 -14
  6. data/examples/api-samples/public/docs/sample05.html +51 -33
  7. data/examples/api-samples/public/docs/sample06.html +86 -40
  8. data/examples/api-samples/public/docs/sample07.html +32 -13
  9. data/examples/api-samples/public/docs/sample08.html +39 -20
  10. data/examples/api-samples/public/docs/sample09.html +93 -39
  11. data/examples/api-samples/public/docs/sample10.html +39 -21
  12. data/examples/api-samples/public/docs/sample11.html +133 -88
  13. data/examples/api-samples/public/docs/sample12.html +33 -14
  14. data/examples/api-samples/public/docs/sample13.html +14 -7
  15. data/examples/api-samples/public/docs/sample14.html +37 -18
  16. data/examples/api-samples/public/docs/sample15.html +29 -10
  17. data/examples/api-samples/public/docs/sample16.html +58 -25
  18. data/examples/api-samples/public/docs/sample17.html +71 -38
  19. data/examples/api-samples/public/docs/sample18.html +98 -71
  20. data/examples/api-samples/public/docs/sample19.html +74 -42
  21. data/examples/api-samples/public/docs/sample20.html +39 -21
  22. data/examples/api-samples/public/docs/sample21.html +113 -69
  23. data/examples/api-samples/public/docs/sample22.html +89 -43
  24. data/examples/api-samples/public/docs/sample23.html +50 -32
  25. data/examples/api-samples/public/docs/sample24.html +64 -31
  26. data/examples/api-samples/public/docs/sample25.html +107 -60
  27. data/examples/api-samples/public/docs/sample26.html +29 -10
  28. data/examples/api-samples/public/docs/sample27.html +107 -61
  29. data/examples/api-samples/public/docs/sample28.html +158 -9
  30. data/examples/api-samples/public/docs/sample29.html +13 -8
  31. data/examples/api-samples/public/docs/sample30.html +34 -15
  32. data/examples/api-samples/public/docs/sample31.html +107 -76
  33. data/examples/api-samples/public/docs/sample32.html +104 -40
  34. data/examples/api-samples/public/docs/sample33.html +85 -36
  35. data/examples/api-samples/public/docs/sample34.html +30 -11
  36. data/examples/api-samples/public/docs/sample35.html +90 -27
  37. data/examples/api-samples/public/docs/sample36.html +36 -16
  38. data/examples/api-samples/public/docs/sample37.html +144 -265
  39. data/examples/api-samples/public/docs/sample38.html +113 -64
  40. data/examples/api-samples/public/docs/sample39.html +118 -45
  41. data/examples/api-samples/public/docs/sample40.html +81 -46
  42. data/examples/api-samples/public/docs/sample41.html +562 -90
  43. data/examples/api-samples/public/docs/sample42.html +94 -39
  44. data/examples/api-samples/public/docs/sample43.html +669 -0
  45. data/examples/api-samples/samples/sample01.rb +17 -10
  46. data/examples/api-samples/samples/sample02.rb +21 -12
  47. data/examples/api-samples/samples/sample03.rb +35 -29
  48. data/examples/api-samples/samples/sample04.rb +19 -12
  49. data/examples/api-samples/samples/sample05.rb +24 -17
  50. data/examples/api-samples/samples/sample06.rb +38 -32
  51. data/examples/api-samples/samples/sample07.rb +19 -12
  52. data/examples/api-samples/samples/sample08.rb +22 -15
  53. data/examples/api-samples/samples/sample09.rb +31 -29
  54. data/examples/api-samples/samples/sample10.rb +22 -15
  55. data/examples/api-samples/samples/sample11.rb +78 -81
  56. data/examples/api-samples/samples/sample12.rb +19 -21
  57. data/examples/api-samples/samples/sample13.rb +19 -21
  58. data/examples/api-samples/samples/sample14.rb +18 -12
  59. data/examples/api-samples/samples/sample15.rb +17 -10
  60. data/examples/api-samples/samples/sample16.rb +25 -17
  61. data/examples/api-samples/samples/sample17.rb +31 -23
  62. data/examples/api-samples/samples/sample18.rb +60 -55
  63. data/examples/api-samples/samples/sample19.rb +48 -42
  64. data/examples/api-samples/samples/sample20.rb +20 -13
  65. data/examples/api-samples/samples/sample21.rb +62 -54
  66. data/examples/api-samples/samples/sample22.rb +37 -31
  67. data/examples/api-samples/samples/sample23.rb +24 -17
  68. data/examples/api-samples/samples/sample24.rb +29 -22
  69. data/examples/api-samples/samples/sample25.rb +47 -40
  70. data/examples/api-samples/samples/sample26.rb +16 -9
  71. data/examples/api-samples/samples/sample27.rb +46 -39
  72. data/examples/api-samples/samples/sample28.rb +42 -11
  73. data/examples/api-samples/samples/sample29.rb +13 -8
  74. data/examples/api-samples/samples/sample30.rb +19 -12
  75. data/examples/api-samples/samples/sample31.rb +63 -58
  76. data/examples/api-samples/samples/sample32.rb +45 -42
  77. data/examples/api-samples/samples/sample33.rb +36 -25
  78. data/examples/api-samples/samples/sample34.rb +16 -9
  79. data/examples/api-samples/samples/sample35.rb +66 -53
  80. data/examples/api-samples/samples/sample36.rb +19 -15
  81. data/examples/api-samples/samples/sample37.rb +61 -99
  82. data/examples/api-samples/samples/sample38.rb +51 -43
  83. data/examples/api-samples/samples/sample39.rb +78 -62
  84. data/examples/api-samples/samples/sample40.rb +48 -38
  85. data/examples/api-samples/samples/sample41.rb +189 -67
  86. data/examples/api-samples/samples/sample42.rb +37 -29
  87. data/examples/api-samples/samples/sample43.rb +127 -0
  88. data/examples/api-samples/views/_others_samples.haml +4 -2
  89. data/examples/api-samples/views/index.haml +4 -2
  90. data/examples/api-samples/views/sample11.haml +1 -1
  91. data/examples/api-samples/views/sample28.haml +8 -0
  92. data/examples/api-samples/views/sample31.haml +1 -6
  93. data/examples/api-samples/views/sample39.haml +7 -2
  94. data/examples/api-samples/views/sample41.haml +78 -33
  95. data/examples/api-samples/views/sample42.haml +0 -1
  96. data/examples/api-samples/views/sample43.haml +91 -0
  97. data/lib/groupdocs/api/helpers/access_rights_helper.rb +6 -5
  98. data/lib/groupdocs/api/helpers/url_helper.rb +2 -1
  99. data/lib/groupdocs/document.rb +3 -3
  100. data/lib/groupdocs/version.rb +1 -1
  101. metadata +5 -2
@@ -1,118 +1,240 @@
1
- # GET request
1
+ #GET request
2
2
  get '/sample41' do
3
3
  haml :sample41
4
4
  end
5
5
 
6
- # POST request
6
+ #POST request
7
+ post '/sample41/callback' do
8
+ begin
9
+ #Get callback request
10
+ data = JSON.parse(request.body.read)
11
+ serialized_data = JSON.parse(data['SerializedData'])
12
+
13
+ raise 'Empty params!' if data.empty?
14
+
15
+ file_guid = serialized_data['DocumentGuid']
16
+ collaborator_guid = serialized_data['UserGuid']
17
+ client_id = nil
18
+ private_key = nil
19
+ #Get private key and client_id from file user_info.txt
20
+ if File.exist?("#{File.dirname(__FILE__)}/../public/user_info.txt")
21
+ contents = File.read("#{File.dirname(__FILE__)}/../public/user_info.txt")
22
+ contents = contents.split(' ')
23
+ client_id = contents.first
24
+ private_key = contents.last
25
+ end
26
+ status = nil
27
+ if file_guid != '' and collaborator_guid != ''
28
+ document = GroupDocs::Storage::File.new(:guid => file_guid).to_document
29
+ #Get all collaborators for the document
30
+ get_collaborator = document.collaborators!
31
+ get_collaborator.each do |reviewer|
32
+ #Set reviewer rights to view only
33
+ reviewer.access_rights = %w(view)
34
+ end
35
+ #Make request to API to update reviewer rights
36
+ status = document.set_reviewers! get_collaborator
37
+ end
38
+
39
+ #Create new file callback_info.txt and write the guid document
40
+ out_file = File.new("#{File.dirname(__FILE__)}/../public/callback_info.txt", 'w')
41
+ #White space is required
42
+ out_file.write(status.nil? ? "Error" : "User rights was set to view only")
43
+ out_file.close
44
+
45
+ rescue Exception => e
46
+ err = e.message
47
+ end
48
+ end
49
+
50
+ #POST request
51
+ post '/sample41/check_guid' do
52
+ pp 'test'
53
+ begin
54
+ result = nil
55
+ i = 0
56
+ for i in 1..10
57
+ i +=1
58
+
59
+ #Check is downloads folder exist
60
+ if File.exist?("#{File.dirname(__FILE__)}/../public/callback_info.txt")
61
+ result = File.read("#{File.dirname(__FILE__)}/../public/callback_info.txt")
62
+ if result != ''
63
+ break
64
+ end
65
+ end
66
+ sleep(3)
67
+ end
68
+ #Check result
69
+ if result == 'Error'
70
+ result = "File was not found. Looks like something went wrong."
71
+ else
72
+ result
73
+ end
74
+
75
+ rescue Exception => e
76
+ err = e.message
77
+ end
78
+ end
79
+
80
+
81
+
82
+ #POST request
7
83
  post '/sample41' do
8
- # set variables
84
+ #Set variables
9
85
  set :client_id, params[:clientId]
10
86
  set :private_key, params[:privateKey]
87
+ set :email, params[:email]
11
88
  set :source, params[:source]
12
89
  set :file_id, params[:fileId]
13
90
  set :url, params[:url]
14
91
  set :base_path, params[:basePath]
92
+ set :callback, params[:callbackUrl]
15
93
 
16
- # Set download path
94
+ #Set download path
17
95
  downloads_path = "#{File.dirname(__FILE__)}/../public/downloads"
18
96
 
19
- # Remove all files from download directory or create folder if it not there
97
+ #Remove all files from download directory or create folder if it not there
20
98
  if File.directory?(downloads_path)
21
99
  Dir.foreach(downloads_path) { |f| fn = File.join(downloads_path, f); File.delete(fn) if f != '.' && f != '..' }
100
+ if File.exist?("#{File.dirname(__FILE__)}/../public/callback_info.txt")
101
+ File.delete("#{File.dirname(__FILE__)}/../public/callback_info.txt")
102
+ end
22
103
  else
23
104
  Dir::mkdir(downloads_path)
24
105
  end
25
106
 
26
- begin
27
- # Check required variables
28
- raise 'Please enter all required parameters' if settings.client_id.empty? or settings.private_key.empty?
29
107
 
30
- if settings.base_path.empty? then settings.base_path = 'https://api.groupdocs.com' end
108
+ begin
109
+ #Check required variables
110
+ raise 'Please enter all required parameters' if settings.client_id.empty? or settings.private_key.empty? or settings.email.empty?
111
+
112
+ #Prepare base path
113
+ if settings.base_path.empty?
114
+ base_path = 'https://api.groupdocs.com'
115
+ elsif settings.base_path.match('/v2.0')
116
+ base_path = settings.base_path.split('/v2.0')[0]
117
+ else
118
+ base_path = settings.base_path
119
+ end
31
120
 
32
- # Configure your access to API server
121
+ #Configure your access to API server
33
122
  GroupDocs.configure do |groupdocs|
34
123
  groupdocs.client_id = settings.client_id
35
124
  groupdocs.private_key = settings.private_key
36
- # Optionally specify API server and version
37
- groupdocs.api_server = settings.base_path # default is 'https://api.groupdocs.com'
125
+ #Optionally specify API server and version
126
+ groupdocs.api_server = base_path # default is 'https://api.groupdocs.com'
127
+
38
128
  end
39
129
 
40
- # Get document by file GUID
130
+ #Write client and private key to the file for callback job
131
+ if settings.callback
132
+ out_file = File.new("#{File.dirname(__FILE__)}/../public/user_info.txt", 'w')
133
+ #White space is required
134
+ out_file.write("#{settings.client_id} ")
135
+ out_file.write("#{settings.private_key}")
136
+ out_file.close
137
+ end
138
+
139
+ #Get document by file GUID
41
140
  case settings.source
42
141
  when 'guid'
43
- # Create instance of File
142
+ #Create instance of File
44
143
  file = GroupDocs::Storage::File.new({:guid => settings.file_id})
45
144
  when 'local'
46
- # Construct path
145
+ #Construct path
47
146
  file_path = "#{Dir.tmpdir}/#{params[:file][:filename]}"
48
- # Open file
147
+ #Open file
49
148
  File.open(file_path, 'wb') { |f| f.write(params[:file][:tempfile].read) }
50
- # Make a request to API using client_id and private_key
149
+ #Make a request to API using client_id and private_key
51
150
  file = GroupDocs::Storage::File.upload!(file_path, {})
52
151
  when 'url'
53
- # Upload file from defined url
152
+ #Upload file from defined url
54
153
  file = GroupDocs::Storage::File.upload_web!(settings.url)
55
154
  else
56
155
  raise 'Wrong GUID source.'
57
156
  end
58
-
59
- # Raise exception if something went wrong
60
- raise 'No such file' unless file.is_a?(GroupDocs::Storage::File)
61
-
62
- # Make GroupDocs::Storage::Document instance
157
+ guid = file.guid
158
+ #Create document object
63
159
  document = file.to_document
64
-
65
- # Create Hash with the options for job. :status=> -1 means the Draft status of the job
66
- options = {:actions => [:convert, :number_lines], :out_formats => ['doc'], :name => 'sample'}
67
-
68
- # Create Job with provided options with Draft status (Sheduled job)
69
- job = GroupDocs::Job.create!(options)
70
-
71
- # Add the documents to previously created Job
72
- job.add_document!(document, {:check_ownership => false})
73
-
74
-
75
- # Update the Job with new status. :status => '0' mean Active status of the job (Start the job)
76
- id = job.update!({:status => 'pending'})
77
-
78
- i = 1
79
-
80
- while i<5 do
81
- sleep(5)
82
- job = GroupDocs::Job.get!(id[:job_id])
83
- break if job.status == :archived
84
- i = i + 1
85
- end
86
-
87
- # Get the document into Pdf format
88
- file = job.documents!()
89
-
90
- document = file[:inputs][0].outputs[0]
91
-
92
- # Set iframe with document GUID or raise an error
93
- if document
94
-
95
- #Get url from request
96
- case settings.base_path
97
-
160
+ #Set file sesion callback - will be trigered when user add, remove or edit commit for annotation
161
+ session = document.set_session_callback! settings.callback
162
+
163
+
164
+ #Get all users from accaunt
165
+ users = GroupDocs::User.new.users!
166
+ user_guid = nil
167
+ # Number of collaborators
168
+ number = Array.new
169
+ if users
170
+ #Pass of each email
171
+ settings.email.each do |email|
172
+ #Pass of each user and get user GUID if user with same email already exist
173
+ users.map do |user|
174
+ if user.primary_email == email
175
+ #Get user GUID
176
+ user_guid = user.guid
177
+ break
178
+ end
179
+ end
180
+
181
+ #Check is user with entered email was founded in GroupDocs account, if not user will be created
182
+ if user_guid.nil?
183
+ #Create new User object
184
+ userNew = GroupDocs::User.new
185
+ #Set email as entered email
186
+ userNew.primary_email = settings.email
187
+ #Set first name as entered first name
188
+ userNew.firstname = settings.email
189
+ #Set last name as entered last name
190
+ userNew.lastname = settings.email
191
+ #Set roles
192
+ userNew.roles = [{:id => '3', :name => 'User'}]
193
+
194
+ #Update account
195
+ new_user = GroupDocs::User.update_account!(userNew)
196
+ #Get user GUID
197
+ user_guid = new_user.guid
198
+ end
199
+ #Get all collaborators for current document
200
+ collaborators = document.collaborators!
201
+
202
+ if collaborators
203
+ #Pass of each collaborator
204
+ collaborators.map do |collaborator|
205
+ #Check is user with entered email already in collaborators
206
+ if collaborator.primary_email == email
207
+ number << collaborator.guid
208
+ end
209
+ end
210
+ end
211
+ end
212
+ #Delete empty email
213
+ if settings.email[1].empty? then settings.email.delete("") end
214
+ #Add user as collaborators for the document
215
+ document.set_collaborators! settings.email if number.size < 2
216
+ #Add user GUID as "uid" parameter to the iframe URL
217
+ iframe = "/document-annotation2/embed/#{file.guid}?uid=#{user_guid}"
218
+ # Construct result string
219
+ url = GroupDocs::Api::Request.new(:path => iframe).prepare_and_sign_url
220
+ #Generate iframe URL
221
+ case base_path
98
222
  when 'https://stage-api-groupdocs.dynabic.com'
99
- url = "http://stage-apps-groupdocs.dynabic.com/document-viewer/embed/#{document.guid}"
223
+ iframe = "https://stage-api-groupdocs.dynabic.com#{url}"
100
224
  when 'https://dev-api-groupdocs.dynabic.com'
101
- url = "http://dev-apps-groupdocs.dynabic.com/document-viewer/embed/#{document.guid}"
225
+ iframe = "https://dev-apps.groupdocs.com#{url}"
102
226
  else
103
- url = "https://apps.groupdocs.com/document-viewer/embed/#{document.guid}"
227
+ iframe = "https://apps.groupdocs.com#{url}"
104
228
  end
105
229
 
106
- # Add the signature in url
107
- iframe = GroupDocs::Api::Request.new(:path => url).prepare_and_sign_url
108
- iframe = "<iframe width='100%' height='600' frameborder='0' src='#{iframe}'></iframe>"
109
- else
110
- raise 'File was not converted'
230
+ iframe = "<iframe src='#{iframe}' id='downloadframe' width='800' height='1000'></iframe>"
111
231
  end
232
+
233
+
112
234
  rescue Exception => e
113
235
  err = e.message
114
236
  end
115
237
 
116
- # Set variables for template
117
- haml :sample41, :locals => {:userId => settings.client_id, :privateKey => settings.private_key, :err => err, :iframe => iframe}
238
+ #Set variables for template
239
+ haml :sample41, :locals => {:userId => settings.client_id, :fileId => settings.file_id, :privateKey => settings.private_key, :iframe => iframe, :callbackUrl => settings.callback, :err => err}
118
240
  end
@@ -1,46 +1,54 @@
1
- # GET request
1
+ #GET request
2
2
  get '/sample42' do
3
3
  haml :sample42
4
4
  end
5
5
 
6
- # POST request
6
+ #POST request
7
7
  post '/sample42' do
8
- # set variables
8
+ #Set variables
9
9
  set :client_id, params[:clientId]
10
10
  set :private_key, params[:privateKey]
11
11
  set :file_id, params[:fileId]
12
12
  set :base_path, params[:basePath]
13
13
 
14
14
  begin
15
- # Check required variables
15
+ #Check required variables
16
16
  raise 'Please enter all required parameters' if settings.client_id.empty? or settings.private_key.empty?
17
17
 
18
- if settings.base_path.empty? then settings.base_path = 'https://api.groupdocs.com' end
18
+ #Prepare base path
19
+ if settings.base_path.empty?
20
+ base_path = 'https://api.groupdocs.com'
21
+ elsif settings.base_path.match('/v2.0')
22
+ base_path = settings.base_path.split('/v2.0')[0]
23
+ else
24
+ base_path = settings.base_path
25
+ end
19
26
 
20
- # Configure your access to API server
27
+ #Configure your access to API server
21
28
  GroupDocs.configure do |groupdocs|
22
29
  groupdocs.client_id = settings.client_id
23
30
  groupdocs.private_key = settings.private_key
24
- # Optionally specify API server and version
25
- groupdocs.api_server = settings.base_path # default is 'https://api.groupdocs.com'
31
+ #Optionally specify API server and version
32
+ groupdocs.api_server = base_path # default is 'https://api.groupdocs.com'
33
+
26
34
  end
27
35
 
28
36
  document = GroupDocs::Storage::File.new(:guid => settings.file_id).to_document
29
37
 
30
- # Check, has document the annotations?
38
+ #Check, has document the annotations?
31
39
  raise 'Your document has no annotations' if document.annotations!.empty?
32
40
 
33
- # Create Hash with the options for job. :status=> -1 means the Draft status of the job
41
+ #Create Hash with the options for job. :status=> -1 means the Draft status of the job
34
42
  options = {:actions => [:import_annotations], :name => 'sample'}
35
43
 
36
- # Create Job with provided options with Draft status (Sheduled job)
44
+ #Create Job with provided options with Draft status (Sheduled job)
37
45
  job = GroupDocs::Job.create!(options)
38
46
 
39
- # Add the documents to previously created Job
47
+ #Add the documents to previously created Job
40
48
  job.add_document!(document, {:check_ownership => false})
41
49
 
42
50
 
43
- # Update the Job with new status. :status => '0' mean Active status of the job (Start the job)
51
+ #Update the Job with new status. :status => '0' mean Active status of the job (Start the job)
44
52
  id = job.update!({:status => '0'})
45
53
 
46
54
  i = 1
@@ -52,39 +60,39 @@ post '/sample42' do
52
60
  i = i + 1
53
61
  end
54
62
 
55
- # Get the document into Pdf format
63
+ #Get the document into Pdf format
56
64
  file = job.documents!()
57
65
 
58
66
  document = file[:inputs]
59
67
 
60
- # Set iframe with document GUID or raise an error
68
+ #Set iframe with document GUID or raise an error
61
69
  if document
62
-
63
- #Get url from request
64
- case settings.base_path
65
-
70
+ #Add the signature in url
71
+ iframe = "/document-annotation2/embed/#{document[0].guid}"
72
+ # Construct result string
73
+ url = GroupDocs::Api::Request.new(:path => iframe).prepare_and_sign_url
74
+ #Generate iframe URL
75
+ case base_path
66
76
  when 'https://stage-api-groupdocs.dynabic.com'
67
- url = "http://stage-apps-groupdocs.dynabic.com/document-annotation/embed/#{document[0].guid}"
77
+ iframe = "https://stage-api-groupdocs.dynabic.com#{url}"
68
78
  when 'https://dev-api-groupdocs.dynabic.com'
69
- url = "http://dev-apps-groupdocs.dynabic.com/document-annotation/embed/#{document[0].guid}"
79
+ iframe = "https://dev-apps.groupdocs.com#{url}"
70
80
  else
71
- url = "https://apps.groupdocs.com/document-annotation/embed/#{document[0].guid}"
81
+ iframe = "https://apps.groupdocs.com#{url}"
72
82
  end
73
83
 
74
- # Add the signature in url
75
- iframe = GroupDocs::Api::Request.new(:path => url).prepare_and_sign_url
76
- iframe = "<iframe width='100%' height='600' frameborder='0' src='#{iframe}'></iframe>"
84
+ iframe = "<iframe width='100%' height='600' id='downloadframe' frameborder='0' src='#{iframe}'></iframe>"
77
85
 
78
86
  path = "#{File.dirname(__FILE__)}/../public/downloads"
79
- # Remove all files from download directory or create folder if it not there
87
+ #Remove all files from download directory or create folder if it not there
80
88
  if File.directory?(path)
81
- Dir.foreach(path) { |f| fn = File.join(downloads_path, f); File.delete(fn) if f != '.' && f != '..' }
89
+ Dir.foreach(path) { |f| fn = File.join(path, f); File.delete(fn) if f != '.' && f != '..' }
82
90
  else
83
91
  Dir::mkdir(path)
84
92
  end
85
93
 
86
94
  GroupDocs::User.download!(path, document[0].outputs[0].name, document[0].outputs[0].guid)
87
- message = "<span style=\"color:green\">File with annotations was downloaded to server's local folder. You can check them <a href=\"/downloads/#{document[0].outputs[0].name}\">here</a></span>"
95
+ message = "<span style=\"color:green\">File with annotations was downloaded to server's local folder. You can check them <a href=\"/downloads/#{document[0].outputs[0].name}\" download>here</a></span>"
88
96
  else
89
97
  raise 'File was not converted'
90
98
  end
@@ -92,6 +100,6 @@ post '/sample42' do
92
100
  err = e.message
93
101
  end
94
102
 
95
- # Set variables for template
103
+ #Set variables for template
96
104
  haml :sample42, :locals => {:userId => settings.client_id, :privateKey => settings.private_key, :err => err, :iframe => iframe, :message => message}
97
105
  end
@@ -0,0 +1,127 @@
1
+ #GET request
2
+ get '/sample43' do
3
+ haml :sample43
4
+ end
5
+
6
+ #POST request
7
+ post '/sample43' do
8
+ #Set variables
9
+ set :client_id, params[:clientId]
10
+ set :private_key, params[:privateKey]
11
+ set :source, params[:source]
12
+ set :file_id, params[:fileId]
13
+ set :url, params[:url]
14
+ set :base_path, params[:basePath]
15
+
16
+ #Set download path
17
+ downloads_path = "#{File.dirname(__FILE__)}/../public/downloads"
18
+
19
+ #Remove all files from download directory or create folder if it not there
20
+ if File.directory?(downloads_path)
21
+ Dir.foreach(downloads_path) { |f| fn = File.join(downloads_path, f); File.delete(fn) if f != '.' && f != '..' }
22
+ else
23
+ Dir::mkdir(downloads_path)
24
+ end
25
+
26
+ begin
27
+ #Check required variables
28
+ raise 'Please enter all required parameters' if settings.client_id.empty? or settings.private_key.empty?
29
+
30
+ #Prepare base path
31
+ if settings.base_path.empty?
32
+ base_path = 'https://api.groupdocs.com'
33
+ elsif settings.base_path.match('/v2.0')
34
+ base_path = settings.base_path.split('/v2.0')[0]
35
+ else
36
+ base_path = settings.base_path
37
+ end
38
+
39
+ #Configure your access to API server
40
+ GroupDocs.configure do |groupdocs|
41
+ groupdocs.client_id = settings.client_id
42
+ groupdocs.private_key = settings.private_key
43
+ #Optionally specify API server and version
44
+ groupdocs.api_server = base_path # default is 'https://api.groupdocs.com'
45
+
46
+ end
47
+
48
+ #Get document by file GUID
49
+ case settings.source
50
+ when 'guid'
51
+ #Create instance of File
52
+ file = GroupDocs::Storage::File.new({:guid => settings.file_id})
53
+ when 'local'
54
+ #Construct path
55
+ file_path = "#{Dir.tmpdir}/#{params[:file][:filename]}"
56
+ #Open file
57
+ File.open(file_path, 'wb') { |f| f.write(params[:file][:tempfile].read) }
58
+ #Make a request to API using client_id and private_key
59
+ file = GroupDocs::Storage::File.upload!(file_path, {})
60
+ when 'url'
61
+ #Upload file from defined url
62
+ file = GroupDocs::Storage::File.upload_web!(settings.url)
63
+ else
64
+ raise 'Wrong GUID source.'
65
+ end
66
+
67
+ #Raise exception if something went wrong
68
+ raise 'No such file' unless file.is_a?(GroupDocs::Storage::File)
69
+
70
+ #Make GroupDocs::Storage::Document instance
71
+ document = file.to_document
72
+
73
+ #Create Hash with the options for job. :status=> -1 means the Draft status of the job
74
+ options = {:actions => [:convert, :number_lines], :out_formats => ['doc'], :name => 'sample'}
75
+
76
+ #Create Job with provided options with Draft status (Sheduled job)
77
+ job = GroupDocs::Job.create!(options)
78
+
79
+ #Add the documents to previously created Job
80
+ job.add_document!(document, {:check_ownership => false})
81
+
82
+
83
+ #Update the Job with new status. :status => '0' mean Active status of the job (Start the job)
84
+ id = job.update!({:status => 'pending'})
85
+
86
+ i = 1
87
+
88
+ while i<5 do
89
+ sleep(5)
90
+ job = GroupDocs::Job.get!(id[:job_id])
91
+ break if job.status == :archived
92
+ i = i + 1
93
+ end
94
+
95
+ #Get the document into Pdf format
96
+ file = job.documents!()
97
+
98
+ document = file[:inputs][0].outputs[0]
99
+
100
+ #Set iframe with document GUID or raise an error
101
+ if document
102
+
103
+ #Add the signature in url
104
+ iframe = "/document-viewer/embed/#{document.guid}"
105
+ # Construct result string
106
+ url = GroupDocs::Api::Request.new(:path => iframe).prepare_and_sign_url
107
+ #Generate iframe URL
108
+ case base_path
109
+ when 'https://stage-api-groupdocs.dynabic.com'
110
+ iframe = "https://stage-api-groupdocs.dynabic.com#{url}"
111
+ when 'https://dev-api-groupdocs.dynabic.com'
112
+ iframe = "https://dev-apps.groupdocs.com#{url}"
113
+ else
114
+ iframe = "https://apps.groupdocs.com#{url}"
115
+ end
116
+
117
+ iframe = "<iframe width='100%' height='600' id='downloadframe' frameborder='0' src='#{iframe}'></iframe>"
118
+ else
119
+ raise 'File was not converted'
120
+ end
121
+ rescue Exception => e
122
+ err = e.message
123
+ end
124
+
125
+ #Set variables for template
126
+ haml :sample43, :locals => {:userId => settings.client_id, :privateKey => settings.private_key, :err => err, :iframe => iframe}
127
+ end