sinatra + mongoid - sinatra

I'm creating an app that use sinatra + mongoid. I have two models, contact has many phones. To test my sinatra controller I post my data with this command `
curl -X POST -d "contact[name]=nome&contact[email]=email#dominio.com&contact[phone][0][number]=88888888&contact[phone][0][type]=1&contact[phone][2][number]=77777777&contact[phone][3][type]=1"
but when I did one query in mongodb I see that not save as expected. I need that phone class will be save with array, but now phone is a hash where the key is "0", "1", N like my post data. How do I to resolve this problem? I want data to be saved so:
{
"_id":"4f889875b336e722a0000003",
"email":"diego.dias2#dominio.com.br",
"github":"diegodfsd",
"name":"diego2",
"phone":{
"0":{
"number":"89311768",
"type":"cellphone"
},
"1":{
"number":"55555555",
"type":"home"
}
},
"twitter":"diegodfsd"
}
gist

You need use phones_attributes params instead of phone
curl -X POST -d "contact[name]=nome&contact[email]=email#dominio.com&contact[phones_attributes][0][number]=88888888&contact[phones_attributes][0][type]=1&contact[phones_attributes][2][number]=77777777&contact[phones_attributes][3][type]=1"

Related

How to make a correct HTTP Post request with Meteor.js to Domino Datalab's Rest API

In my server side Meteor.js method, I'm trying to correctly make a request to Domino Data Lab's (DDL) Rest API.
DDL provides a platform for makes it possible to call a data science model via a REST API. Their documentation on this API is here:
http://support.dominodatalab.com/hc/en-us/articles/204173149-API-Endpoints-Model-Deployment
But, I doubt the documentation is helpful because I think an experienced Meteor developer will see the request examples in CURL or Python and know how to get the params correctly into the JSON format that DDL is looking for.
Domino Datalab provides the instructions for 4 methods, but not for Meteor.js. I'll post the examples for Curl and Python:
Examples
CURL Request
curl -v -X POST \
https://app.dominodatalab.com/MYURL \
-H 'Content-Type: application/json' \
-H 'X-Domino-Api-Key: YOUR_API_KEY' \
-d '{"parameters": [ "FOO", "BAR", "ETC"]}'
Python Request
import requests
response =
requests.post("https://app.dominodatalab.com/MYURL",
headers = {
"X-Domino-Api-Key": "YOUR_API_KEY",
"Content-Type": "application/json"
},
json = {
"parameters": ["FOO", "BAR", "ETC"]
}
)
print(response.status_code)
print(response.headers)
print(response.json())
I've tried a few different ways (using both the dataand paramsoptions) based on the documentation for Meteor, but here is my best try:
Meteor.methods({
score_app: function(){
var test = HTTP.call("POST", "https://app.dominodatalab.com/MYURL",
{ headers: {
"Content-Type": "application/json",
"X-Domino-Api-Key": "YOUR_API_KEY"
},
// This is where the problem is. Have tried multiple syntax versions and tried using the `params`options for the HTTP call instead of `data`
data: {'params': [143]
}
},
function (error, result) {
// The syntax below should be if not an error, log the result (for testing etc, otherwise, log "http post error". I may have incorrectly switched this around, but the original version I got from an online example had it the console.log statements in the reverse order.
if (!error) {
console.log(result);
} else{
console.log("http post error");
};
});
}
});
I've been using this entry in the Meteor documentation to send the parameters as a JSON object correctly:
http://docs.meteor.com/api/http.html
The connection to Data Domino Lab (DDL) is made correctly, but it doesn't recognize the parameters correctly because the request is not sending the parameters in the JSON format that DDL wants.
result: 'You must provide a JSON object in your request body
with a parameters key containing an array of parameters.' } }
I'm on the DDL free plan, but I will email a link to this question to their tech support. This is a niche issue, but it could be important to Meteor.js developers in the future wishing to link to a data science model in DDL.
I'm one of the engineers at Domino who has worked on the API Endpoints feature recently. The error
message you're getting means that the JSON object you're sending to our server doesn't contain the
key "parameters". I'm not an expert in Meteor, but it looks like you're using "params" where you
should use "parameters" in your JSON payload.
Around line 9 can you change...
{'data': {'params': [143]}}
to
{'data': {'parameters': [143]}}
If my understanding of your code is correct, that'll work correctly.
Cheers!

Send advanced push notification in parse.com with Rest API

