Mandrill handlebars example - javascript

The variables in my html content appear to be stripped so no dynamic content is showing up in the email. The template language is set to handlebars in the code and globally.
The documentation does not appear to give any other details or examples. There are no error messages.
var subject = "A really great subject";
var htmlContent = "<h1>Hi, {{user}}</h1><p>{{productName}}?</p><br/><a href='{{unsub redirect_merge_var}}'>Unsubscribe</a>"
var mailJSON ={
"key": "myKey",
"merge_language": "handlebars",
"merge": true,
"global_merge_vars": [
{
"name": "productName",
"content": "Mandrill_User1"
},
{
"name": "user",
"content": "cool guy"
}
],
"message": {
"html": htmlContent,
"subject": subject,
"from_email": "stuff#xxxxxxxxxx.com",
"from_name": "stuff-app",
"to": [
{
"email": ""+person.email,
"name": ""+person.name,
"type": "to"
}
],
"important": false,
"track_opens": null,
"track_clicks": null,
"auto_text": null,
"auto_html": null,
"inline_css": null,
"url_strip_qs": null,
"preserve_recipients": null,
"view_content_link": null,
"tracking_domain": null,
"signing_domain": null,
"return_path_domain": null
},
"async": false,
"ip_pool": "Main Pool"
}

move this:
"merge_language": "handlebars",
"merge": true,
"global_merge_vars": […],
into your message struct. (Official API documentation)
You can also remove your NULL values if you don't need them.

Related

Need some help translating a get value in a JSON file

Our school receives data from a source during a sync.
I'm familiar with JavaScript but would like to ask for a litle help before I make a change.
Here is the scenario: The source sending the information to us has the default value as "tobedeleted". We need this to be translated to "inactivate". and then put into our DB.
What's being sent I think is simply ignored because it doesn't match any of our enum values.
My idea is to get help writing: if the get value = "tobedeleted" then translate it to "inactivate" and then update our database.
{
"path": "/v1/courses/{course_id}/enrollments/{id}",
"description": "Conclude, deactivate, or delete an enrollment. If the +task+ argument isn't given, the enrollment\nwill be concluded.",
"operations": [
{
"method": "DELETE",
"summary": "Conclude, deactivate, or delete an enrollment",
"notes": "Conclude, deactivate, or delete an enrollment. If the +task+ argument isn't given, the enrollment\nwill be concluded.",
"nickname": "conclude_deactivate_or_delete_enrollment",
"parameters": [
{
"paramType": "path",
"name": "course_id",
"description": "ID",
"type": "string",
"format": null,
"required": true,
"deprecated": false
},
{
"paramType": "path",
"name": "id",
"description": "ID",
"type": "string",
"format": null,
"required": true,
"deprecated": false
},
{
"paramType": "query",
"name": "task",
"description": "The action to take on the enrollment.\nWhen inactive, a user will still appear in the course roster to admins, but be unable to participate.\n(\"inactivate\" and \"deactivate\" are equivalent tasks)",
"type": "string",
"format": null,
"required": false,
"deprecated": false,
"enum": [
"conclude",
"delete",
"inactivate",
"deactivate"
]
}
],
"response_fields": [
],
"deprecated": false,
"deprecation_description": "",
"type": "Enrollment"
}
]
},
Thank you in advance!
Lets assign the JSON to a variable named data. Then you can do
data.operations.map((operation) => {
if (operation.method === 'DELETE') {
operation.parameters.map((param, queryIndex) => {
if (param.paramType === 'query') {
param.enum.map((item, enumIndex) => {
if (item === 'tobedeleted') {
operation.parameters[queryIndex].enum[enumIndex] = 'inactivate';
//item = 'inactivate';
}
});
}
});
}
return operation;
});
Note: This may not be an optimized code but it does the work.

trouble while parsing json

