Mandrill template merge fail - ruby

I'm using mandrill to send emails from my API (in ruby with the mandrill-api gem), like signup confirmation email.
I have a merge tag in the template to put the username :
Hello *|USERNAME|*,
Thank you very much for installing the app! ...
...
My ruby code looks like that :
m = Mandrill::API.new ENV['MANDRILL_KEY']
template_name = "app-registration-welcome-email"
template_content = [{}]
message = {
:from_name=> "From Name",
:to=>[
{
:email => user.email,
:name => user.name,
}
],
:global_merge_vars => [{
:name => "username",
:content => user.name
}],
:merge_language => "mailchimp",
:merge => true,
:merge_vars => [{
:rcpt => user.email,
:vars => [{
:name => "username",
:content => user.name
}],
}],
:track_opens => true,
}
m.messages.send_template template_name, template_content, message
Unfortunately, when i receive the email, all is fine (to, name, from, ...) but the merge tag isn't replaced in the body and i still have *|USERNAME|* displayed.
What am I missing here?

You should only need to set USERNAME once, either in :global_merge_vars or :merge_vars.
If you're sending to just one recipient, it doesn't matter which you do.
If there are multiple recipients, use :merge_vars.
Try building message and then puts message.to_json and drop it into the debugger at https://mandrillapp.com/api/docs/messages.JSON.html#method=send-template (Click the 'Try it' button.) See if that gives you any clues.
You might also try using "USERNAME" instead of "username" as your variable name. The docs say merge vars are case-insensitive, but it's worth removing one more possible mismatch.

Related

Creating a Gmail Draft with Recipients through Gmail API

I have been trying to figure out how to automatically add recipients to a Draft email that is created using the Gmail API through their Ruby library. I can create the draft without any issues but setting the recipients is causing me troubles and I haven't been able to find any good examples showing the best way to add email specific things.
Using the Google API playground and pulling in drafts that have already been created, it looks like the structure should be something similar to what is shown below, but whenever the draft is created, there are no recipients.
#result = client.execute(
:api_method => gmail.users.drafts.create,
:parameters => {
'userId' => "me"
},
:body_object => {
'message' => {
'raw' => Base64.urlsafe_encode64('Test Email Message'),
'payload' => {
'headers' =>
[
{
'name' => "To",
'value' => "John Smith <john_smith.fake#gmail.com>"
}
]
}
}
}
)
'raw' should contain the entire (RFC822) email, complete with body and headers. Do not use the 'payload.headers' structure, that parsed format is only used for returning during message.get() presently.
so for 'raw' you'd want to Base64.urlsafe_encode64() a string like:
"To: someguy#example.com\r\nFrom: myself#example.com\r\nSubject: my subject\r\n\r\nBody goes here"

What else should I send for Paypal chained payments?

I'm working on an App where a user A can buy up to 10 items from different sellers, so I need to send money to different users at the same time and I'm trying to use Paypal Chained Payments.
Right now I'm just playing around with Classic API (Adaptive payments) but I'm just wondering why I'm always getting this error:
"The fee payer PRIMARYRECEIVER can only be used if a primary receiver is specified"
I already specified a primary receiver and I'm still getting that error.
I found these examples: https://paypal-sdk-samples.herokuapp.com/adaptive_payments/pay
and I tried to do a chained payment:
This is my pay request:
require 'paypal-sdk-adaptivepayments'
#api = PayPal::SDK::AdaptivePayments::API.new
# Build request object
#pay = #api.build_pay({
:actionType => "PAY",
:cancelUrl => "https://paypal-sdk-samples.herokuapp.com/adaptive_payments/pay",
:currencyCode => "USD",
:feesPayer => "PRIMARYRECEIVER",
:ipnNotificationUrl => "https://paypal-sdk-samples.herokuapp.com/adaptive_payments/ipn_notify",
:receiverList => {
:receiver => [{
:amount => 1.0,
:email => "platfo_1255612361_per#gmail.com",
:primary => true }] },
:returnUrl => "https://paypal-sdk-samples.herokuapp.com/adaptive_payments/pay",
:sender => {
:useCredentials => true } })
# Make API call & get response
#pay_response = #api.pay(#pay)
And this is the response
{
:responseEnvelope => {
:timestamp => "2013-11-20T05:16:31-08:00",
:ack => "Failure",
:correlationId => "b002d0e27fd33",
:build => "7935900" },
:error => [{
:errorId => 580023,
:domain => "PLATFORM",
:subdomain => "Application",
:severity => "Error",
:category => "Application",
:message => "The fee payer PRIMARYRECEIVER can only be used if a primary receiver is specified",
:parameter => [{
:value => "feesPayer" },{
:value => "PRIMARYRECEIVER" }] }] }
Thanks in advance!
taking a look at your call, you're indeed specifying a primary receiver. however, for chained payment, you will have to specify a secondary receiver.
I just ran a quick test with the following paramters:
actionType = PAY
requestEnvelope.errorLanguage = en_US
cancelUrl = http://abortURL
returnUrl = http://returnURL
ipnNotificationUrl = http://ipnURL
applicationId = Test
memo = Test
currencyCode = USD
receiverList.receiver(0).email = test#test.com
receiverList.receiver(0).amount = 5.00
receiverList.receiver(0).primary = true
feesPayer = PRIMARYRECEIVER
and got the result:
responseEnvelope.timestamp=2013-11-20T05:41:56.751-08:00
responseEnvelope.ack=Failure
responseEnvelope.correlationId=b61a6b31ea2ab
responseEnvelope.build=7935900
error(0).errorId=580023
error(0).domain=PLATFORM
error(0).subdomain=Application
error(0).severity=Error
error(0).category=Application
error(0).message=The fee payer PRIMARYRECEIVER can only be used if a primary receiver is specified
error(0).parameter(0)=feesPayer
error(0).parameter(1)=PRIMARYRECEIVER
However, once I change the FeesPayer to EACHRECEIVER, I get the error message that is causing the chained payment to fail in the first place:
responseEnvelope.timestamp=2013-11-20T05:48:09.202-08:00
responseEnvelope.ack=Failure
responseEnvelope.correlationId=987210ec4d03a
responseEnvelope.build=7935900
error(0).errorId=579008
error(0).domain=PLATFORM
error(0).subdomain=Application
error(0).severity=Error
error(0).category=Application
error(0).message=You must specify only one primary receiver and at least one secondary receiver
error(0).parameter(0)=1
I hope this helps.
Please refer to the PayPal Adaptive Payments SDK available under http://paypal.github.io/#adaptive-payments for some additional examples and inspiration

