Access Nested Netsuite Ruby Hash - ruby-on-rails

I am trying to access a Netsuite Ruby Hash (Netsuite gem) and return orders which need updating from the db, the process uses two service objects, one to get the orders from db, (ListOrdersService), and the this file to compare those against modified ones in Netsuite. Is all working except I am having problems getting some of the nested values in Netsuite in to the output. Code is below with a troublesome item commented out. Its just an each method which compares dates then puts the needed orders in returned value.
def process_order_updates
get_order_updates = []
# Get all our open orders from DB
bj_open_orders = ListOrdersService.new.call
# Get all identical open orders from Netsuite
bj_open_orders.each do |item|
netsuite_sales_orders = NetSuite::Records::SalesOrder.get(item['sales_order_internal_id'])
# Compare the last modified date from Netsuite to the last checked date from app DB
if netsuite_sales_orders.present? && netsuite_sales_orders.last_modified_date > item['last_checked_date']
# If the last modified date is newer, then we create a new hash with the updated order info
get_order_updates << {
sales_order_internal_id: item['sales_order_internal_id'],
order_status: item['order_status']
# quantity_fulfilled: item['items_list']['item']['quantity_fulfilled']
}
puts "still open order #{item['sales_order_internal_id']} needs to be updated, it was last checked by at #{item['last_checked_date']} but it was just modified, on #{netsuite_sales_orders.last_modified_date}"
end
end
puts "Here are the orders that need to be updated: #{get_order_updates}"
end
The Netsuite file code I am referencing is below, and trying to get quantity_fulfilled, quantity_billed, and some others in the file. items_list is a top level item
"item_list": {
"list": [
{
"attributes": {
"item": {
"internal_id": "110",
"external_id": null,
"type": null,
"attributes": {
"name": "000002 Kerosene (UN1223) 3.PGIII (D/E)"
}
},
"expand_item_group": false,
"quantity": "1000.0",
"units": {
"internal_id": "1",
"external_id": null,
"type": null,
"attributes": {
"name": "ltr"
}
},
"description": "Kerosene (UN1223) 3.PGIII (D/E)",
"price": {
"internal_id": "-1",
"external_id": null,
"type": null,
"attributes": {}
},
"rate": "0.81",
"amount": "810.0",
"is_closed": false,
"gross_amt": "850.5",
"line": "1",
"cost_estimate_type": "_averageCost",
"cost_estimate": "900.79",
"quantity_back_ordered": "0.0",
"quantity_billed": "0.0",
"quantity_committed": "1000.0",
"quantity_fulfilled": "0.0",
"tax1_amt": "40.5",
"tax_code": {
"internal_id": "2214",
"external_id": null,
"type": null,
"attributes": {
"name": "VAT:RDR-5%"
}
},
Any tips on how to get those items, directly or with a hashmap welcome Thanks

Related

Apache Ranger REST API addUsersAndGroups returns 404 not found

We have installed Apache Ranger and the Web UI works fine, most of the REST API method works fine on both PublicAPIsv2 and RoleREST as per https://ranger.apache.org/apidocs/ui/index.html.
I can get “test_role” id by calling GET /public/v2/api/roles/name/test_role which returns the id 409.
I can get test_role content by calling GET /public/v2/api/roles/409
I can change test_role users list by editing the response I get from GET /public/v2/api/roles/409 and submitting it through PUT /public/v2/api/roles/409
The body is:
{
"id": 409,
"isEnabled": true,
"createdBy": "admin",
"updatedBy": "admin",
"createTime": 1598241102841,
"updateTime": 1601975068428,
"name": "test_role",
"options": {},
"users": [
{
"name": "test_user1”,
"isAdmin": true
},
{
"name": “test_user2”,
"isAdmin": true
},
{
"name": “test_user3”,
"isAdmin": false
}
],
"groups": [
{
"name": "test_group”,
"isAdmin": false
}
],
"roles": []
}
But calling PUT /public/v2/api/roles/409/addUsersAndGroups returns “404 not found”.
I tried with the same body as above as parameter, and also with:
{
"users": [
{
"name": “test_user4”,
"isAdmin": true
}
]
}
Would anybody know what is the correct body to send as parameter to:
/public/v2/api/roles/409/addUsersAndGroups?
Also, making a wrong call such as GET /public/v2/api/roles/409/addUsersAndGroups returns “405 method not allowed”. So I believe it shows the end point does exist. I’m not sure why calling PUT public/v2/api/roles/409/addUsersAndGroups with (probably) incorrect body returns “404 not found” and not an error message related to the wrong parameter.
It happens because Apache Ranger API documentation is wrong, remove the suffix /addUsersAndGroups of your endpoint and it will work.
Example: https://ranger_url/service/roles/roles/409
Where 409 is the role ID, as you're using on your example.
The body that is needed:
{
"name": "test_role",
"users": [
{
"name": "test_user1",
"isAdmin": true
}
]
}

Accessing Values in Deeply Nested Array - Ruby on Rails

I am using an API call which returns a JSON response. I want to access the data inside the response so I can create some nice display cards showing the info and pictures. Here is a snippet from the response, the response property is populated with about 20 objects I'll include just two for brevity:
{
"success": true,
"message": "",
"result": [
{
"MarketCurrency": "LTC",
"BaseCurrency": "BTC",
"MarketCurrencyLong": "Litecoin",
"BaseCurrencyLong": "Bitcoin",
"MinTradeSize": 1e-8,
"MarketName": "BTC-LTC",
"IsActive": true,
"Created": "2014-02-13T00:00:00",
"Notice": null,
"IsSponsored": null,
"LogoUrl": "https://i.imgur.com/R29q3dD.png"
},
{
"MarketCurrency": "DOGE",
"BaseCurrency": "BTC",
"MarketCurrencyLong": "Dogecoin",
"BaseCurrencyLong": "Bitcoin",
"MinTradeSize": 1e-8,
"MarketName": "BTC-DOGE",
"IsActive": true,
"Created": "2014-02-13T00:00:00",
"Notice": null,
"IsSponsored": null,
"LogoUrl": "https://i.imgur.com/e1RS4Hn.png"
},
In my Rails controller I'm using JSON.parse and I'm trying to turn it into an object with the Open struct option:
#markets = JSON.parse(markets.to_json, object_class: OpenStruct)
In my view I'll do this <%=#markets.class%> and it shows Array and not object. So I try this <%=#markets.size%> and it shows 1. If I do <%=#markets[0]['success']%> I would expect it to return true but it returns 'success'. So, I'm not understanding why the ostruct library isn't working like I would expect or how I can get to the objects stored in the result array. Any help is greatly appreciated!
You already have a JSON response, isn't needed to use to_json again, try just parsing that object, and then use the dot . to access its fields, as an OpenStruct object now then you can access them as methods:
require 'json'
a = '{
"success": true,
"message": "",
"result": [{
"MarketCurrency": "LTC",
"BaseCurrency": "BTC",
"MarketCurrencyLong": "Litecoin",
"BaseCurrencyLong": "Bitcoin",
"MinTradeSize": 1e-8,
"MarketName": "BTC-LTC",
"IsActive": true,
"Created": "2014-02-13T00:00:00",
"Notice": null,
"IsSponsored": null,
"LogoUrl": "https://i.imgur.com/R29q3dD.png"
}, {
"MarketCurrency": "DOGE",
"BaseCurrency": "BTC",
"MarketCurrencyLong": "Dogecoin",
"BaseCurrencyLong": "Bitcoin",
"MinTradeSize": 1e-8,
"MarketName": "BTC-DOGE",
"IsActive": true,
"Created": "2014-02-13T00:00:00",
"Notice": null,
"IsSponsored": null,
"LogoUrl": "https://i.imgur.com/e1RS4Hn.png"
}]
}'
b = JSON.parse(a, object_class: OpenStruct)
p b.success
# => true
After much debugging and some assistance, I was able to get it to work. The response from the API call was an array with one item. The item was a long string of the entire dataset.
In order to get the expected behavior of "true" when calling #markets.success, I first had to
raw_markets = JSON.parse(markets.to_json)
followed by
#markets = raw_markets.map do |market|
JSON.parse(market, object_class: OpenStruct)
Note: the variable markets holds the original api call:
markets = open('url-to-api')
After this I would get #markets.success = "true" and #markets.result[0] held the first result, #markets.result[1] held the second result, and so on.

Searching nested hash

These is sample response of hashes in ruby.
Eg:-
find abcd1234
should give me
i was able to find by but it's not sufficent
I have response of sth like these and list keep on going different value but same structure
[
{
"addon_service": {
"id": "01234567-89ab-cdef-0123-456789abcdef",
"name": "heroku-postgresql"
},
"config_vars": [
"FOO",
"BAZ"
],
"created_at": "2012-01-01T12:00:00Z",
"id": "01234567-89ab-cdef-0123-456789abcdef",
"name": "acme-inc-primary-database",
"plan": {
"id": "01234567-89ab-cdef-0123-456789abcdef",
"name": "heroku-postgresql:dev"
},
"app": {
"id"=>"342uo23iu4io23u4oi2u34",
"name"=>"heroku-staging"},
},
"provider_id": "abcd1234",
"updated_at": "2012-01-01T12:00:00Z",
"web_url": "https://postgres.heroku.com/databases/01234567-89ab-cdef-0123-456789abcdef"
} .........
]
can anyone know how to grab those?
You can iterate all array element (a hash) and display its content if the hash meet your requirement:
element_found = 0
YOUR_DATA.each do |element|
if element["provider_id"].match(/abcd1234/)
element_found += 1
puts "addon_service: #{element['addon_service']['name']}"
puts "app: #{element['app']['name']}"
end
end
if element_found == 0 puts "Sorry match didn't found"
Since the elements of the array are hashes you can select the appropriate ones by matching the desired key.
select {|app| app[:provider_id] == "abcd1234"}
Do you know what to do with the element once you select it?
I think you want some of the items from the hash, but not all of them.
That might look like:
select {|app| app[:provider_id] == "abcd1234"}.map {|app| app.select {|key, v| [:addon_service, :app].include?(key) } }

Extract specific field from JSON nested hashes

I am thinking of writing a web application that crawls an API and returns this information in JSON form.
However, I am only after one number, then current price (in this sample, "227"). How can I access that in Ruby? I have no clue where to begin. I've never dealt with text like this.
For discussion's sake, suppose I save this output into instance variable #information
{
"item": {
"icon": "http://services.runescape.com/m=itemdb_rs/4332_obj_sprite.gif?id=4798",
"icon_large": "http://services.runescape.com/m=itemdb_rs/4332_obj_big.gif?id=4798",
"id": 4798,
"type": "Ammo",
"typeIcon": "http://www.runescape.com/img/categories/Ammo",
"name": "Adamant brutal",
"description": "Blunt adamantite arrow...ouch",
"current": {
"trend": "neutral",
"price": 227
},
"today": {
"trend": "neutral",
"price": 0
},
"day30": {
"trend": "positive",
"change": "+1.0%"
},
"day90": {
"trend": "positive",
"change": "+1.0%"
},
"day180": {
"trend": "positive",
"change": "+2.0%"
},
"members": "true"
}
}
First follow this post to parse this JSON in to Hash
Parsing a JSON string in Ruby
say the parsed hash name is my_hash then the following should give you price
my_hash['item']['current']['price']
Edit:
As you said you want to save it in #information
#information = my_hash['item']['current']['price']
Even you can use hashie it gives your json into readable structural code
Install Hashie
gem install hashie
then in your code all that json take in a variable my_json
myhash = Hashie::Mash.new(my_json)
#information = my_hash.item.current.price
Tips:-
if your json is dynamic and it may respond some other structural element so you can maintain exceptional code
#information = my_hash.item.try(:current).try(:price)

How do I add JSON data in the child table against the parent table in Ruby on Rails?

I have been struggling with something in Ruby on Rails.
I have four tables which are interlinked: A, B, C, and D. A is the parent for B and B is the parent for C and D.
I have a records already existing in table B and want to add multiple entries against a particular record, for example "3", in the 'C' and 'D' tables against this id.
The data format is:
[{\"waypoint\":{\"latitude\":37.3645616666667,\"timestamp\":\"2012-10-16T09:58:50Z\",\"background\":false,\"estimated_speed\":17.4189262390137,\"journey_id\":null,\"longitude\":-112.850676666667}},{\"waypoint\":{\"latitude\":37.3648733333333,\"timestamp\":\"2012-10-16T09:58:54Z\",\"background\":false,\"estimated_speed\":17.076057434082,\"journey_id\":null,\"longitude\":-112.85077}},{\"waypoint\":{\"latitude\":37.3651116666667,\"timestamp\":\"2012-10-16T09:58:57Z\",\"background\":false,\"estimated_speed\":15.4269437789917,\"journey_id\":null,\"longitude\":-112.850766666667}},{\"waypoint\":{\"latitude\":37.36547,\"timestamp\":\"2012-10-16T09:59:02Z\",\"background\":false,\"estimated_speed\":17.1007328033447,\"journey_id\":null,\"longitude\":-112.85072}},{\"waypoint\":{\"latitude\":37.3658433333333,\"timestamp\":\"2012-10-16T09:59:11Z\",\"background\":false,\"estimated_speed\":10.3052024841309,\"journey_id\":null,\"longitude\":-112.850738333333}}]"
I get this data from a web service. But I see journey_id as null, whereas I want it to be 3, as I want to make the entry against this id.
How can I save this data in a child table using this id?
Your JSON string isn't opened correctly in your sample, as it's missing the leading '"'. Fixing that and moving on, here's what the JSON looks like "prettified":
[
{
"waypoint": {
"latitude": 37.3645616666667,
"timestamp": "2012-10-16T09:58:50Z",
"background": false,
"estimated_speed": 17.4189262390137,
"journey_id": null,
"longitude": -112.850676666667
}
},
{
"waypoint": {
"latitude": 37.3648733333333,
"timestamp": "2012-10-16T09:58:54Z",
"background": false,
"estimated_speed": 17.076057434082,
"journey_id": null,
"longitude": -112.85077
}
},
{
"waypoint": {
"latitude": 37.3651116666667,
"timestamp": "2012-10-16T09:58:57Z",
"background": false,
"estimated_speed": 15.4269437789917,
"journey_id": null,
"longitude": -112.850766666667
}
},
{
"waypoint": {
"latitude": 37.36547,
"timestamp": "2012-10-16T09:59:02Z",
"background": false,
"estimated_speed": 17.1007328033447,
"journey_id": null,
"longitude": -112.85072
}
},
{
"waypoint": {
"latitude": 37.3658433333333,
"timestamp": "2012-10-16T09:59:11Z",
"background": false,
"estimated_speed": 10.3052024841309,
"journey_id": null,
"longitude": -112.850738333333
}
}
]
You have an array of waypoint objects. Parsing that JSON into a Ruby object:
obj = JSON["[{\"waypoint\":..."] # purposely truncated for brevity
returns an array of hashes:
[{"waypoint"=>
{"latitude"=>37.3645616666667,
"timestamp"=>"2012-10-16T09:58:50Z",
"background"=>false,
"estimated_speed"=>17.4189262390137,
"journey_id"=>nil,
"longitude"=>-112.850676666667}},
{"waypoint"=>
{"latitude"=>37.3648733333333,
"timestamp"=>"2012-10-16T09:58:54Z",
"background"=>false,
"estimated_speed"=>17.076057434082,
"journey_id"=>nil,
"longitude"=>-112.85077}},
{"waypoint"=>
{"latitude"=>37.3651116666667,
"timestamp"=>"2012-10-16T09:58:57Z",
"background"=>false,
"estimated_speed"=>15.4269437789917,
"journey_id"=>nil,
"longitude"=>-112.850766666667}},
{"waypoint"=>
{"latitude"=>37.36547,
"timestamp"=>"2012-10-16T09:59:02Z",
"background"=>false,
"estimated_speed"=>17.1007328033447,
"journey_id"=>nil,
"longitude"=>-112.85072}},
{"waypoint"=>
{"latitude"=>37.3658433333333,
"timestamp"=>"2012-10-16T09:59:11Z",
"background"=>false,
"estimated_speed"=>10.3052024841309,
"journey_id"=>nil,
"longitude"=>-112.850738333333}}]
You can walk through that array and access, or change, the value for journey_id:
row = 3
obj = obj.map{ |h| h['waypoint']['journey_id'] = row }
obj.first
Looking at the first hash shows the value was changed, as were all the rest:
{
"waypoint" => {
"latitude" => 37.3645616666667,
"timestamp" => "2012-10-16T09:58:50Z",
"background" => false,
"estimated_speed" => 17.4189262390137,
"journey_id" => 3,
"longitude" => -112.850676666667
}
}
At that point, you need to recreate the JSON string. You can figure that out by reading the JSON documentation.
You could do all this by modifying the received string directly, but you don't want to get into the habit of directly modifying JSON strings because you can inadvertently damage the payload. It's better to let the parser give you the structure, modify that, then let JSON recreate the string.
How you store it to your database is left as an exercise for you also.

Resources