docusign_rest gem not populating tab from template - ruby-on-rails

I am trying to test the docusign_rest gem for possible integration into my rails app. I have set up the gem and configured it with my username, password, and integrator key. I have set the version of the api to 'v2' and checked all the of the names of both the signers and the tab fields multiple times. Here is the code I am using to make the request:
response = c.create_envelope_from_template(
status: 'sent',
email: {
subject: "Test email subject",
body: "Test email body",
},
template_id: template["templateId"],
signers: [
{
embedded: false,
name: "Name",
email: 'example#gmail.com',
role_name: "Signer1",
tabs: {
textTabs: [
{
tabLabel: "\\*address",
value: "123 Example St.",
locked: true
}
]
}
}
]
)
The request gets sent and the envelope gets sent but the 'address' field is not populated. Is there anything I am doing obviously wrong?
Thanks in advance.

Try changing the signers node in you request body to templateRoles. When you use templates in DocuSign instead of specifying the signers and their types and tabs, you assign them to existing template roles that are configured in the template. That's why no signer types should be specified in this call, just the template roles that each recipient will get matched to, and the values of any tabs you want to populate.
I've never used Ruby before so there might be some formatting or syntax issues I'm missing here, and I also have never seen the ruby gem you're referring to so not sure how stable or correct that code is. But basically this is what I think you need to do...
Right now you have:
status: 'sent',
email: {
subject: "Test email subject",
body: "Test email body",
},
template_id: template["templateId"],
signers: [
{
...
Try changing signers to templateRoles, like so:
status: 'sent',
email: {
subject: "Test email subject",
body: "Test email body",
},
template_id: template["templateId"],
templateRoles: [
{
...
The resulting http request body that you need to construct should look something like the following (in JSON format):
{
"emailSubject": "DocuSign API - Signature Request from Template",
"templateId": "ABCD1234",
"templateRoles": [
{
"email": "firstperson#email.com",
"name": "John Doe",
"roleName": "Template Role Name goes here",
"tabs": [
{
"textTabs": [
{
"tabLabel": "\\*address",
"value": "432 Sorrento Dr.",
"locked": "true"
}
]
}
]
}
],
"status": "sent"
}
Lastly, the DocuSign API Walkthrough for requesting a signature from a template is a great resource for sending from a template so this might help too. There's sample code for making this api call in 6 different languages (unfortunately Ruby is not one of them):
http://iodocs.docusign.com/APIWalkthrough/requestSignatureFromTemplate

Finally figured it out myself by modifying the gem to inspect the request. With the docusign_rest gem, the documentation on the github page is incorrect. The tabs dictionary should not be there and the items in this specific request should be underscore separated instead of camel case. Therefore, the actual request would look something like:
response = c.create_envelope_from_template(
status: 'sent',
email: {
subject: 'Test email subject',
body: 'Test email body',
},
template_id: template["templateId"],
signers: [
{
embedded: false,
name: 'Name',
email: 'example#gmail.com',
role_name: 'Signer1',
text_tabs: [
{
label: 'Address',
name: 'Address',
value: '123 Example Street.',
}
]
}
]
)
This request will allow you with the gem and v2 of the api to create an envelope from a template and prepopulate any fields. All other field types go the same way as the text_tabs array.

Related

Creating multiple resources in tandem with JSONAPI:Resources for Rails

On my application, I would like to add a post and user in tandem using jsonapi:resources. A user can be created already. And a post can be created already if someone is signed in. However for this special scenario I would like a user to be able to submit their first post while creating their account. Here is the payload that would be submitted.
{
"data":{
"user":{
"attributes":{
"name": "blah blah",
"age": 20,
"address": "mickey mouse street"
}
},
"relationships": {
"post": {
"content": "this is a new post",
"type": "blog"
}
}
}
}
Is this at all possible? If so in which resource file would I create them both? Would it be the resource file for posts or for users? Or would it be best practice to create the user first and then once the user has been created make a separate call to create the post? Thanks!

Ruby on Rails and SendGrid; dynamic template data is not populating

I'm fairly new to Ruby on Rails and SendGrid.
I cannot figure out why the code sample below does not work? It does send an email using the correct template, but the dynamic data does not fill in. Any input/insight is appreciated.
require 'sendgrid-ruby'
include SendGrid
mail = SendGrid::Mail.new
mail.template_id = 'template id'
mail.from = Email.new(
email: 'someone#somewhere.com',
name: 'ABC')
personalization = SendGrid::Personalization.new
personalization.add_to(Email.new(email: 'someone#somewhere.com', name: 'ABC'))
personalization.add_dynamic_template_data(
'variable' => [
{ 'first_name' => 'John' },
{ 'last_name' => 'Doe' },
{ 'email' => 'john#doe.com' },
{ 'message' => 'The message is as follows' }
]
)
mail.add_personalization(personalization)
sgp = SendGrid::API.new(api_key: ENV['sendgrid_api_key'])
response = sgp.client.mail._('send').post(request_body: mail.to_json)
The template code looks like the following.
Hello from {{first_name}} {{last_name}}.
{{message}}
And the result ends up without the variable values filled in.
Hello from .
Twilio SendGrid developer evangelist here.
The dynamic data that you are adding is more complicated than the template. Instead of sending an array of hashes as the dynamic data, you can just send a hash. Try this instead:
personalization.add_dynamic_template_data({
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'john#doe.com',
'message' => 'The message is as follows'
})
See this example in the SendGrid Ruby library docs too.

How to add additional attributes to the JSON response in my Rails API

I am using Rails 5.2 for my application.
Request:
http://localhost:3000/reports
Response:
[
{
id: 1,
name: "Ram",
details: {
path: "dev/daily_summary_20190503.csv",
success_detail: "Report uploaded to S3"
},
status: "success"
},
{
id: 2,
name: "John",
details: {
path: "dev/daily_summary_20190504.csv",
error_detail: "Error in uploading report. Refer log for details"
},
status: "failed"
}
]
I want to add download_url, message parameters to every records where parameters is not added to schema. Following is my expected output,
Expected output:
[
{
id: 1,
name: "Ram",
details: {
path: "dev/daily_summary_20190503.csv",
success_detail: "Report uploaded to S3"
},
status: "success",
download_url: "https://<S3_HOST>/dev/daily_summary_20190503.csv",
message: "Report uploaded to S3"
},
{
id: 2,
name: "John",
details: {
error_detail: "Error in uploading report. Refer log for details"
},
status: "failed",
message: "Error in uploading report. Refer log for details"
}
]
I have tried using attr_accessor, but which doesn't help me to display download_url in all records of index method in controller.
How can I add the paramters for index and show of every records?
If you want to change the JSON that is in the HTTP response you need to find the place where the JSON is generated. Follow the trail from the request URL:
use rails routes on the command line
in the outbut, find your url /reports and read which controller + action it is mapped to
open the controller file (probably /app/controllers/reports_controller.rb) and find the action (probably index)
look at the end of the action
If you find a render statement like this:
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #reports }
end
you need to follow the block after format.json. In this example you see
that the data that is displayed is stored in the #reports variable.
study the action to find out how the data is created. The data is
then rendered using the template /app/views/reports/index.json.jbuilder.
Read up on jbuilder.
In a more complex application instead of jbuilder ActiveModelSerializer might be used.

Docusign in Rails

I'm trying to implement Document signing on my rails app.
I have a template which is sent to many users.
So i have made a custom tag with label Member and i want to fill it with a custom text say 'abc' through my app.
Here is my code which i tried so far. The member part still looks blank. Other fields like name, signature and date are working properly.
client = DocusignRest::Client.new
#envelope_response = client.create_envelope_from_template(
status: 'sent',
email: {
subject: "The test email subject envelope 7",
body: "Envelope body content here"
},
template_id: template_id,
signers: [
{
name: 'Name',
email: 'sample_email#gmail.com',
role_name: 'Expert',
tabs: {
text_tabs: [
{
tab_label: 'Member',
value: 'abc'
}
]
}
}
]
)
I dont know where i went wrong. Please help
it seems the 'text_tabs' should not be embedded inside a 'tabs'. instead, pass the 'text_tabs' directly within a signer. also, instead of 'tab_label', try just using 'label'.

Mailchimp API for Rails - list.subscribe(:double_optin => false) not working

I am using the mailchimp-api gem and can get the submitted email to work by submitting an email and having a confirmation email sent to that email that signed up but I want to disable the double_optin flag. I am trying to do it with:
#mc = Mailchimp::API.new('my api key here')
#mc.list.subscribe('list_id', {'email' => params[:email]}, :double_optin => false)
This is still sending a confirmation email to that email address.
I really don't like how it redirects to a mailchimp page to have you confirm your subscription and have to click another button to be redirected to the site. If you could customize the confirmation email that would be one thing but having this generic confirmation page is terrible.
I am wondering if you have to have a paid account to be able to toggle the :double_optin flag?
This is what I used from mailchimp-api
subscribe(id, email, merge_vars = nil, email_type = 'html', double_optin = true, update_existing = false, replace_interests = true, send_welcome = false) ⇒ Hash
The double_optin is the parameter which will be passed
So, I used:
client.lists.subscribe(#list_id, email, nil, 'html', false)
This just expands on the answer from #Jordan above to explain why that works and why the accepted answer is incorrect.
If you are using the mailchimp-api gem, you can see in the documentation that the format for a single subscribe is subscribe(id, email, merge_vars = nil, email_type = 'html', double_optin = true, update_existing = false, replace_interests = true, send_welcome = false). After a lot of guessing and testing, I figured out that they don't want you to pass the parameter names, just the values. So, while I was originally passing double_optin: false in the function, they really just want you to pass false as the 5th parameter. In other words, this is what worked for me:
mailchimp = Mailchimp::API.new('mailchimp_api_key')
subscribe = mailchimp.lists.subscribe(
'list_ID_number',
{ email: user.email },
{ fname: user.first_name, lname: user.last_name },
'html',
false,
false,
false,
false
)
Note that the second and third parameters (email and merge_vars) were passed as hashes because that's how those parameters are defined in the documentation referenced above.
After making that change (removing the parameter names), subscriptions went right through without the confirmation emails.
Ended up getting this to work with the following:
#mc = Mailchimp::API.new('my api key here')
#mc.list.subscribe({:id => 'list_id', :email => {:email => params[:email]}, :double_optin => false})
Note the difference here is that every parameter in #mc.list.subscribe is in a hash.
The accepted answer using subscribe method does not work and will still send the confirmation email, but it is easy to do this using the batch_subscribe method instead.
First, create an array containing a hash for each subscriber you want to add (or just one), like this:
subscribers = [{ "EMAIL" => { "email" => user.email},
:EMAIL_TYPE => 'html',
:merge_vars => { "NAME" => user.name
}
}]
Then, use batch_subscribe method, like this:
mailchimp = Mailchimp::API.new(MAILCHIMP_API_KEY)
mailchimp.lists.batch_subscribe(MAILCHIMP_LIST_ID, subscribers, false, false, false)
The three "falses" at the end refer to, in order: double_optin, update_existing, and replace_interests. You can set those to true or false, but double_optin needs to be "false" if you want to skip sending the confirmation email.
Here is the documentation for the batch_subscribe method.
After going through mailchimp-api gem I found this solution
#mc.list.subscribe('list_id', { email: params[:email] }, nil, double_optin = false)

Resources