Im trying to send push with two criteria in where.
I make this so:
curl -X POST
-H "X-Parse-Application-Id: myappId" \
-H "X-Parse-REST-API-Key: myRESTApiId" \
-H "Content-Type: application/json" \
-d '{
"where": {“$and”:[{“deviceType": "winphone”},{”channels":{"$all":[“string1”],"$nin":[“string2”]}}]},
"data": {"alert": “String1 is comming”}
}' \
https://api.parse.com/1/push
Something like: https://parse.com/questions/rest-api-or-constraint-on-multiple-fields-using-where, but I getting error message: error code 107 - invalid JSON Parse
How can I send push notification for given device and for given channel with condition $all and $nin.
Thanks for your help!
Hipek
This error is likely being returned because your where value does not match the REST API spec. You will also want to make sure you are consistent in your use of double quotes as these can also lead to malformed JSON errors (e.g. do not use “ and ”, use ").
After fixing that, we end up with the following, which is still not valid per the REST API Parse docs:
"where": {
"$and": [
{"deviceType": "winphone”},
{"channels": {
"$all": ["string1"],
"$nin":["string2"]}
}
]
},
There's a couple of problems with your query:
$and is not a valid Parse REST API operator, and does not appear in the REST API docs. All constraints in a where query are implicitly ANDed, so this is unnecessary, anyway.
Your $all and $nin constraints over channels conflict with each other as there cannot be more than one such query per key. You may want to instead create a unique channel for those installations that should receive messages aimed at the string1 channel but not the string2 channel.

How do I retrieve thumbnail from facebook ad api?

With facebook ad api, I can send a query to retrieve ad image information.
The following is the examples from facebook doc. https://developers.facebook.com/docs/reference/ads-api/adimage/v2.2
curl -G \
-d "access_token=<ACCESS_TOKEN>" \
"https://graph.facebook.com/act_<ACCOUNT_ID>/adimages"
curl -G \
-d "hashes=[%220d500843a1d4699a0b41e99f4137a5c3%22,%22012feg987e98g789f789e87976210983%22]" \
-d "access_token=<ACCESS_TOKEN>" \
"https://graph.facebook.com/act_<AD_ACCOUNT_ID>"
Both calls work fine. But the problem is: they return just the array of image id and image hash, nothing else. Again, example from facebook doc. My test shows response with the same format.
{
"data": {
{
"hash": "0d500843a1d4699a0b41e99f4137a5c3",
"id": "16522000:0d500843a1d4699a0b41e99f4137a5c3"
},
{
"hash": "012feg987e98g789f789e87976210983",
"id": "16522001:012feg987e98g789f789e87976210983"
}
},
"paging": {
"cursors": {
"before": "NDIyNDAzMzc0NDY4NjQxOjE2...",
"after": "NDIyNDAzMzc0NDY4NjQxOmU5Njg..."
}
}
}
Ad Image objects are supposed to have 'url', 'width', 'height', properties. But I cannot retrieve anything more than id and hash, whatever I try.
Any way to get thumbnail url or other image properties using image hash or ad account id?
What I want to achieve ultimately is make a migration from manual management to automatic api based management, and get the url/properties of images already uploaded to facebook (to be saved in db and reused when necessary).
Like almost for all the Ad Objects, if you call an Ad Image endpoint without specifying any additional fields you only get and id and, sometimes, a few more parameters.
If you look carefully in the section "Create" of that page you'll find a list of fields; Add the needed one as a field param in your curl request.
Ex.
curl -G \
-d "access_token=<ACCESS_TOKEN>" \
-d "fields=id,url,whatever" \
"https://graph.facebook.com/act_<ACCOUNT_ID>/adimages"

Difference between XPOST and XPUT

I'm just starting to use ElasticSearch. And I tried to know how to insert documents. I only found examples using the PUT method : $ curl -XPUT 'http://localhost:9200/...'
But it also seems to work using POST. Is there any difference between these two methods?
Thank you.
Generally when using a REST API:
- POST is used to create a resource, where the server will pick an ID.
- PUT is used to update OR PLACE a resource at a known ID.
Doc creation examples in the ES documentation show the caller picking an ID.
Like so:
curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}'
Since the caller is picking the ID a PUT seems appropriate here.
BUT
using POST Elasticsearch can also generate an ID for you.
$ curl -XPOST 'http://localhost:9200/twitter/tweet/' -d '{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}'
Somehow they have the same functionality with small different
PUT verb (“store this document at this URL”)
POST verb (“store this document under this URL”)
in the put you must indicate the exact URL but in the post you can set document by just type
for example:
PUT /website/blog/123 says put this document at exact this URL but POST /website/blog will insert the document in blog and auto increment the id of the last document.

Send a push to people who don't subscribe to a channel using Parse?

I'm able to send push notifications to mobile clients that subscribe to channels (let's say channel_1 and channel_2 using Parse REST API by POSTing a JSON to https://api.parse.com/1/push:
{
"channels": ["channel_1", "channel2"],
"data": { "alert": "Test" }
}
However, I'd like to send the notifications to people who subscribe to channel_1 and channel_2 but also don't subscribe to another specific channel (channel_3).
Is there a way to do that with Parse REST API? I know I can do that via Parse's admin panel.
I think advanced targeting is a better fit for what you're trying to achieve
https://parse.com/docs/push_guide#sending-channels/REST
It is easier to understand the concept looking at the Javascript documentation and then replicate for REST.
I posted my question on Parse forum, and someone from Parse responded:
In this case, you'll want to use Advanced Targeting. Basically, construct a query over the Installation class that targets objects subscribed to both channels, and don't have the third in the "channels" key.
curl -X POST \
-H "X-Parse-Application-Id: YOUR_APP_ID" \
-H "X-Parse-REST-API-Key: YOUR_REST_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"where": {
"channels": {"$all": ["channel_1", "channel_2"], "$nin": ["channel_3"]}
},
"data": {
"alert": "Hello world!"
}
}' \
https://api.parse.com/1/push