Access to nested elements in list with maps in elixir - list

I am new to elixir an phoenix an got some problems accessing nested elements inside a test. I am testing a controller and got so far following response:
.[%{"attributes" => %{"first_name" => "Timmy 96", "last_name" =>
"Assistant"},
"id" => "bca58c53-7c6e-4281-9bc8-0c4616a30051",
"relationships" => %{"avatar" => %{"data" => %{"id" => "011300fd-ca98-42b4-
9561-f1cdc93d2d25",
"type" => "pictures"}}}, "type" => "users"}]
I am using JSON-API-format for the response and am fetching the attributes with userdata:
user_attr = Enum.filter(includes, fn(item)->
item["relationships"]["avatar"] != nil
end)
IO.inspect user_attr
case Enum.fetch(user_attr ,0) do
{:ok, value} ->
assert value["attributes"]["first_name"] == user.first_name
assert value["attributes"]["last_name"] == user.last_name
{_} ->
assert false
end
I want to shorten this part, dont want to use a case, but no idea how to get the value of the user_attr without using the value part in the case.
I would also want to asser the id of the relationships -> avatar -> data -> id with the id I inserted before, but no idea how to access this value. The id is part of the picture i inserted before so I would like to
assert XXX == picture.id
But how to get the XXX?
Hope someone can help me. Last years only Java and C#, never Ruby and now I got somehow into elixir :/
Thanks.

You can use get_in/2 to do this.
iex()> list
[%{"attributes" => %{"first_name" => "Timmy 96", "last_name" =>
"Assistant"},
"id" => "bca58c53-7c6e-4281-9bc8-0c4616a30051",
"relationships" => %{"avatar" => %{"data" => %{"id" => "011300fd-ca98-
42b4-\n9561-f1cdc93d2d25",
"type" => "pictures"}}}, "type" => "users"}]
iex()> [map] = list
[%{"attributes" => %{"first_name" => "Timmy 96", "last_name" =>
"Assistant"},
"id" => "bca58c53-7c6e-4281-9bc8-0c4616a30051",
"relationships" => %{"avatar" => %{"data" => %{"id" => "011300fd-ca98-
42b4-\n9561-f1cdc93d2d25",
"type" => "pictures"}}}, "type" => "users"}]
iex()> get_in map, ["attributes", "first_name"]
"Timmy 96"
iex()> get_in map, ["attributes", "last_name"]
"Assistant"
iex()> get_in map, ["relationships", "avatar", "data", "id"]
"011300fd-ca98-42b4-\n9561-f1cdc93d2d25"

You should try using pattern matching more.
# fixture data.
user = %{first_name: "Timmy 96", last_name: "Assistant"}
picture = %{id: "011300fd-ca98-42b4-\n9561-f1cdc93d2d25"}
value = %{
"attributes" => %{"first_name" => "Timmy 96", "last_name" => "Assistant"},
"id" => "bca58c53-7c6e-4281-9bc8-0c4616a30051",
"relationships" => %{
"avatar" => %{
"data" => %{
"id" => "011300fd-ca98-42b4-\n9561-f1cdc93d2d25",
"type" => "pictures",
},
},
},
"type" => "users",
}
assert %{"attributes" => attributes} = value
# ensure the expected value match with actual value and than bind the attributes variable with actual attributes map.
assert %{"first_name" => user.first_name, "last_name" => user.last_name} == attributes
assert %{"relationships" => %{"avatar" => %{ "data" => avatar_data}}} = value
assert %{"id" => picture.id, "type" => "pictures"} == avatar_data
One of Elixir's most powerful features is pattern matching via the = operator(match operator).
The above example shows you that we can use match operator to assert that the data structures of expected value match with actual value.
Learn more about testing and pattern matching:
https://semaphoreci.com/community/tutorials/introduction-to-testing-elixir-applications-with-exunit

Related

Creating Salesorder with Vtiger Webservice missing Tax