I am trying to react displayName in req
when I write
console.log(req.session.passport.user._raw)
the output is:
{
"kind": "plus#person",
"etag": "\"ucaTEV-ZanNH5M3SCxYRM0QRw2Y/XiR7kPThRbzcIw-YLiARoF22TMY\"",
"emails": [
{
"value": "rajanlagah#gmail.com",
"type": "account"
}
],
"objectType": "person",
"id": "100428644453494579140",
"displayName": "Rajan Lagah",
"name": {
"familyName": "Lagah",
"givenName": "Rajan"
},
"url": "https://plus.google.com/100428644453494579140",
"image": {
"url": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50",
"isDefault": true
},
"isPlusUser": true,
"language": "en",
"circledByCount": 0,
"verified": false
}
Now the from this object I am trying to get kind (say)
console.log(req.session.passport.user._raw.kind)
then the output is undefined
Can any one tell my mistake?
Is req.session.passport.user._raw a string? You may need to parse it first, eg:
var data = JSON.parse(req.session.passport.user._raw);
console.log(data.kind);

How to insert values of matching JSON Keys between 2 JSON files

I have 2 JSON files:
A Template JSON
A JSON output (from awscli)
The template is a small JSON file as below:
{
"DryRun": true,
"ImageId": "",
"KeyName": "",
"SecurityGroups": [
""
],
"InstanceType": "",
"Monitoring": {
"Enabled": false
},
"SubnetId": "",
"DisableApiTermination": true,
"PrivateIpAddress": "",
"IamInstanceProfile": {
"Arn": "",
"Name": ""
},
"EbsOptimized": true,
"TagSpecifications": [{
"ResourceType": "",
"Tags": [{
"Key": "",
"Value": ""
}]
}]
}
The original file is the output of aws ec2 describe-instances:
{
"Reservations": [{
"OwnerId": "123456789012",
"ReservationId": "r-12345678",
"Groups": [],
"Instances": [{
"Monitoring": {
"State": "disabled"
},
"PublicDnsName": "ec2-12-34-56-78.ap-southeast-1.compute.amazonaws.com",
"RootDeviceType": "ebs",
"State": {
"Code": 16,
"Name": "running"
},
"EbsOptimized": false,
"LaunchTime": "2016-02-09T03:06:21.000Z",
"PublicIpAddress": "12.34.56.78",
"PrivateIpAddress": "172.31.1.2",
"ProductCodes": [],
"VpcId": "vpc-1a2b3c4d",
"StateTransitionReason": "",
"InstanceId": "i-abcd1234",
"ImageId": "ami-1234abcd",
"PrivateDnsName": "ip-172-31-1-2.ap-southeast-1.compute.internal",
"KeyName": "tempKey",
"SecurityGroups": [{
"GroupName": "somegroup1",
"GroupId": "sg-ZZZZZ"
},
{
"GroupName": "somegroup2",
"GroupId": "sg-YYYYY"
}
],
"ClientToken": "NutKc123456789012",
"SubnetId": "subnet-00001234",
"InstanceType": "t2.medium",
"NetworkInterfaces": [{
"Status": "in-use",
"MacAddress": "02:AA:BB:CC:DD:EE",
"SourceDestCheck": true,
"VpcId": "vpc-1a2b3c4d",
"Description": "",
"Association": {
"PublicIp": "12.34.56.78",
"PublicDnsName": "ec2-12-34-56-78.ap-southeast-1.compute.amazonaws.com",
"IpOwnerId": "123456789012"
},
"NetworkInterfaceId": "eni-XXXXXXXX",
"PrivateIpAddresses": [{
"PrivateDnsName": "ip-172-31-1-2.ap-southeast-1.compute.internal",
"Association": {
"PublicIp": "1.2.3.4",
"PublicDnsName": "ec2-12-34-56-78.ap-southeast-1.compute.amazonaws.com",
"IpOwnerId": "123456789012"
},
"Primary": true,
"PrivateIpAddress": "172.31.1.2"
}],
"PrivateDnsName": "ip-172-31-1-2.ap-southeast-1.compute.internal",
"Attachment": {
"Status": "attached",
"DeviceIndex": 0,
"DeleteOnTermination": true,
"AttachmentId": "eni-attach-XXXXXXXX",
"AttachTime": "2016-01-13T08:33:37.000Z"
},
"Groups": [{
"GroupName": "somegroup1",
"GroupId": "sg-ZZZZZZ"
},
{
"GroupName": "somegroup2",
"GroupId": "sg-YYYYYY"
}
],
"Ipv6Addresses": [],
"SubnetId": "subnet-00001234",
"OwnerId": "123456789012",
"PrivateIpAddress": "172.31.1.2"
}],
"SourceDestCheck": true,
"Placement": {
"Tenancy": "default",
"GroupName": "",
"AvailabilityZone": "ap-southeast-1b"
},
"Hypervisor": "xen",
"BlockDeviceMappings": [{
"DeviceName": "/dev/xvda",
"Ebs": {
"Status": "attached",
"DeleteOnTermination": true,
"VolumeId": "vol-33221100",
"AttachTime": "2016-01-13T08:33:39.000Z"
}
}],
"Architecture": "x86_64",
"StateReason": {
"Message": "Client.UserInitiatedShutdown: User initiated shutdown",
"Code": "Client.UserInitiatedShutdown"
},
"RootDeviceName": "/dev/xvda",
"VirtualizationType": "hvm",
"Tags": [{
"Value": "SomeValue",
"Key": "SomeKey"
},
{
"Value": "AnotherValue",
"Key": "Name"
}
],
"AmiLaunchIndex": 0
}]
}]
}
I want to copy the values of the Keys in the original JSON file to the template file.
For example, KeyName is a common key between the 2 JSON files. The corresponding value tempKey is replaced in the template file.
The main use case of this is: I am trying to migrate a number of servers on AWS from 1 region to another. This is a part of migration process which will remove tons of manual clicking and configuration on AWS Console.
Note: I use BASH command line.
There's a way to do this with jq but it doesn't take a JSON template as input.
You'll have to modify it to become a query. This isn't the actual query you need, but something to get you started:
cat temp.json | jq '.Reservations[].Instances[] | { DryRun, ImageId, KeyName, SecurityGroups, InstanceType, Monitoring }'
Where temp.json is your output above that I placed into a file. For regular commands, just do something like aws ec2 describe-instances | jq ...
The output that gives me (keep in mind the restricted set I queried for) is:
{
"DryRun": null,
"ImageId": "ami-1234abcd",
"KeyName": "tempKey",
"SecurityGroups": [
{
"GroupName": "somegroup1",
"GroupId": "sg-ZZZZZ"
},
{
"GroupName": "somegroup2",
"GroupId": "sg-YYYYY"
}
],
"InstanceType": "t2.medium",
"Monitoring": {
"State": "disabled"
}
}
Hope this helps.

