Matching expressions for Twilio TaskRouter workers on data in objects in arrays - twilio

I've got Twilio Taskrouter workers with attributes that look like as follows:
{
"name": "Bob",
"id": "45",
"roles": [
{ "id": "19", "name": "Foobar" },
{ "id": "20", "name": "Foobaz" }
]
}
I'd like to write a queue expression to only match Workers with roles with an id of 20. How would I do that?
It would look something like...
"20" in roles.id
...but this doesn't work. As it seems Taskrouter is not smart enough to "unroll" the ids and match within them (like using a tool like jq). I am not able to find a solution in the Twilio Taskrouter expression docs.

Twilio developer evangelist here.
I can't find a solution for you with the data like that. A workaround I just considered would be to add an array of, for example, role_ids to your worker as well. You can keep the existing array of roles, but add a simpler data type to use in the expression matching.
So, the attributes would look like this:
{
"name": "Bob",
"id": "45",
"roles": [
{ "id": "19", "name": "Foobar" },
{ "id": "20", "name": "Foobaz" }
],
"role_ids": ["19", "20"]
}
And you could then use the expression:
"20" in role_ids

Related

Can Avro schema have unknown / dynamic field names?

Consider the following json:
{
"id": "5ffhj4ksWRyt",
"name": "some name",
"custom_fields": [
"randomFieldName1": "a",
"randomFieldName2": "b"
]
id and name fields are always known, but custom_fields names are dynamic and can have any key name. Is it possible to write a schema that will take that into account ?
You can define custom_fields as a map
https://avro.apache.org/docs/current/spec.html#Maps

Getting Zero fee in Stripe Balance Transaction API Response

I am using Stripe API for receiving the contribution from multiple user. So I want to generate statement for our customer so they can see what exact amount, fee deduction, transaction count etc.
After looked into Stripe API I found there are two API(Stripe::Transfer and Stripe::BalanceTransaction) these can be used to fullfil my requirement.
So as per documentation Stripe API I am calling Stripe::Transfer API and this API returning correct response like below
transfer = #<Stripe::Transfer:0x3f997455ac88 id=tr_1xxxxxxxxxxxxxx> JSON: {
"id": "tr_1xxxxxxxxxxxxxxxxx",
"object": "transfer",
"amount": 9510,
"amount_reversed": 0,
"application_fee": null,
"balance_transaction": "txn_123",
"created": 1477485158,
"currency": "usd",
"date": 1477485158,
"description": null,
"destination": "acct_xxxxxxxxxxxxxx",
"destination_payment": "py_xxxxxxxxxxx",
"failure_code": null,
"failure_message": null,
"livemode": false,
"metadata": {},
"method": "standard",
"recipient": null,
"reversals": {"object":"list","data":[],"has_more":false,"total_count":0,"url":"/v1/transfers/tr_xx/reversals"},
"reversed": false,
"source_transaction": "ch_xxxxxxxxx",
"source_type": "card",
"statement_descriptor": null,
"status": "paid",
"type": "stripe_account"
}
I am calling Stripe::BalanceTransaction API to get the details particular transfer's transactions
balance_transaction_id = transfer.balance_transaction
Stripe::BalanceTransaction.retrieve(balance_transaction_id)
Getting below response
#<Stripe::BalanceTransaction:0x3f9974f2f1c8 id=txn_123> JSON: {
"id": "txn_123",
"object": "balance_transaction",
"amount": -9510,
"available_on": 1478044800,
"created": 1477485158,
"currency": "usd",
"description": null,
"fee": 0,
"fee_details": [
],
"net": -9510,
"source": "tr_xxxxxxxx",
"sourced_transfers": {"object":"list","data": [],"has_more":false,"total_count":0,"url":"/v1/transfers? source_transaction=tr_xxxx"},
"status": "available",
"type": "transfer"
}
I setup application fee(2%) and this showing correct on Stripe Dashboard but in the above response I am getting fee is 0
So how can I get fee and total_count values in this response?
You don't want to look at the transfer and its balance transaction here to get the fees. This is an automatic transfer between accounts from a destination payment so there is no fee linked to it.
You need to retrieve the original charge in source_transaction and then look at this charge's own balance_transaction and application_fee objects.
You can even use the expand feature to do this in one API call:
https://stripe.com/docs/api#expanding_objects
Regarding the total count I suspect you want the number of transactions for this user, then you need to look at this when listing all transfers to this account and add the following optional parameter to the request:
:include => ['total_count']
This would only work if you don't create manual transfers to the connected account on top of destination charges though.

Update an array of object using CouchRest, Rails

Im trying to update a parameter in a collection of objects using Rails, CouchRest.
This is what im doing currently,
class User
property :country
property :some_flag
end
#users = User.by_country(:key => 'country-name')
#users.each do |user|
user.update_attributes({:some_flag => 'true'})
end
Whenever update_attributes failed for a User object i want the entire transaction to be rolled back. How can i achieve this?
Am Using CouchDB. Rails, CouchRest and Not using ActiveRecord.
CouchDB doesn't explicitly support transaction, but there is something called "bulk update" which may be of help:
http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API#Modify_Multiple_Documents_With_a_Single_Request
Example data for the update:
{
"all_or_nothing": true,
"docs": [
{"_id": "0", "_rev": "1-62657917", "integer": 10, "string": "10"},
{"_id": "1", "_rev": "2-1579510027", "integer": 2, "string": "2"},
{"_id": "2", "_rev": "2-3978456339", "integer": 3, "string": "3"}
]
}

Rails 3 How can I allow nested attributes to be passed without the _attributes designation

When using accepts_nested_attributes_for, instead of having to pass "child_attributes", I'd like to pass "child". I'm pretty sure if I put a lot of the logic in my controller to create the the records and children, I could accomplish this. However, in an effort to keep my controllers clean and logic where it should be, the model in this case, I'd like to know how to switch rails 3 around to use this syntax when doing a POST or PUT.
{
"name": "test",
"child_attributes": [
{
"id": 1,
"name": "test_child_update"
},
{
"name": "test_child_create"
}
}
Rather
{
"name": "test",
"child": [
{
"id": 1,
"name": "test_child_update"
},
{
"name": "test_child_create"
}
}
Evidently, this can't be done.
The _attributes suffix adds no value to JSON requests and responses, but to get rid of it in the model layer, you would have to monkey patch ActiveRecord. Everyone hates monkey-patching ActiveRecord relations.
How about doing it in the controller layer?
#comment = Comment.new(attributify(:comment))
# snip
# ApplicationController
def attributify()
# Now I'll go and write this!
end
Edit: Done. The controller mixin is here: https://gist.github.com/johncant/6056036

Build json from ValueObjects in Rails 3?

i was wondering how i can build a json response from a value object?
Situation?
I want to return clear json, only the field i need in the front-end. Which means: All associations should be included in the json. But again: only the fields I need. This i why i would like to use a special value object (defining the field) on top of my models.
Problem?
Is this a good idea? How to build value objects (VOs) with rails?
Thanks for helping
Of course it's possible. Look here:
Here is an example:
konata.to_json(:only => [ :id, :name ])
# => {"id": 1, "name": "Konata Izumi"}
As you're talking about associations:
konata.to_json(:include => :posts)
# => {"id": 1, "name": "Konata Izumi", "age": 16,
"created_at": "2006/08/01", "awesome": true,
"posts": [{"id": 1, "author_id": 1, "title": "Welcome to the weblog"},
{"id": 2, author_id: 1, "title": "So I was thinking"}]}
This one sounds interesting:
http://fabrik42.github.com/acts_as_api/

Resources