How to stop DataMapper from double query when limiting columns/fields?

I'm not sure if I'm at fault here or if my approach is wrong with this.
I want to fetch a user (limiting columns/fields only to name, email, id):
#user = User.first(:api_key => request.env["HTTP_API_KEY"], :fields => [:id, :name, :email])
The output in the command line is correct as follows:
SELECT "id", "name", "email" FROM "users" WHERE "api_key" = '90e20c4838ba3e1772ace705c2f51d4146656cc5' ORDER BY "id" LIMIT 1
Directly after the above query, I have this code:
render_json({
:success => true,
:code => 200,
:user => #user
})
render_json() looks like this, nothing special:
def render_json(p)
status p[:code] if p.has_key?(:code)
p.to_json
end
The problem at this point is that the #user variable contains the full user object (all other fields included) and DataMapper has made an additional query to the database to fetch the fields not included in the :fields constraint, from the logs:
SELECT "id", "password", "api_key", "premium", "timezone", "verified", "notify_me", "company", "updated_at" FROM "users" WHERE "id" = 1 ORDER BY "id"
My question is this: how do I stop DM from performing the additional query? I know it has to do with it's lazy loading architecture and that returning the #user variable in JSON assumes that I want the whole user object. I particularly don't want the password field to be visible in any output representation of the user object.
The same behaviour can be seen when using DM's own serialisation module.
I think you should use an intermediate object for json rendering.
First, query the user from database :
db_user = User.first(:api_key => request.env["HTTP_API_KEY"], :fields => [:id, :name, :email])
Then, create a "json object" to manipulate this user :
#user = { id: db_user.id, name: db_user.name, email: db_user.email }

How do you add a Google Calendar to all users for a domain?

I am having trouble figuring out how to add a calendar to all of a domain's users' CalendarList.
So far I can successfully create the Calendar and create a domain wide ACL but I don't know how to insert the calendar into domain users' lists.
Using the ruby client API it looks something like this:
client = Google::APIClient.new
service = client.discovered_api("calendar", "v3")
calendar_response = JSON.parse(client.execute(:api_method => service.calendars.insert,
:body => JSON.dump({"summary" => "events"}),
:headers => { "Content-Type" => "application/json" }).response.body)
rule = {
"scope" => {
"type" => "domain",
"value" => domain,
},
"role" => "writer"
}
acl_result = client.execute(:api_method => service.acl.insert,
:parameters => { "calendarId" => calendar_response["id"] },
:body => JSON.dump(rule),
:headers => { "Content-Type" => "application/json" })
This all works fine. It creates the calendar and it is shared with everyone in the domain. What I can't figure out is how to explicitly added it to the users' list of Calendars.
To add to the single authed user's calendar list would look like this:
list_params = {
"calendarId" => calendar_response["id"],
"hidden" => false,
"selected" => true
}
calendar_list_result = client.execute(:api_method => service.calendar_list.insert,
:body => JSON.dump(list_params),
:headers => { "Content-Type" => "application/json" })
Questions:
If I am authenticated as a domain admin, can I create CalendarList item for all users?
If so, can it be done with a single command or do I need to make a call with every user id?
If I need to do a command for each user, how do I get the user ids?
If I am authenticated as a domain admin, can I create CalendarList item for all users?
No you can't :(.
If so, can it be done with a single command or do I need to make a call with every user id?
You have to make a call for each user id.
If I need to do a command for each user, how do I get the user ids?
To retrieve the user id you first must list all the users in the domain with the Admin SDK API, https://developers.google.com/admin-sdk/directory/v1/guides/manage-users#get_all_domain_users , then for each INSERT command in the calendar you change the user id, for the email address.

How to add a Person using the Highrise Ruby gem?

I am trying to use https://github.com/tapajos/highrise/ to update user accounts when people sign up to an app. However I am not getting very far.
In console I am doing:
person = Highrise::Person.create(:name => "charlie")
Which saves fine, but if I do something like
person = Highrise::Person.create(:name => "charlie", :email => "charlie#222.com")
then I get:
Unprocessable Entity
I can not get my head around this, I essentially want to add a full record:
person = Highrise::Person.create(:name => "charlie", :params => {:contact_data => {:email_addresses => "charlie#222.com"}})
but still i get the same error and can not find any examples online
You were on the right track with that last attempt. Give this a try:
person = Highrise::Person.create(
:first_name => "Charlie", :last_name => "Bravo",
:contact_data => {
:email_addresses => [{
:email_address => {:address => "charlie#222.com"}
}]
}
)
This matches the structure of the create a person request, as defined in the Highrise API. https://github.com/37signals/highrise-api/blob/master/sections/people.md#create-person
Also you can refer to ruby api's test spec for more examples https://github.com/tapajos/highrise/blob/f44cb3212c6d49549330c46454fe440ac117fa1b/spec/highrise/person_spec.rb#L40

Resources