How to sum items in a List in Dart - dart

What's the best way to sum all the "amounts" of all "expenses", for each user?
I've tried a few different things but I can't quite get it right. It should return 2 values: 20.0 and 24.90
[
{
"id": 3,
"company": {
"id": 2
},
"user": {
"id": 3,
"first_name": "Fred",
"last_name": "Smith",
"email": "asdfasf",
"is_suspended": false,
"vendor_id": "FS-100",
"username": "etytyurtyu",
"expense": [
{
"id": 7,
"date": "2019-12-14T00:00:00.000Z",
"amount": 20.0,
"payment_type": "companyAccount",
"last_modified": "2019-12-16T23:50:00.459064Z",
"receipt_uri": [
"URL8",
"URL9"
],
"user": {
"id": 3
},
"expense_type": {
"id": 4
},
"booking": {
"id": "HI-3565346"
}
}
]
}
},
{
"id": 2,
"company": {
"id": 2
},
"user": {
"id": 2,
"first_name": "Pierre",
"last_name": "XXXMar",
"email": "asdfasdfads",
"is_suspended": false,
"vendor_id": "PM-100",
"username": "asdfas",
"expense": [
{
"id": 2,
"date": "2019-12-16T00:00:00.000Z",
"amount": 12.45,
"payment_type": "provided",
"last_modified": "2019-12-16T19:01:37.092932Z",
"receipt_uri": [
"URL1"
],
"user": {
"id": 2
},
"expense_type": {
"id": 6
},
"booking": {
"id": "MU-123414"
}
},
{
"id": 5,
"date": "2019-12-08T00:00:00.000Z",
"amount": 12.45,
"payment_type": "provided",
"last_modified": "2019-12-16T23:50:00.459064Z",
"receipt_uri": [
"URL1"
],
"user": {
"id": 2
},
"expense_type": {
"id": 6
},
"booking": {
"id": "MU-123414"
}
},
{
"id": 3,
"date": "2019-12-17T00:00:00.000Z",
"amount": 20.0,
"payment_type": "companyCard",
"last_modified": "2019-12-16T19:01:37.092932Z",
"receipt_uri": [
"URL5",
"URL6"
],
"user": {
"id": 2
},
"expense_type": {
"id": 12
},
"booking": {
"id": "HI-3565346"
}
}
]
}
}
]
Thanks

You would first map the expenses amounts, then fold it into a single value, like this :
double sum = expenses.map((expense) => expense.amount).fold(0, (prev, amount) => prev + amount);

List item
For the sake of completeness: In 2021 you can replace the .fold(0, (prev, amount) => prev + amount) part by just calling .sum on the list. Just import import 'package:collection/collection.dart'; first –
tmaihoff

Related

Twilio IVR Speech Recognition

