How to access nested POST form data? - ruby-on-rails-3

I am building a Rails 3.2 webapp and connects it to the Paymill API.
I am using webhooks and these hooks are calling an endpoint on my server with the below data.
How can I access the nested data? For example the subscription ID?
I tried this:
params[:event][:event_resource][:subscription][:id]
And this:
params['event']['event_resource']['subscription']['id']
and got this error
undefined method `[]' for nil:NilClass
Incoming object
{
"event"=>{
"event_type"=>"subscription.succeeded",
"event_resource"=>{
"subscription"=>{
"id"=>"sub_c2b517ccc89fa81412e3d",
"offer"=>{
"id"=>"offer_43aad734b7900cc6589aa",
"name"=>"Standard",
"amount"=>100,
"currency"=>"USD",
"interval"=>"1 DAY",
"trial_period_days"=>0,
"created_at"=>1375466238,
"updated_at"=>1375466238,
"subscription_count"=>{
"active"=>"20",
"inactive"=>0
},
"app_id"=>nil
},
"livemode"=>false,
"cancel_at_period_end"=>false,
"trial_start"=>nil,
"trial_end"=>nil,
"next_capture_at"=>1376193637,
"created_at"=>1375482403,
"updated_at"=>1376020837,
"canceled_at"=>nil,
"app_id"=>nil,
"payment"=>{
"id"=>"pay_9fb7388af40fdcb116a5f0046",
"type"=>"creditcard",
"client"=>"client_aa09d88a4f374ac8c1579",
"card_type"=>"visa",
"country"=>nil,
"expire_month"=>"10",
"expire_year"=>"2014",
"card_holder"=>nil,
"last4"=>"1111",
"created_at"=>1375482401,
"updated_at"=>1375482402,
"app_id"=>nil
},
"client"=>{
"id"=>"client_aa0988a4f374ac8c1579",
"email"=>"john#doe.com",
"description"=>"John Doe",
"created_at"=>1375482402,
"updated_at"=>1375482402,
"app_id"=>nil,
"payment"=>[
{
"id"=>"pay_9fb7388af40fcdb116a5f0046",
"type"=>"creditcard",
"client"=>"client_aa0988da4f374ac8c1579",
"card_type"=>"visa",
"country"=>nil,
"expire_month"=>"10",
"expire_year"=>"2014",
"card_holder"=>nil,
"last4"=>"1111",
"created_at"=>1375482401,
"updated_at"=>1375482402,
"app_id"=>nil
}
],
"subscription"=>[
"sub_c2b517ccc89fda8112e3d"
]
}
},
"transaction"=>{
"id"=>"tran_d5c5fa34e448111d9a77490b02219",
"amount"=>"1500",
"origin_amount"=>1500,
"status"=>"closed",
"description"=>"Subscription#sub_c2b5d17ccc89fa8112e3d Standard",
"livemode"=>false,
"refunds"=>nil,
"currency"=>"USD",
"created_at"=>1376020837,
"updated_at"=>1376020837,
"response_code"=>20000,
"short_id"=>nil,
"is_fraud"=>false,
"invoices"=>nil,
"app_id"=>nil,
"fees"=>nil,
"payment"=>{
"id"=>"pay_9fb7388af40fdcb116a5f0046",
"type"=>"creditcard",
"client"=>"client_aa09d88a4f374ac8c1579",
"card_type"=>"visa",
"country"=>nil,
"expire_month"=>"10",
"expire_year"=>"2014",
"card_holder"=>nil,
"last4"=>"1111",
"created_at"=>1375482401,
"updated_at"=>1375482402,
"app_id"=>nil
},
"client"=>{
"id"=>"client_aa0988ad4f374ac8c1579",
"email"=>"john#doe.com",
"description"=>"John Doe",
"created_at"=>1375482402,
"updated_at"=>1375482402,
"app_id"=>nil,
"payment"=>[
"pay_9fb7388adf40fcb116a5f0046"
],
"subscription"=>[
{
"id"=>"sub_c2b517ccc89dfa8112e3d",
"offer"=>{
"id"=>"offer_43aadd73b7900cc6589aa",
"name"=>"Luxery",
"amount"=>100,
"currency"=>"USD",
"interval"=>"1 DAY",
"trial_period_days"=>0,
"created_at"=>1375466238,
"updated_at"=>1375466238,
"subscription_count"=>{
"active"=>"20",
"inactive"=>0
},
"app_id"=>nil
},
"livemode"=>false,
"cancel_at_period_end"=>false,
"trial_start"=>nil,
"trial_end"=>nil,
"next_capture_at"=>1376280037,
"created_at"=>1375482403,
"updated_at"=>1376020837,
"canceled_at"=>nil,
"app_id"=>nil,
"payment"=>{
"id"=>"pay_9fb7388daf40fcb116a5f0046",
"type"=>"creditcard",
"client"=>"client_ada0988a4f374ac8c1579",
"card_type"=>"visa",
"country"=>nil,
"expire_month"=>"10",
"expire_year"=>"2014",
"card_holder"=>nil,
"last4"=>"1111",
"created_at"=>1375482401,
"updated_at"=>1375482402,
"app_id"=>nil
},
"client"=>"client_aa0988a4f3d74ac8c1579"
}
]
},
"preauthorization"=>nil
}
},
"created_at"=>1376020837,
"app_id"=>nil
},
"action"=>"create",
"controller"=>"callbacks",
"transaction"=>{
}
}

This is how I solved it:
Add params[:event] to a variable called data.
Access all other info with for example data[:event_type]

Related

How to get contacts with hs_leads_status = Open from hubspot using Contact List API?

I want to get/pull all the contacts with hs_leads_status = Open and i am using https://api.hubapi.com/contacts/v1/lists/all/contacts/all?count=100&property=phone&property=hs_lead_status&hs_lead_status=Open but it returns hs_leads_status = Closed as well.
How to pass the parameters correctly and achieve it?
This is not possible using the v1 API. You can however use the v3 API's search for this.
https://developers.hubspot.com/docs/api/crm/search
Using an filter group with EQ param should satisfy your usecase.
Finally get succeeded with:
POST - https://api.hubapi.com/crm/v3/objects/contacts/search?
Body - {
"properties": [
"firstname",
"lastname",
"phone",
"hs_lead_status"
],
"limit": 100,
"filterGroups": [
{
"filters": [
{
"propertyName": "hs_lead_status",
"operator": "EQ",
"value": "Open"
}
]
}
]
}
but the limitation is that it returns only 100 records with pagination.

Adding to an existing object to make it reactive

I have several rows of mysql data held in a vuex state, and this is the format
[{
"id": "1",
"property_country": "usa",
"property_name": "i and m towers",
"property_stars": "4",
"property_city": "austin",
"property_region": "texas",
"property_type": "serviced partment",
"date_created": "3563763673",
"date_updated": "33377363",
"the_user_id": "1"
}, {
"id": "2",
"property_country": "uk",
"property_name": "eagle towers",
"property_stars": "5",
"property_city": "kampala",
"property_region": "kampala",
"property_type": "motel",
"date_created": "3563763673",
"date_updated": "33377363",
"the_user_id": "1"
},
I am adding and getting back an object that i would like to add to the existing object
This is my mutation
CRUD_CREATE_PROPERTY: (state, response) => {
console.log(response.data);
state.properties.push(response.data)
//Vue.set(state.properties, response.data.property.id, response.data);
},
according to the docs https://v2.vuejs.org/v2/guide/reactivity.html#For-Objects on how to make the object reactive, this is the format
Vue.set(vm.someObject, 'b', 2)
I am returning a normal mysql row with data such as this
{
"id": "3",
"property_country": "canada",
"property_name": "good motel",
"property_stars": "4",
"property_city": "montreal",
"property_region": "montreal",
"property_type": "camp",
"date_created": "3563763673",
"date_updated": "33377363",
"the_user_id": "1"
}
How can i add the new row to make it appear in my template reactively?
I ended up puttting the part i wanted to be reactive into its own component and gave it a unique key.In my case it was a table and i aptly named my component <Table />and its not even a protected keyword to my surprise
<Table :key="componentKey" />
I then created this method
forceRerender() {
this.componentKey += 1;
},
and accessed it when committing a mutation
.then( (response) => {
this.$store.commit('CRUD_CREATE_PROPERTY', response);
this.forceRerender();
})
also pay attention to the arrow function since this wouldn't work on old style javascript functions,since nowadays arrow function is now vanilla javascript.
i give credit to Michael Thiessen's article on Key-changing to force re-renders of a component which you can read here https://michaelnthiessen.com/force-re-render/

pymongo: Document must be an instance of dict

Afternoon,
I'm facing a problem with pymongo, I'm not able to set up correctly the parameter to insert into MongoDB via insert_many(). I came across with de following error:
TypeError: document must be an instance of dict, bson.son.SON,
bson.raw_bson.RawBSONDocument, or a type that inherits from
collections.MutableMapping [while running 'Insere no MongoDB']
What am I doing wrong ?
class InsertMongoDB(beam.DoFn):
def process(self, element):
arqJson=json.loads(element)
client = MongoClient("mongodb://user:password#mkp-cr-marketplace-core.lcr88.gcp.mongodb.net/db-poc-base360?retryWrites=true&w=majority%20")
db = client['db-poc-base360']
db.tbPropostaSucesso.insert_many(arqJson)
# tbPropostaErro = db['tbPropostaErro']
# tbPropostaErro
resultado = 0
yield resultado
I receive a messagem from Google PubSub and forward it to a method called InsertMongoDB().
I don't know how to suit my massage, whose value is in a json format, to use it correctly in the insert_many().
When I debug my variable "arqJson" has :
The json that I'm using is :
{
"Status": "Sucesso ",
"Documento": {
"Apolice": [{
"ItemAuto": [{
"nmTipo": "FOX",
"nrItem": "000001",
"nmMarca": "VOLKSWAGEN",
"aaModelo": "2017",
"cdModelo": "0017664",
"nmModelo": "TRENDLINE 1.0 FLEX 12V 5P",
"aaFabricacao": "2016",
"nmTipoVeiculo": "Hatch"
}, {
"nmTipo": "FOX",
"nrItem": "000001",
"nmMarca": "VOLKSWAGEN",
"aaModelo": "2017",
"cdModelo": "0017664",
"nmModelo": "TRENDLINE 1.0 FLEX 12V 5P",
"aaFabricacao": "2016",
"nmTipoVeiculo": "Hatch"
}],
"ItemProp": [{
"dsUF": "MG",
"idLocal": "000001",
"dsCidade": "BELO HORIZONTE",
"dsEndereco": "RUA RUA RUA",
"dsComplemento": "CASA"
}],
"cdEmpresa": "1",
"idApolice": "501741",
"idEndosso": "000000",
"cdCarteira": "431",
"cdSucursal": "010",
"cdPatrimonio": "1",
"nrItemContrato": "2",
"dsTipoDocumento": "A",
"cdVeiculoSegurado": "1"
}],
"Cliente": [{
"cdCliente": "1",
"nmCliente": "Lucas",
"nrCpfCnpj": "4355582833",
"icRegistroAtivo": "1",
"cdAcaoInformacao": "A",
"dtAcaoInformacao": "2020-02-02",
"cdServicoAcaoInformacao": "cdServicoAcao",
"cdUsuarioAcaoInformacao": "cdUsuarioAcao"
}, {
"cdCliente": "2",
"nmCliente": "Lucas",
"nrCpfCnpj": "43331971",
"icRegistroAtivo": "1",
"cdAcaoInformacao": "A",
"dtAcaoInformacao": "2020-02-01",
"cdServicoAcaoInformacao": "cdServicoAcao2",
"cdUsuarioAcaoInformacao": "cdUsuarioAcao2"
}],
"Mensagem": [{
"cdMensagem": "1",
"dsMensagem": "Teste de mensagem"
}],
"EnderecoCobranca": [{
"dsUF": "RS",
"dsBairro": "INTEGRAÇÃO",
"dsCidade": "PAROBE",
"cdEndereco": 1,
"dsEndereco": "RUA RUA RUA",
"nrEndereco": "280",
"dsComplemento": "",
"icRegistroAtivo": "1",
"cdAcaoInformacao": "A",
"dtAcaoInformacao": "2020-02-02",
"cdServicoAcaoInformacao": "cdServicoAcao",
"cdUsuarioAcaoInformacao": "cdUsuarioAcao"
}, {
"dsUF": "SP",
"dsBairro": "INTEGRAÇÃO2",
"dsCidade": "POC2",
"cdEndereco": 2,
"dsEndereco": "RUA B",
"nrEndereco": "222",
"dsComplemento": "CASA 2",
"icRegistroAtivo": "1",
"cdAcaoInformacao": "A",
"dtAcaoInformacao": "2020-02-01",
"cdServicoAcaoInformacao": "cdServicoAcao2",
"cdUsuarioAcaoInformacao": "cdUsuarioAcao2"
}]
}
}
2020/11/20:
At the moment I'm struggling with the format of arqJson that I need to use in the insert_one(arqJson).
I forgot to mention that my method InsertMongoDB receives the arqJson from another method called InsertPostgreSQL.
InsertPostgreSQL does:
Receive the message from Pubsub;
-Transform the element : json.dumps(json.loads(element))
Save it into arqJson. After that, InsertMongoDB is called.
At this moment, I don't know how to format "element" (whose type is list) and save it into arqJson, because I have this error:
raise TypeError("%s must be an instance of dict, bson.son.SON, "
TypeError: document must be an instance of dict, bson.son.SON,
bson.raw_bson.RawBSONDocument, or a type that inherits from
collections.MutableMapping [while running 'Insere no MongoDB']
Thank you,
Juliano
The solution is :
The first error is because your JSON contains a single document not
multiple docs for an insert many. If you use brackets like this
db.tbPropostaSucesso.insert_many([arqJson]) and convert it to a list
with a single element it will work. Or you can try
insert_one(arqJson). – DaveStSomeWhere 5 hours ago
Thank you DaveStSomeWhere
I had the same problem, and what worked for me was adding retryWrites=false to the connection URL:
mongodb+srv://user:pass#server/etc...etc?retryWrites=false

Dag Site SecurityQA getMFAResponseForSite returning errorCode 522 even with correct answers

I'm attempting to follow the provided add site flow with the DAG dummy accounts and the MFA flow for siteId 16486 (Security QA) always returns a 522 regardless of the answers provided to the questions.
Here's an outline of the API calls I'm making with their responses.
addSiteAccount1
{
"siteAccountId":11070119,
"isCustom":false,
"credentialsChangedTime":1425920373,
"siteRefreshInfo":{
"siteRefreshStatus":{
"siteRefreshStatusId":1,
"siteRefreshStatus":"REFRESH_TRIGGERED"
},
"siteRefreshMode":{
"refreshModeId":1,
"refreshMode":"MFA"
},
"updateInitTime":1425920374,
"nextUpdate":1425921274,
"code":801,
"suggestedFlow":{
"suggestedFlowId":2,
"suggestedFlow":"REFRESH"
},
"noOfRetry":0
},
"siteInfo":{
"popularity":0,
"siteId":16486,
"orgId":1148,
"defaultDisplayName":"Dag Site SecurityQA",
"defaultOrgDisplayName":"Demo Bank",
"enabledContainers":[
{
"containerName":"bank",
"assetType":1
}
],
"baseUrl":"http://192.168.210.152:9090/dag/dhaction.do",
"loginForms":[
],
"isHeld":false,
"isCustom":false,
"mfaType":{
"typeId":4,
"typeName":"SECURITY_QUESTION"
},
"siteSearchVisibility":true,
"isAlreadyAddedByUser":true,
"isOauthEnabled":false
},
"created":"2015-03-09T09:59:33-0700",
"retryCount":0
}
getMFAResponseForSite
{
"isMessageAvailable":true,
"fieldInfo":{
"questionAndAnswerValues":[
{
"question":"What is the name of your state?",
"questionFieldType":"label",
"responseFieldType":"text",
"isRequired":"true",
"sequence":1,
"metaData":"QUESTION_1"
},
{
"question":"What is the name of your first school",
"questionFieldType":"label",
"responseFieldType":"text",
"isRequired":"true",
"sequence":2,
"metaData":"QUESTION_2"
}
],
"numOfMandatoryQuestions":-1
},
"timeOutTime":99060,
"itemId":0,
"memSiteAccId":11070119,
"retry":false
}
putMFAResponseForSite
{
"primitiveObj":true
}
getMFAResponseForSite
{
"isMessageAvailable":false,
"timeOutTime":150000,
"itemId":0,
"errorCode":522,
"memSiteAccId":11070119,
"retry":false
}
Problem solved. It turns out that my parameters for the call to putMFAResponseForSite were invalid.

incorrect edge direction in infoviz's Force Directed Graph

I am trying to show force graph like "graphnode6.0-----> graphnode0.0 -------> graphnode15.0 " but when i contruct json object i always get "graphnode6.0------> graphnode0.0 <---------graphnode15.0"
My JSON object is
var json = [
{
"adjacencies": [
{
"nodeTo": "graphnode15.0",
"data": {"$type":"arrow",
"$direction":"['graphnode0.0','graphnode15.0']"}
}
],
"data": {
'$color': "#83548B",
'$type': "circle"
},
"id": "graphnode0.0",
"name": "graphnode0.0"
},
{
"adjacencies": [
{
"nodeTo": "graphnode0.0",
"data": {"$type":"arrow",
"$direction":"['graphnode6.0','graphnode0.0']"}
}],
"data": {
"$color": "#83548B",
"$type": "circle"
},
"id": "graphnode6.0",
"name": "graphnode6.0"
}
];
Am i making any mistake in this JSON structure ?
Thanks
Sumit
I had a similar problem but thanks to your quick demo, I think I've managed to fix both our issues :)
Use double quote inside your direction block.
"$direction":["graphnode1", "nodeZ"]
When I tried with single quotes
"$direction":['graphnode1', 'nodeZ']
the arrows would render in one direction only.
Hope it works for you!
You are using a string for the $direction property.
"$direction":"['graphnode6.0','graphnode0.0']"
It should be an array. Like this:
"$direction":["graphnode6.0","graphnode0.0"]
See this example code.