I have the Problem when creating an salesorder with Vtiger Webservice,
The sales order is created but somehow the tax is not added.
I thought it has something to do with the parameter : hdnTaxType
Cause even if I add this value 'group' it does not apply the Tax to the Salesorder.
I have to manually add The Taxtype 'group' then the system adds the tax.
thats why i tried to add values like:
'tax1' => '7.00',
and
'group_tax' =>[
'group_tax_percentage1' => '7.0'
],
nothing so far did help...
has anybody an Idea what the problem is?
Thank you
Tobi
$salesOrder =[
'lastname' => $customer['lastname'],
'subject' => 'Order from 01.01.1018',
'sostatus' => '1',
'assigned_user_id' => '',
'bill_street' => 'Rechunngsstrasse 123',
'ship_street' =>'Lieferungsstrasse 123',
'productid' => '14x4325',
'currency_id' => '21x1',
'carrier' => 'DHL',
'txtAdjustment' => '13',
'salescommission' => '12',
'exciseduty' => '15',
'hdnTaxType' => 'group',
'tax1' => '7.00',
'hdnS_H_Amount' => '22.22',
'group_tax' =>[
'group_tax_percentage1' => '7.0'
],
'LineItems' => [
0 => [
"taxid" => "33x1",
'productid'=>'14x6',
'listprice'=>'20.53',
'quantity'=>'3',
'comment' => "Product123"
]
]
Webservices in Vtiger are not complete.
In this case you can register your own webservice that makes your queries, or check the SalesOrder in the after-save event

Query string in drupal service custom resources

I am trying to use query string structure in drupal services API. it's not working for me.
I have also search most of the solutions, but all failed.
here is m drupal code:
function rapid_services_resources() {
$resources = array(
'get_data' => array(
'operations' => array(
'retrieve' => array(
'help' => t('Gets user email of uid passed.'),
'callback' => 'my_module_get_user_email',
'args' => array(
array(
'name' => 'nid',
'type' => 'int',
'description' => 'The display ID of the view to get.',
'source' => array('param' => 'nid'),
'optional' => TRUE,
'default value' => 'default',
),
),
'access arguments' => '_blog_access_provide_access',
'access callback' => '_blog_access_provide_access',
//~ 'access arguments append' => FALSE,
),
),
),
);
return $resources;
}
function _blog_access_provide_access() {
return TRUE;
}
function my_module_get_user_email($nid){
var_dump($args);die;
}
I want url like this.:
http://localhost/drupaltest/rapidapi/get_data/?nid=111
Please let me know where i did wrong.
thanks in advance.
Hi here are to reference that will be useful
http://pingv.com/blog/an-introduction-drupal-7-restful-services
https://www.drupal.org/node/783460
Also I am not sure about the query string, but for the retrieve operation you can set it as part of the url
ex:
http://localhost/drupaltest/rapidapi/get_data/<nid should be here>
http://localhost/drupaltest/rapidapi/get_data/111
Also using the source path as source
'source' => array('path' => '0'),
I would think that source param only works for an index operation and not retrieve

Calabash: Get state of toggle switch

I am trying to get the state of a toggle switch in my native app.
This is the query of the switch itself, when ON:
query "Switch id:'quadrant_buzz'"
and its results
[
[0] {
"class" => "android.widget.Switch",
"tag" => nil,
"description" => "android.widget.Switch{29ddf658 VFED..C. ........ 877,0-1017,81 #7f10017f app:id/quadrant_buzz}",
"id" => "quadrant_buzz",
"text" => "",
"visible" => true,
"rect" => {
"height" => 81,
"width" => 140,
"y" => 1303,
"x" => 877,
"center_x" => 947,
"center_y" => 1343
},
"enabled" => true,
"contentDescription" => nil
}]
This is the result of the query when the switch is OFF:
[
[0] {
"class" => "android.widget.Switch",
"tag" => nil,
"description" => "android.widget.Switch{29ddf658 VFED..C. ........ 877,0-1017,81 #7f10017f app:id/quadrant_buzz}",
"id" => "quadrant_buzz",
"text" => "",
"visible" => true,
"rect" => {
"height" => 81,
"width" => 140,
"y" => 1213,
"x" => 877,
"center_x" => 947,
"center_y" => 1253
},
"enabled" => true,
"contentDescription" => nil
}]
I am unsure how to check for its on/off state. When I tap/touch the switch, nothing changes in the returned query. The only difference I see is the center-y.
Is there something in the Calabash Api for this?
Thanks
Looking up the documentation for a Switch widget on Android developers it is the method "isChecked". http://developer.android.com/reference/android/widget/Switch.html
Therefore you can find this value in Calabash by invoking
query("Switch id:'quadrant_buzz'", :isChecked)
You could also used just :checked in Calabash.
The isChecked works.
irb(main):001:0> query("android.widget.Switch",:isChecked)
[
[0] true
]

In Rails4 app, integrating Stripe, but Stripe says "Received unknown parameter: type"

UPDATE:
Ah! This fixed it:
stripe_account = Stripe::Account.create(
{
:legal_entity => { :type => "company" },
:country => "US",
:managed => true
}
)
In our Rails4 app we are trying to integrate Stripe. We are building an auction site, and we will use Stripe to both charge customers and then also send the money to the sellers.
We set up the pages that deal with charging, and everything works great on that side. Customers can make a purchase. We auth their cards, and then, when the seller delivers the product, we capture the charge. 100% perfection.
However, I have run into problems when I try to create the managed accounts for the sellers. This was working fine:
stripe_account = Stripe::Account.create(
{
:country => "US",
:managed => true
}
)
But the documentation says that I need to specify whether the seller is an individual or a company, so I did:
stripe_account = Stripe::Account.create(
{
:type => "company",
:country => "US",
:managed => true
}
)
And now I get this error:
Stripe::InvalidRequestError in Supplier::ProfilesController#new
Received unknown parameter: type
If I look here:
https://stripe.com/docs/api#create_account
I see:
type
string
Either “individual” or “company”, for what kind of legal entity the account owner is for
So why is this an error?
UPDATE:
Ah! This fixed it:
stripe_account = Stripe::Account.create(
{
:legal_entity => { :type => "company" },
:country => "US",
:managed => true
}
)
Ah! This fixed it:
stripe_account = Stripe::Account.create(
{
:legal_entity => { :type => "company" },
:country => "US",
:managed => true
}
)

Dynamodb query error - Query key condition not supported

I am trying to query my dynamodb table to get feed_guid and status_id = 1. But it returns Query key condition not supported error.
Please find my table schema and query.
$result =$dynamodbClient->createTable(array(
'TableName' => 'feed',
'AttributeDefinitions' => array(
array('AttributeName' => 'user_id', 'AttributeType' => 'S'),
array('AttributeName' => 'feed_guid', 'AttributeType' => 'S'),
array('AttributeName' => 'status_id', 'AttributeType' => 'N'),
),
'KeySchema' => array(
array('AttributeName' => 'feed_guid', 'KeyType' => 'HASH'),
),
'GlobalSecondaryIndexes' => array(
array(
'IndexName' => 'StatusIndex',
'ProvisionedThroughput' => array (
'ReadCapacityUnits' => 5,
'WriteCapacityUnits' => 5
),
'KeySchema' => array(
array(
'AttributeName' => 'status_id',
'KeyType' => 'HASH'
),
),
'Projection' => array(
'ProjectionType' => 'ALL'
)
),
array(
'IndexName' => 'UserIdIndex',
'ProvisionedThroughput' => array (
'ReadCapacityUnits' => 5,
'WriteCapacityUnits' => 5
),
'KeySchema' => array(
array(
'AttributeName' => 'user_id',
'KeyType' => 'HASH'
),
),
'Projection' => array(
'ProjectionType' => 'ALL'
)
)
),
'ProvisionedThroughput' => array(
'ReadCapacityUnits' => 10,
'WriteCapacityUnits' => 20
)
));
Following is my query to update that table.
$result = $dynamodbClient->query(array(
'TableName' => 'feed',
'KeyConditionExpression' => 'feed_guid = :v_fid AND status_id = :v_sid ',
'ExpressionAttributeValues' => array(
':v_fid' => array('S' => '71a27f0547cd5456d9ee7c181b6cb2f8'),
':v_sid' => array('N' => 1)
),
'ConsistentRead' => false
));
As mentioned, the attribute included in "KeyConditionExpression" should be your hash key only, matching your base table schema (in this case 'feed_guid'). If you want to query on both 'feed_guid' and 'status_id', you need to create the table with hash and range key and specify 'status_id' as your range key.
Global secondary indexes are completely separate from the base table, so in this case you can query the indexes separately (use 'status_id' in key condition when querying StatusIndex and use 'user_id' in key condition when querying UserIdIndex).
Please find more details on querying global secondary indexes here
Another option would be to use a FilterExpression in addition to the KeyConditionExpression. As Daniela mentioned KeyConditionExpression should contain only columns in the hash key. But any un-indexed columns can be included in the FilterExpression.
You need to have both hash_key (Partition Key) and range_key (Sort Key) provided in the KeyConditionExpression. hash_key must be exact match (=), but the range_key can use other operators like 'begins_with'