I'm new to developing IVR with twilio studio so I started with the basic template and even that's not working.
This is the log:
LOG
Split Based On...
DETAIL
Input evaluated to 'Sales.' from '{{widgets.gather_input.SpeechResult}}'
Transitioning to 'say_play_1' because 'Sales.' did not match any expression
The split is set to "Equal to" sales which then connects the call to a number. It's obviously recognizing the correct speech input but still not working. Any ideas?
{
"description": "IVR",
"states": [
{
"name": "Trigger",
"type": "trigger",
"transitions": [
{
"event": "incomingMessage"
},
{
"next": "gather_input",
"event": "incomingCall"
},
{
"event": "incomingRequest"
}
],
"properties": {
"offset": {
"x": 250,
"y": 50
}
}
},
{
"name": "gather_input",
"type": "gather-input-on-call",
"transitions": [
{
"next": "split_key_press",
"event": "keypress"
},
{
"next": "split_speech_result",
"event": "speech"
},
{
"event": "timeout"
}
],
"properties": {
"voice": "alice",
"speech_timeout": "auto",
"offset": {
"x": 290,
"y": 250
},
"loop": 1,
"hints": "support,sales",
"finish_on_key": "",
"say": "Hello, how can we direct your call? Press 1 for sales, or say sales. To reach support, press 2 or say support.",
"language": "en",
"stop_gather": false,
"gather_language": "en-US",
"profanity_filter": "false",
"timeout": 5
}
},
{
"name": "split_key_press",
"type": "split-based-on",
"transitions": [
{
"event": "noMatch"
},
{
"next": "connect_call_to_sales",
"event": "match",
"conditions": [
{
"friendly_name": "1",
"arguments": [
"{{widgets.gather_input.Digits}}"
],
"type": "equal_to",
"value": "1"
}
]
},
{
"next": "connect_call_to_support",
"event": "match",
"conditions": [
{
"friendly_name": "2",
"arguments": [
"{{widgets.gather_input.Digits}}"
],
"type": "equal_to",
"value": "2"
}
]
}
],
"properties": {
"input": "{{widgets.gather_input.Digits}}",
"offset": {
"x": 100,
"y": 510
}
}
},
{
"name": "split_speech_result",
"type": "split-based-on",
"transitions": [
{
"next": "say_play_1",
"event": "noMatch"
},
{
"next": "connect_call_to_sales",
"event": "match",
"conditions": [
{
"friendly_name": "sales",
"arguments": [
"{{widgets.gather_input.SpeechResult}}"
],
"type": "equal_to",
"value": "sales"
}
]
},
{
"next": "connect_call_to_support",
"event": "match",
"conditions": [
{
"friendly_name": "support",
"arguments": [
"{{widgets.gather_input.SpeechResult}}"
],
"type": "equal_to",
"value": "support"
}
]
}
],
"properties": {
"input": "{{widgets.gather_input.SpeechResult}}",
"offset": {
"x": 510,
"y": 510
}
}
},
{
"name": "connect_call_to_sales",
"type": "connect-call-to",
"transitions": [
{
"event": "callCompleted"
}
],
"properties": {
"offset": {
"x": 100,
"y": 750
},
"caller_id": "{{contact.channel.address}}",
"noun": "number",
"to": "12222222",
"timeout": 30
}
},
{
"name": "connect_call_to_support",
"type": "connect-call-to",
"transitions": [
{
"event": "callCompleted"
}
],
"properties": {
"offset": {
"x": 520,
"y": 750
},
"caller_id": "{{contact.channel.address}}",
"noun": "number",
"to": "12222222",
"timeout": 30
}
},
{
"name": "say_play_1",
"type": "say-play",
"transitions": [
{
"next": "gather_input",
"event": "audioComplete"
}
],
"properties": {
"offset": {
"x": 710,
"y": 200
},
"loop": 1,
"say": "not valid choice."
}
}
],
"initial_state": "Trigger",
"flags": {
"allow_concurrent_calls": true
}
}
Twilio developer evangelist here.
That is weird behaviour, mainly because the log says "evaluated to 'Sales.'". Split widgets conditions are not case-sensitive and should trim leading and following white-space. For some reason this appears to have a capital "S" and a full-stop.
I would suggest a couple of things. Firstly, raise a ticket with Twilio support to look into why the condition didn't match correctly.
Then, try some of the other conditions. When I generate a new IVR template in Studio, the conditions use "Matches any of" instead of "Equal to". You might also try "Contains".
So, experiment with the ways you can match the operators, but get support involved to drill down into why it didn't work in the first place.
The period is needed after the word for some reason...I just put both, for example:
"1, 1., Uno, Uno., Una, Una., Uno, Uno., Español, Español., Español, Español., Español, Español."

How to connect Nest.js and NEo4j

