Skip to content

Commit 0d081c2

Browse files
authored
Merge pull request #192 from docusign/feature/webforms-example-2
Add second Web Forms example
2 parents 637a327 + 9fad807 commit 0d081c2

File tree

11 files changed

+370
-132
lines changed

11 files changed

+370
-132
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ gem 'docusign_click', '~> 1.4.0'
7373
gem 'docusign_esign', '~> 5.1.0'
7474
gem 'docusign_monitor', '~> 1.2.0'
7575
gem 'docusign_rooms', '~> 1.3.0'
76-
gem 'docusign_webforms', '~> 2.0.0'
76+
gem 'docusign_webforms', '~> 2.1.0'
7777
gem 'omniauth-oauth2', '~> 1.8.0'
7878
gem 'omniauth-rails_csrf_protection'
7979

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ GEM
134134
json (~> 2.1, >= 2.1.0)
135135
jwt (~> 2.2, >= 2.2.1)
136136
typhoeus (~> 1.0, >= 1.0.1)
137-
docusign_webforms (2.0.0)
137+
docusign_webforms (2.1.0)
138138
addressable (~> 2.7, >= 2.7.0)
139139
json (~> 2.1, >= 2.1.0)
140140
jwt (~> 2.2, >= 2.2.1)
@@ -401,7 +401,7 @@ DEPENDENCIES
401401
docusign_esign (~> 5.1.0)
402402
docusign_monitor (~> 1.2.0)
403403
docusign_rooms (~> 1.3.0)
404-
docusign_webforms (~> 2.0.0)
404+
docusign_webforms (~> 2.1.0)
405405
jbuilder (~> 2.13.0)
406406
listen (~> 3.9.0)
407407
matrix (~> 0.4.2)

app/controllers/eg_controller.rb