Can't get value of Object in Javascript , nodejs, stripe library

Im trying to get a value "status" from "customer" object with this simple script:
console.log(JSON.stringify(customer.subscriptions.data.plan.status));
When i execute this function, the console returns me:
TypeError: Cannot read property 'data' of undefined
"customer" Object:
customer: {
"object":"customer",
"created":xxxxxx,
"id":"xxxxxxx",
"livemode":false,
"description":null,
"email":"xxxx#xxxx.com",
"shipping":null,
"delinquent":false,
"metadata":{},
"subscriptions":{
"object":"list",
"total_count":1,
"has_more":false,
"url":"/v1/customers/xxxxxxxxx/subscriptions",
"data":[{
"id":"xxxxxxxxx",
"plan":{
"interval":"month",
"name":"xxxxxx",
"created":xxxxx,
"amount":xxxxx,
"currency":"eur",
"id":"6month",
"object":"plan",
"livemode":false,
"interval_count":6,
"trial_period_days":null,
"metadata":{},
"statement_descriptor":null,
"statement_description":null},
"object":"subscription",
"start":xxxxx,
"status":"active",
...,
Please help me.
Thanks.
The error doesn't match up with the data. It should be that it can't read status of undefined. This is because customer does have subscriptions, and subscriptions does have data, but then you're treating data as though it had a plan property, which it doesn't. data refers to an array, the first entry of which has a plan property. Also note that status is not a property of plan, it's a property of the same object that plan is a property of.
So accessing the first entry's status would be:
customer.subscriptions.data[0].status
// Note -------------------^^^
If there are subsequent entries in data, they would be at indexes 1, 2, 3, etc.
Example:
var customer = {
"object": "customer",
"created": "xxxxxx",
"id": "xxxxxxx",
"livemode": false,
"description": null,
"email": "xxxx#xxxx.com",
"shipping": null,
"delinquent": false,
"metadata": {},
"subscriptions": {
"object": "list",
"total_count": 1,
"has_more": false,
"url": "/v1/customers/xxxxxxxxx/subscriptions",
"data": [
{
"id": "xxxxxxxxx",
"plan": {
"interval": "month",
"name": "xxxxxx",
"created": "xxxxx",
"amount": "xxxxx",
"currency": "eur",
"id": "6month",
"object": "plan",
"livemode": false,
"interval_count": 6,
"trial_period_days": null,
"metadata": {},
"statement_descriptor": null,
"statement_description": null
},
"object": "subscription",
"start": "xxxxx",
"status": "active"
}
]
}
};
document.body.innerHTML = customer.subscriptions.data[0].status;

Display data from common topic in Freebase using jQuery

I am playing around with Freebase and have had some decent success, but have hit a wall. My MQL is below. I do not have any issue displaying name,latin name, etc, which I created in my base. I do not know how to display the article which is in a different base.
Here is the jQuery I am using to display data:
$('<div>',{text:this.name}).appendTo(document.body);
Thank you very much,
Todd
query : [
{
"/common/topic/article": {
"guid": null,
"limit": 1,
"optional": true
},
"/common/topic/image": {
"id": null,
"limit": 1,
"optional": true
},
"id": null,
"larval_food": [
{
"index": null,
"lang": "/lang/en",
"limit": 6,
"optional": true,
"sort": "index",
"type": "/type/text",
"value": null
}
],
"latin_name": [
{
"index": null,
"lang": "/lang/en",
"limit": 6,
"optional": true,
"sort": "index",
"type": "/type/text",
"value": null
}
],
"limit": 60,
"name": null,
"s0:type": [
{
"id": "/base/butterflies/butterfly",
"link": [
{
"timestamp": [
{
"optional": true,
"type": "/type/datetime",
"value": null
}
],
"type": "/type/link"
}
],
"type": "/type/type"
}
],
"sort": "-s0:type.link.timestamp.value",
"type": "/base/butterflies/butterfly"
}
]
Change
"type": "/base/butterflies/butterfly"
to the type of the thing you actually want to include.
As an aside, that looks like a query which was exported from one of the Freebase.com view pages. It can be greatly simplified and some of the stuff, like the sorting, you probably want removed altogether.
Here's your query simplied (I also recommend using the standard scientific name property instead of inventing your own "Latin name" property):
[{
"type": "/base/butterflies/butterfly",
"mid": null,
"name": null,
"/common/topic/article": [],
"/common/topic/image": ["mid":null,"optional":true],
"larval_food": [],
"latin_name": [],
"/biology/organism_classification/scientific_name" : [],
}]
Here's a version of the query which shows all organism classifications (species in this case) which have the tribe Danaini two levels up. It optionally decorates it with data (larval_food) from your base, if it exists:
[{
"type": "/biology/organism_classification",
"higher_classification": [{
"/biology/organism_classification/higher_classification": "Danaini"
}],
"mid": null,
"name": null,
"scientific_name": [],
"/common/topic/article": [],
"/common/topic/image": [{
"mid": null,
"optional": true
}],
"/base/butterflies/butterfly/larval_food": [],
}]
You can try it here: http://tinyurl.com/6wht7lx

Categories

Resources