I am new to Neo4j and want it to connect with Nest.js,I am using neo4j Version 3.5.12 and
following the tutorial.I connect Nest.js and Neo4j by using Graphql but we cannot get the result and show some JSON type and also I try without using Graphql but the GET and POST were not working.
This my controller
import { Controller, Get } from '#nestjs/common';
import {Neo4jService} from './neo4j.service';
#Controller('neo4j')
export class Neo4jController {
constructor (private readonly retriveNodes: Neo4jService){}
#Get()
async findAll() {
console.log('retriving nodes as per given query in findAll() method. ');
//return'Welcome To All';
return this.retriveNodes.findAll();
}
}
Service
import { Injectable, Inject } from '#nestjs/common';
import * as v1 from 'neo4j-driver';
#Injectable()
export class Neo4jService {
constructor(#Inject("Neo4j") private readonly neo4j: v1.Driver) {}
async findAll(): Promise<any> {
return this.neo4j.session().run('MATCH (n:Movie) RETURN n LIMIT 5');
}
}
Using this I get the result in JSON form.
OUTPUT
{
"records": [
{
"keys": [
"n"
],
"length": 1,
"_fields": [
{
"identity": {
"low": 0,
"high": 0
},
"labels": [
"Movie"
],
"properties": {
"title": "The Martix",
"year": {
"low": 1999,
"high": 0
},
"id": "82999192-ae57-406e-8c19-d7753d0d5748"
}
}
],
"_fieldLookup": {
"n": 0
}
},
{
"keys": [
"n"
],
"length": 1,
"_fields": [
{
"identity": {
"low": 1,
"high": 0
},
"labels": [
"Movie"
],
"properties": {
"title": "A Few Good Men",
"year": {
"low": 1992,
"high": 0
},
"id": "c52c721c-c410-45a2-8d2e-824caa51847a"
}
}
],
"_fieldLookup": {
"n": 0
}
},
{
"keys": [
"n"
],
"length": 1,
"_fields": [
{
"identity": {
"low": 2,
"high": 0
},
"labels": [
"Movie"
],
"properties": {
"title": "Top Gun",
"year": {
"low": 1986,
"high": 0
},
"id": "d121663a-597f-4963-8acc-c68021bee860"
}
}
],
"_fieldLookup": {
"n": 0
}
},
{
"keys": [
"n"
],
"length": 1,
"_fields": [
{
"identity": {
"low": 17,
"high": 0
},
"labels": [
"Movie"
],
"properties": {
"title": "The Martix",
"year": {
"low": 1999,
"high": 0
},
"id": "453941ca-c309-419b-8cad-41d150e06b2a"
}
}
],
"_fieldLookup": {
"n": 0
}
},
{
"keys": [
"n"
],
"length": 1,
"_fields": [
{
"identity": {
"low": 18,
"high": 0
},
"labels": [
"Movie"
],
"properties": {
"title": "A Few Good Men",
"year": {
"low": 1992,
"high": 0
},
"id": "79fc529c-bfd1-4f7d-9bae-7f00b6d7d540"
}
}
],
"_fieldLookup": {
"n": 0
}
}
],
"summary": {
"query": {
"text": "MATCH (n:Movie) RETURN n LIMIT 5",
"parameters": {
}
},
"queryType": "r",
"counters": {
"_stats": {
"nodesCreated": 0,
"nodesDeleted": 0,
"relationshipsCreated": 0,
"relationshipsDeleted": 0,
"propertiesSet": 0,
"labelsAdded": 0,
"labelsRemoved": 0,
"indexesAdded": 0,
"indexesRemoved": 0,
"constraintsAdded": 0,
"constraintsRemoved": 0
},
"_systemUpdates": 0
},
"updateStatistics": {
"_stats": {
"nodesCreated": 0,
"nodesDeleted": 0,
"relationshipsCreated": 0,
"relationshipsDeleted": 0,
"propertiesSet": 0,
"labelsAdded": 0,
"labelsRemoved": 0,
"indexesAdded": 0,
"indexesRemoved": 0,
"constraintsAdded": 0,
"constraintsRemoved": 0
},
"_systemUpdates": 0
},
"plan": false,
"profile": false,
"notifications": [
],
"server": {
"address": "localhost:7687",
"version": "Neo4j/3.5.12"
},
"resultConsumedAfter": {
"low": 191,
"high": 0
},
"resultAvailableAfter": {
"low": 437,
"high": 0
},
"database": {
"name": null
}
}
}
I want both .run and .then but in here only .run is showing .No sugection is showing i want field,properties and id in above code.

How to merge a JSON-object in Jbuilder

I am creating a rails app with Backbone/Marionette frontend.
I want to take the JSON-object below and transform it.
This is how it looks now
[{
"total_entries": 4
},
{
"entries": [{
"user": {
"id": 1,
"fullname": "Paul Paulsen"
},
"reports": [{
"id": 1,
"name": "Anna Pearson",
"relation": "wife",
"phone": "2232",
"email": "adsas#sss.se"
}
]
},
{
"user": {
"id": 2,
"fullname": "Anna Palmgren"
},
"reports": [{
"id": 3,
"name": "Mika",
"relation": "Andersen",
"phone": "12312321",
"email": "aas#sss.se"
}]
}
]
}
]
I want to make it look like this
[{
"total_entries": 4
},
{
"entries": [{
"id": 1,
"fullname": "Paul Paulsen"
}, {
"id": 1,
"name": "Anna Pearson",
"relation": "wife",
"phone": "2232",
"email": "adsas#sss.se"
}, {
"id": 1,
"fullname": "Anna Palmgren"
}, {
"id": 3,
"name": "Mika",
"relation": "Andersen",
"phone": "12312321",
"email": "aas#sss.se"
}
]
}
]
This is the Rabl file/code I use
json.array! [0,1] do |index|
if index == 0
json.total_entries #total
else
json.entries #reports.group_by(&:user) do |user, reports|
json.user user, :id, :fullname
json.reports reports do |report|
json.extract! report, :id, :name, :relation, :phone, :email
end
end
end
end
Try this ruby snippet.
x= [{
"total_entries": 4
},
{
"entries": [{
"user": {
"id": 1,
"fullname": "Paul Paulsen"
},
"reports": [{
"id": 1,
"name": "Anna Pearson",
"relation": "wife",
"phone": "2232",
"email": "adsas#sss.se"
}
]
},
{
"user": {
"id": 2,
"fullname": "Anna Palmgren"
},
"reports": [{
"id": 3,
"name": "Mika",
"relation": "Andersen",
"phone": "12312321",
"email": "aas#sss.se"
}]
}
]
}
]
x[1][:entries] = x[1][:entries].map{|p| [p[:user], p[:reports]]}.flatten

Woocommerce JSON data is not showing in collection view

**
i was trying to load woo commerce product list in my collection view.
i am gating data from server also able to print data. but when i go to
showing data in my collection view cell it's not showing.
**
here what i have done
var newArraivalArray = [AnyObject]()
Here i try to get Json data array
Alamofire.request("https://infinitymegamall.com/wp-json/wc/v2/products?after=2016-12-19T16:39:57-08:00", parameters:
["consumer_key":"*******", "consumer_secret":"*******"])
.responseJSON{ response in
if let json = response.result.value {
newArraivalArray = json as! [AnyObject]
print(json)
}
self.collectionView.reloadData()
}
for number of item in collectionview
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return newArraivalArray.count
}
for showing data in collection view.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cellC = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! NewArraivelCollectionViewCell
let productName = newArraivalArray[indexPath.row]["name"]
cellC.name.text = productName as? String
return cellC
}
but i did't see any data in my collection view cell.but some how product name is not showing in my cell. i have no idea what happening.
here what error i get
Here is my json Data
[
{
"id": 57678,
"name": "Mens Formal Printed Shirt",
"slug": "mens-formal-printed-shirt-3",
"permalink": "https://infinitymegamall.com/product/mens-formal-printed-shirt-3/",
"date_created": "2018-01-10T18:29:58",
"date_created_gmt": "2018-01-10T12:29:58",
"date_modified": "2018-01-10T18:29:58",
"date_modified_gmt": "2018-01-10T12:29:58",
"type": "variable",
"status": "publish",
"featured": false,
"catalog_visibility": "visible",
"description": "<p>Code:-4105110568</p>\n",
"short_description": "<p>Code:-4105110568</p>\n",
"sku": "4105110568",
"price": "1971",
"regular_price": "",
"sale_price": "",
"date_on_sale_from": null,
"date_on_sale_from_gmt": null,
"date_on_sale_to": null,
"date_on_sale_to_gmt": null,
"price_html": "<div class=\"product-price\"><del><span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">৳ </span> 2,190.00</span></del> <ins><span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">৳ </span> 1,971.00</span></ins></div>",
"on_sale": true,
"purchasable": true,
"total_sales": 0,
"virtual": false,
"downloadable": false,
"downloads": [],
"download_limit": -1,
"download_expiry": -1,
"external_url": "",
"button_text": "",
"tax_status": "taxable",
"tax_class": "",
"manage_stock": false,
"stock_quantity": null,
"in_stock": true,
"backorders": "no",
"backorders_allowed": false,
"backordered": false,
"sold_individually": false,
"weight": "",
"dimensions": {
"length": "",
"width": "",
"height": ""
},
"shipping_required": true,
"shipping_taxable": true,
"shipping_class": "",
"shipping_class_id": 0,
"reviews_allowed": true,
"average_rating": "0.00",
"rating_count": 0,
"related_ids": [
47912,
47785,
47886,
48373,
48708
],
"upsell_ids": [],
"cross_sell_ids": [],
"parent_id": 0,
"purchase_note": "",
"categories": [
{
"id": 220,
"name": "Full Sleeve",
"slug": "full-sleeve-casual"
},
{
"id": 37,
"name": "Men",
"slug": "men"
},
{
"id": 53,
"name": "Shirt",
"slug": "shirt"
},
{
"id": 91,
"name": "Casual",
"slug": "casual"
}
],
"tags": [],
"images": [
{
"id": 57683,
"date_created": "2018-01-10T18:25:13",
"date_created_gmt": "2018-01-10T12:25:13",
"date_modified": "2018-01-10T18:25:13",
"date_modified_gmt": "2018-01-10T12:25:13",
"src": "https://infinitymegamall.com/wp-content/uploads/2018/01/Mens-Formal-Printed-Shirt-Sky-Print-4105110568-2190tk-2.png",
"name": "Mens Formal Printed Shirt-Sky Print—–4105110568—-2,190tk (2)",
"alt": "",
"position": 0
},
{
"id": 57684,
"date_created": "2018-01-10T18:25:33",
"date_created_gmt": "2018-01-10T12:25:33",
"date_modified": "2018-01-10T18:25:33",
"date_modified_gmt": "2018-01-10T12:25:33",
"src": "https://infinitymegamall.com/wp-content/uploads/2018/01/Mens-Formal-Printed-Shirt-Sky-Print-4105110568-2190tk-1.png",
"name": "Mens Formal Printed Shirt-Sky Print—–4105110568—-2,190tk (1)",
"alt": "",
"position": 1
}
],
"attributes": [
{
"id": 3,
"name": "Size",
"position": 0,
"visible": true,
"variation": true,
"options": [
"L",
"M",
"XL"
]
}
],
"default_attributes": [],
"variations": [
57682,
57680,
57681
],
"grouped_products": [],
"menu_order": 0,
"meta_data": [
{
"id": 481056,
"key": "_vc_post_settings",
"value": {
"vc_grid_id": []
}
},
{
"id": 481216,
"key": "des_content",
"value": "Code:-4105110568"
},
{
"id": 481217,
"key": "trending_product",
"value": "off"
},
{
"id": 481269,
"key": "post_views",
"value": "3"
}
],
"_links": {
"self": [
{
"href": "https://infinitymegamall.com/wp-json/wc/v2/products/57678"
}
],
"collection": [
{
"href": "https://infinitymegamall.com/wp-json/wc/v2/products"
}
]
}
},
{
"id": 57672,
"name": "Mens Formal Printed Shirt",
"slug": "mens-formal-printed-shirt-2",
"permalink": "https://infinitymegamall.com/product/mens-formal-printed-shirt-2/",
"date_created": "2018-01-10T18:12:29",
"date_created_gmt": "2018-01-10T12:12:29",
"date_modified": "2018-01-10T18:12:29",
"date_modified_gmt": "2018-01-10T12:12:29",
"type": "variable",
"status": "publish",
"featured": false,
"catalog_visibility": "visible",
"description": "<p>Code:-4105110563</p>\n",
"short_description": "<p>Code:-4105110563</p>\n",
"sku": "4105110563",
"price": "1971",
"regular_price": "",
"sale_price": "",
"date_on_sale_from": null,
"date_on_sale_from_gmt": null,
"date_on_sale_to": null,
"date_on_sale_to_gmt": null,
"price_html": "<div class=\"product-price\"><del><span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">৳ </span> 2,190.00</span></del> <ins><span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">৳ </span> 1,971.00</span></ins></div>",
"on_sale": true,
"purchasable": true,
"total_sales": 0,
"virtual": false,
"downloadable": false,
"downloads": [],
"download_limit": -1,
"download_expiry": -1,
"external_url": "",
"button_text": "",
"tax_status": "taxable",
"tax_class": "",
"manage_stock": false,
"stock_quantity": null,
"in_stock": true,
"backorders": "no",
"backorders_allowed": false,
"backordered": false,
"sold_individually": false,
"weight": "",
"dimensions": {
"length": "",
"width": "",
"height": ""
},
"shipping_required": true,
"shipping_taxable": true,
"shipping_class": "",
"shipping_class_id": 0,
"reviews_allowed": true,
"average_rating": "0.00",
"rating_count": 0,
"related_ids": [
48222,
48373,
47892,
50683,
48377
],
"upsell_ids": [],
"cross_sell_ids": [],
"parent_id": 0,
"purchase_note": "",
"categories": [
{
"id": 220,
"name": "Full Sleeve",
"slug": "full-sleeve-casual"
},
{
"id": 37,
"name": "Men",
"slug": "men"
},
{
"id": 53,
"name": "Shirt",
"slug": "shirt"
},
{
"id": 91,
"name": "Casual",
"slug": "casual"
}
],
"tags": [],
"images": [
{
"id": 57676,
"date_created": "2018-01-10T18:07:45",
"date_created_gmt": "2018-01-10T12:07:45",
"date_modified": "2018-01-10T18:07:45",
"date_modified_gmt": "2018-01-10T12:07:45",
"src": "https://infinitymegamall.com/wp-content/uploads/2018/01/Mens-Formal-Printed-Shirt-Maroon-4105110563-2190tk-2.png",
"name": "Mens Formal Printed Shirt-Maroon——4105110563—-2,190tk (2)",
"alt": "",
"position": 0
},
{
"id": 57677,
"date_created": "2018-01-10T18:08:25",
"date_created_gmt": "2018-01-10T12:08:25",
"date_modified": "2018-01-10T18:08:25",
"date_modified_gmt": "2018-01-10T12:08:25",
"src": "https://infinitymegamall.com/wp-content/uploads/2018/01/Mens-Formal-Printed-Shirt-Maroon-4105110563-2190tk-1.png",
"name": "Mens Formal Printed Shirt-Maroon——4105110563—-2,190tk (1)",
"alt": "",
"position": 1
}
],
"attributes": [
{
"id": 3,
"name": "Size",
"position": 0,
"visible": true,
"variation": true,
"options": [
"L",
"M",
"XL"
]
}
],
"default_attributes": [],
"variations": [
57673,
57674,
57675
],
"grouped_products": [],
"menu_order": 0,
"meta_data": [
{
"id": 480903,
"key": "_vc_post_settings",
"value": {
"vc_grid_id": []
}
},
{
"id": 481053,
"key": "des_content",
"value": "Code:-4105110563"
},
{
"id": 481054,
"key": "trending_product",
"value": "off"
},
{
"id": 481055,
"key": "post_views",
"value": "3"
}
],
"_links": {
"self": [
{
"href": "https://infinitymegamall.com/wp-json/wc/v2/products/57672"
}
],
"collection": [
{
"href": "https://infinitymegamall.com/wp-json/wc/v2/products"
}
]
}
}]