Lines changed: 110 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,110 @@
1-
# frozen_string_literal: true
2-
3-
class EgController < ApplicationController
4-
skip_before_action :verify_authenticity_token
5-
before_action :eg_name, :set_eg, :set_meta, :ensure_manifest
6-
7-
def file_name
8-
"#{controller_path}_service.rb"
9-
end
10-
11-
def eg_name
12-
controller_name.to(4)
13-
end
14-
15-
def set_eg
16-
session[:eg] = controller_name.split('_', 2).first
17-
end
18-
19-
def get
20-
@messages = ''
21-
22-
# to have the user authenticate or re-authenticate.
23-
@token_ok = check_token
24-
@config = Rails.application.config
25-
if @token_ok || controller_name.include?('cneg')
26-
# addSpecialAttributes(model)
27-
@envelope_ok = session[:envelope_id].present?
28-
@documents_ok = session[:envelope_documents].present?
29-
@document_options = session.fetch(:envelope_documents, {})['documents']
30-
@gateway_ok = @config.gateway_account_id.try(:length) > 25
31-
@template_ok = session[:template_id].present?
32-
@documentation = "#{@config.documentation}#{eg_name}" #= Config.documentation + EgName
33-
@show_doc = @config.documentation
34-
else
35-
redirect_to '/ds/mustAuthenticate'
36-
end
37-
end
38-
39-
def set_meta
40-
@source_file = file_name.to_s
41-
#remove extra character that doesn't exist in service file
42-
index = @source_file.index('/')
43-
@source_file = index.nil? ? @source_file.sub(/^.*?eg/, 'eg') : @source_file.sub(%r{/.+?eg}, '/eg')
44-
@source_url = "#{Rails.application.config.github_example_url}#{@source_file}"
45-
end
46-
47-
def check_token(buffer_in_min = 10)
48-
buffer = buffer_in_min * 60
49-
expires_at = session[:ds_expires_at]
50-
remaining_duration = expires_at.nil? ? 0 : expires_at - buffer.seconds.from_now.to_i
51-
if expires_at.nil?
52-
Rails.logger.info '==> Token expiration is not available: fetching token'
53-
elsif remaining_duration.negative?
54-
Rails.logger.debug "==> Token is about to expire in #{time_in_words(remaining_duration)} at: #{Time.at(expires_at)}: fetching token"
55-
else
56-
Rails.logger.debug "==> Token is OK for #{time_in_words(remaining_duration)} at: #{Time.at(expires_at)}"
57-
end
58-
remaining_duration.positive?
59-
end
60-
61-
private
62-
63-
def time_in_words(duration)
64-
"#{Object.new.extend(ActionView::Helpers::DateHelper).distance_of_time_in_words(duration)}#{duration.negative? ? ' ago' : ''}"
65-
end
66-
67-
def param_gsub(parameter)
68-
parameter.gsub(/([^\w \-@.,])+/, '')
69-
end
70-
71-
def check_auth(api)
72-
# if not authorized for same API type example or
73-
# if it is an attempt to authorize from home page
74-
# then user will be redirected to login page
75-
unless (session[:api] == api) || ((api == 'eSignature') && !session[:api])
76-
session[:api] = api
77-
params[:auth] = 'jwt-auth' if api == 'Monitor'
78-
79-
return redirect_to '/ds/mustAuthenticate'
80-
end
81-
82-
minimum_buffer_min = 10
83-
token_ok = check_token(minimum_buffer_min)
84-
return if token_ok
85-
86-
flash[:messages] = 'Sorry, you need to re-authenticate.'
87-
# We could store the parameters of the requested operation so it could be restarted automatically
88-
# But since it should be rare to have a token issue here, we'll make the user re-enter the form data after authentication
89-
redirect_to '/ds/mustAuthenticate'
90-
end
91-
92-
def handle_error(e)
93-
error = JSON.parse e.response_body
94-
@error_code = e.code || error['errorCode']
95-
@error_message = error['error_description'] || error['message'] || error['error']
96-
render 'ds_common/error'
97-
end
98-
99-
def create_source_path
100-
# code here
101-
end
102-
103-
def ensure_manifest
104-
@manifest = Utils::ManifestUtils.new.get_manifest(Rails.configuration.example_manifest_url)
105-
end
106-
107-
def format_string(string, *args)
108-
string.gsub(/\{(\d+)\}/) { |s| args[s.to_i] }
109-
end
110-
end
1+
# frozen_string_literal: true
2+
3+
class EgController < ApplicationController
4+
skip_before_action :verify_authenticity_token
5+
before_action :eg_name, :set_eg, :set_meta, :ensure_manifest
6+
7+
def file_name
8+
"#{controller_path}_service.rb"
9+
end
10+
11+
def eg_name
12+
controller_name.to(4)
13+
end
14+
15+
def set_eg
16+
session[:eg] = controller_name.split('_', 2).first
17+
end
18+
19+
def get
20+
@messages = ''
21+
22+
# to have the user authenticate or re-authenticate.
23+
@token_ok = check_token
24+
@config = Rails.application.config
25+
if @token_ok || controller_name.include?('cneg')
26+
# addSpecialAttributes(model)
27+
@envelope_ok = session[:envelope_id].present?
28+
@documents_ok = session[:envelope_documents].present?
29+
@document_options = session.fetch(:envelope_documents, {})['documents']
30+
@gateway_ok = @config.gateway_account_id.try(:length) > 25
31+
@template_ok = session[:template_id].present?
32+
@documentation = "#{@config.documentation}#{eg_name}" #= Config.documentation + EgName
33+
@show_doc = @config.documentation
34+
else
35+
redirect_to '/ds/mustAuthenticate'
36+
end
37+
end
38+
39+
def set_meta
40+
@source_file = file_name.to_s
41+
#remove extra character that doesn't exist in service file
42+
index = @source_file.index('/')
43+
@source_file = index.nil? ? @source_file.sub(/^.*?eg/, 'eg') : @source_file.sub(%r{/.+?eg}, '/eg')
44+
@source_url = "#{Rails.application.config.github_example_url}#{@source_file}"
45+
end
46+
47+
def check_token(buffer_in_min = 10)
48+
buffer = buffer_in_min * 60
49+
expires_at = session[:ds_expires_at]
50+
remaining_duration = expires_at.nil? ? 0 : expires_at - buffer.seconds.from_now.to_i
51+
if expires_at.nil?
52+
Rails.logger.info '==> Token expiration is not available: fetching token'
53+
elsif remaining_duration.negative?
54+
Rails.logger.debug "==> Token is about to expire in #{time_in_words(remaining_duration)} at: #{Time.at(expires_at)}: fetching token"
55+
else
56+
Rails.logger.debug "==> Token is OK for #{time_in_words(remaining_duration)} at: #{Time.at(expires_at)}"
57+
end
58+
remaining_duration.positive?
59+
end
60+
61+
private
62+
63+
def time_in_words(duration)
64+
"#{Object.new.extend(ActionView::Helpers::DateHelper).distance_of_time_in_words(duration)}#{duration.negative? ? ' ago' : ''}"
65+
end
66+
67+
def param_gsub(parameter)
68+
parameter.gsub(/([^\w \-@.,])+/, '')
69+
end
70+
71+
def check_auth(api)
72+
# if not authorized for same API type example or
73+
# if it is an attempt to authorize from home page
74+
# then user will be redirected to login page
75+
unless (session[:api] == api) || ((api == 'eSignature') && !session[:api])
76+
session[:api] = api
77+
params[:auth] = 'jwt-auth' if api == 'Monitor'
78+
79+
return redirect_to '/ds/mustAuthenticate'
80+
end
81+
82+
minimum_buffer_min = 10
83+
token_ok = check_token(minimum_buffer_min)
84+
return if token_ok
85+
86+
flash[:messages] = 'Sorry, you need to re-authenticate.'
87+
# We could store the parameters of the requested operation so it could be restarted automatically
88+
# But since it should be rare to have a token issue here, we'll make the user re-enter the form data after authentication
89+
redirect_to '/ds/mustAuthenticate'
90+
end
91+
92+
def handle_error(e)
93+
error = JSON.parse e.response_body
94+
@error_code = e.code || error['errorCode']
95+
@error_message = error['error_description'] || error['message'] || error['error']
96+
render 'ds_common/error'
97+
end
98+
99+
def create_source_path
100+
# code here
101+
end
102+
103+
def ensure_manifest
104+
@manifest = Utils::ManifestUtils.new.get_manifest(Rails.configuration.example_manifest_url)
105+
end
106+
107+
def format_string(string, *args)
108+
string.gsub(/\{(\d+)\}/) { args[::Regexp.last_match(1).to_i] }
109+
end
110+
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# frozen_string_literal: true
2+
3+
class Webforms::Weg002CreateRemoteInstanceController < EgController
4+
before_action -> { check_auth('WebForms') }
5+
before_action -> { @example = Utils::ManifestUtils.new.get_example(@manifest, 2, 'WebForms') }
6+
7+
def create_web_form_template
8+
args = {
9+
template_name: 'Web Form Example Template',
10+
account_id: session[:ds_account_id],
11+
base_path: session[:ds_base_path],
12+
access_token: session[:ds_access_token]
13+
}
14+
15+
begin
16+
web_form_template_id = Webforms::Eg002CreateRemoteInstanceService.new(args).create_web_form_template
17+
Utils::FileUtils.new.replace_template_id(File.join('data', Rails.application.config.web_form_config_file), web_form_template_id)
18+
session[:web_form_template_id] = web_form_template_id
19+
20+
redirect_to '/weg002webForm'
21+
rescue DocuSign_eSign::ApiError => e
22+
handle_error(e)
23+
end
24+
end
25+
26+
def create_web_form_instance
27+
args = {
28+
form_name: 'Web Form Example Template',
29+
account_id: session[:ds_account_id],
30+
base_path: Rails.application.config.webforms_host,
31+
signer_name: Rails.application.config.signer_name,
32+
signer_email: Rails.application.config.signer_email,
33+
access_token: session[:ds_access_token]
34+
}
35+
create_remote_instance_service = Webforms::Eg002CreateRemoteInstanceService.new(args)
36+
web_forms = create_remote_instance_service.list_web_forms
37+
38+
if web_forms.items.nil? || web_forms.items.empty?
39+
@error_code = '404'
40+
@error_message = @example['CustomErrorTexts'][0]['ErrorMessage']
41+
return render 'ds_common/error'
42+
end
43+
results = create_remote_instance_service.create_web_form_instance web_forms.items.first.id
44+
45+
@title = @example['ExampleName']
46+
@message = format_string(@example['ResultsPageText'], results.envelopes[0].id, results.id)
47+
render 'ds_common/example_done'
48+
end
49+
50+
def get_web_form_create_view
51+
redirect_to '/weg002' if session[:web_form_template_id].nil?
52+
53+
additional_page = @example['AdditionalPage'].find { |p| p['Name'] == 'create_web_form' }
54+
@title = @example['ExampleName']
55+
@description = format_string(additional_page['ResultsPageText'], 'data')
56+
57+
render 'webforms/weg002_create_remote_instance/web_form_create'
58+
end
59+
end

app/helpers/application_helper.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
# frozen_string_literal: true
2-
3-
module ApplicationHelper
4-
require 'json/ext'
5-
6-
def format_string(string, *args)
7-
string.gsub(/\{(\d+)\}/) { |s| args[s.to_i] }
8-
end
9-
10-
def to_json(hash)
11-
hash.to_json
12-
end
13-
14-
def example_available?(example)
15-
!example['SkipForLanguages'] or !example['SkipForLanguages'].include? 'ruby'
16-
end
17-
end
1+
# frozen_string_literal: true
2+
3+
module ApplicationHelper
4+
require 'json/ext'
5+
6+
def format_string(string, *args)
7+
string.gsub(/\{(\d+)\}/) { args[::Regexp.last_match(1).to_i] }
8+
end
9+
10+
def to_json(hash)
11+
hash.to_json
12+
end
13+
14+
def example_available?(example)
15+
!example['SkipForLanguages'] or !example['SkipForLanguages'].include? 'ruby'
16+
end
17+
end

0 commit comments

Comments
 (0)