Beautify JSON parsing with Hashie::Mash Rash?

Trying to parse some ugly JSON:
image = product.images.find { |i| i["sizeName"] == "Large" }
If I use Hashie::Mash Rash, can I make it look like this instead?
image = product.images.find { |i| i["size_name"] == "large" }
If so, why am I getting undefined method 'each_pair' for #<Array:0x007f84a0408540>? Please see https://gist.github.com/frankie-loves-jesus/6b8012f9197ca6c675a9 for a full example including a live app.
Example JSON:
{
"metadata": {
"category": {
"id": "women",
"name": "Women's Fashion"
},
"showSizeFilter": false,
"showColorFilter": true,
"offset": 0,
"limit": 20,
"total": 974184
},
"products": [
{
"id": 377083005,
"name": "BCBGeneration Women's Contrast Sleeve Trench",
"currency": "USD",
"price": 168,
"priceLabel": "$168.00",
"salePrice": 106.43,
"salePriceLabel": "$106.43",
"inStock": true,
"retailer": {
"id": "849",
"name": "Amazon.com",
"url": "http://www.shopstyle.com/browse/Amazon.com-US?pid=uid9616-726296-93"
},
"locale": "en_US",
"description": "This jacket features contrasting leather sleeves",
"brand": {
"id": "51",
"name": "BCBG MAX AZRIA",
"url": "http://www.shopstyle.com/browse/BCBG-MAX-AZRIA?pid=uid9616-726296-93"
},
"clickUrl": "http://api.shopstyle.com/action/apiVisitRetailer?id=377083005&pid=uid9616-726296-93",
"images": [
{
"sizeName": "Small",
"width": 32,
"height": 40,
"url": "http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a_small.jpg"
},
{
"sizeName": "Medium",
"width": 112,
"height": 140,
"url": "http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a_medium.jpg"
},
{
"sizeName": "Large",
"width": 164,
"height": 205,
"url": "http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a.jpg"
}
],
"colors": [
{
"name": "Chino"
}
],
"sizes": [
{
"name": "XX-Small"
},
{
"name": "X-Small"
}
],
"categories": [
{
"id": "raincoats-and-trenchcoats",
"name": "Raincoats & Trenchcoats"
}
]
}
]
}
This is my working code
require 'json'
require 'rash'
#json_text = <<END
{
"metadata": {
"category": {
"id": "women",
"name": "Women's Fashion"
},
"showSizeFilter": false,
"showColorFilter": true,
"offset": 0,
"limit": 20,
"total": 974184
},
"products": [
{
"id": 377083005,
"name": "BCBGeneration Women's Contrast Sleeve Trench",
"currency": "USD",
"price": 168,
"priceLabel": "$168.00",
"salePrice": 106.43,
"salePriceLabel": "$106.43",
"inStock": true,
"retailer": {
"id": "849",
"name": "Amazon.com",
"url": "http://www.shopstyle.com/browse/Amazon.com-US?pid=uid9616-726296-93"
},
"locale": "en_US",
"description": "This jacket features contrasting leather sleeves",
"brand": {
"id": "51",
"name": "BCBG MAX AZRIA",
"url": "http://www.shopstyle.com/browse/BCBG-MAX-AZRIA?pid=uid9616-726296-93"
},
"clickUrl": "http://api.shopstyle.com/action/apiVisitRetailer?id=377083005&pid=uid9616-726296-93",
"images": [
{
"sizeName": "Small",
"width": 32,
"height": 40,
"url": "http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a_small.jpg"
},
{
"sizeName": "Medium",
"width": 112,
"height": 140,
"url": "http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a_medium.jpg"
},
{
"sizeName": "Large",
"width": 164,
"height": 205,
"url": "http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a.jpg"
}
],
"colors": [
{
"name": "Chino"
}
],
"sizes": [
{
"name": "XX-Small"
},
{
"name": "X-Small"
}
],
"categories": [
{
"id": "raincoats-and-trenchcoats",
"name": "Raincoats & Trenchcoats"
}
]
}
]
}
END
hash = JSON.parse(#json_text)
#rash = Hashie::Rash.new( hash )
images = []
#rash.products.each do |product|
images << product.images.find { |i| i.size_name.downcase == "large" }
end
puts images.inspect
#[#<Hashie::Rash height=205 size_name="Large" url="http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a.jpg" width=164>]
It doesn't raise the error you have mentioned.
And I use
$ruby --version
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin13.0.0]
gem list --local |grep 'rash'
rash (0.4.0)
$gem list --local |grep 'hashie'
hashie (3.2.0, 2.0.5)
Could you check yours?
And, if possible, dump the json at the moment that it raises the error.

Resources