I have an array of objects I am sending to a REST API to receive back information on those objects. To do this I am using RestClient with the following lines to send the call and parse the response.
response_raw = RestClient.get "http://#{re_host}:#{re_port}/reachengine/api/inventory/search?rql=fCSAssetNumber=#{fcs_id_num}%20size%20#{size}%20&apiKey=#{api_key}", headers
response_json = Crack::JSON.parse(response_raw)
response_json['results'].each do |result|
For the first 20+ records I perform this action on, everything works fine. Then I start to get a NoMethodError: undefined method `[]' for nil:NilClass
When I run the code step by step in IRB, what I see in the results is very strange
result = response_json['results'][0]
>=> {"name"=>"Publicaciòn_Listin_Diario.png", " id"=>" 294290", " dateCreated"=>" 2015-09-20T20:35:06.000+0000", " dateUpdated"=>" 2015-12-23T19:33:13.000+0000", " systemKeywords"=>" Publicaciòn_Listin_Diario.png Image ", "t humbnailId"=>"4 24725", "m etadata"=>{" sourceFilePath"=>"/ Volumes/ONLINE_DAM/MEDIA/RAW_GRAPHICS/1307001_August_2013_KOS/Publicaciòn_Listin_Diario.png", "pa MdCustAgency_picklist_sortable"=>"nul l", "th umbnailAssetFlag"=>"fal se", "re storeKey"=>"nul l", "ar chiveStatus_picklist_sortable"=>"nul l", "fC SAssetNumber"=>"18 2725", "fC SMetadataSet"=>"Ra w Graphic", "cu stKeywords"=>"Do minican Republic Cycling Team, 1307001 August 2013 KOS Kickoff show", "cu stAssetStatus_picklist_sortable"=>"nul l", "se archableFlag"=>"fal se", "as setType"=>"Im age", "pa MdCustHerbalifeJobNumber"=>"13 07001", "da teCreated"=>"20 15-09-20T20:35:06", "da teLocked"=>"nul l", "uu id"=>"30 9d9bb3-6935-4ab6-a04a-ef7264132bc6", "ve rsionFlag"=>"nul l", "ag ency_picklist_sortable"=>"nul l", "pr oducer_picklist_sortable"=>"nul l", "tr uncatedFlag"=>"fal se", "cu stDescription"=>"*R aw Graphics for 1307001_August_2013_KOS"}, "in ventoryKey"=>"im age"}
Usually, with this response, I can run result['metadata']['fCSAssetNumber']
However; because of the random spaces, this fails with a "NoMethodError: undefined method `[]' for nil:NilClass" because instead of the string being 'metadata' it is actually 'm etadata'
Whats really strange about all of this and why this is a Ruby issue and not easily determined to be the API's issue is that the same exact call made via Postman REST Client in chrome returns this result:
>{
"results": [
{
"name": "Publicaciòn_Listin_Diario.png",
"id": "294290",
"dateCreated": "2015-09-20T20:35:06.000+0000",
"dateUpdated": "2015-12-23T19:33:13.000+0000",
"systemKeywords": "Publicaciòn_Listin_Diario.png Image ",
"thumbnailId": "424725",
"metadata": {
"sourceFilePath": "/Volumes/ONLINE_DAM/MEDIA/RAW_GRAPHICS/1307001_August_2013_KOS/Publicaciòn_Listin_Diario.png",
"paMdCustAgency_picklist_sortable": null,
"thumbnailAssetFlag": false,
"restoreKey": null,
"archiveStatus_picklist_sortable": null,
"fCSAssetNumber": "182725",
"fCSMetadataSet": "Raw Graphic",
"custKeywords": "Dominican Republic Cycling Team, 1307001 August 2013 KOS Kickoff show",
"custAssetStatus_picklist_sortable": null,
"searchableFlag": false,
"assetType": "Image",
"paMdCustHerbalifeJobNumber": "1307001",
"dateCreated": "2015-09-20T20:35:06",
"dateLocked": null,
"uuid": "309d9bb3-6935-4ab6-a04a-ef7264132bc6",
"versionFlag": null,
"agency_picklist_sortable": null,
"producer_picklist_sortable": null,
"truncatedFlag": false,
"custDescription": "*Raw Graphics for 1307001_August_2013_KOS"
},
"inventoryKey": "image"
}
],
"total": "1"
}
Ad you can see above, when Postman runs the same exact call there is no issue with the response, but when ruby runs the call there is. Also note that this doesnt happen all of the time.
Below is a sample response from the same exact ruby call that actually worked.
result = response_json['results'][0]
=> {"name"=>"Marco_1er_dia.png", "id"=>"294284", "dateCreated"=>"2015-09-`20T20:34:54.000+0000", "dateUpdated"=>"2015-12-23T19:33:10.000+0000", "systemKeywords"=>"Marco_1er_dia.png Image ", "thumbnailId"=>"424716", "metadata"=>{"sourceFilePath"=>"/Volumes/ONLINE_DAM/MEDIA/RAW_GRAPHICS/1307001_August_2013_KOS/Marco_1er_dia.png", "paMdCustAgency_picklist_sortable"=>nil, "collectionMemberships"=>"320 321", "thumbnailAssetFlag"=>false, "restoreKey"=>nil, "fCSMetadataSet"=>"Raw Graphic", "fCSAssetNumber"=>"182722", "archiveStatus_picklist_sortable"=>nil, "custAssetStatus_picklist_sortable"=>nil, "custKeywords"=>"1307001 August 2013 KOS Kickoff show, Dominican Republic Cycling Team", "searchableFlag"=>false, "assetType"=>"Image", "paMdCustHerbalifeJobNumber"=>"1307001", "dateCreated"=>"2015-09-20T20:34:54", "dateLocked"=>nil, "uuid"=>"b5e55c14-b94e-4629-9e2a-61a2dc0876f6", "versionFlag"=>nil, "fCSProductionStatus_picklist_sortable"=>nil, "agency_picklist_sortable"=>nil, "producer_picklist_sortable"=>nil, "truncatedFlag"=>false, "custDescription"=>"*Raw Graphics for 1307001_August_2013_KOS"}, "inventoryKey"=>"image"}`
Notice how the response above has no spacing issue? The only glaring difference I can see here is that there is a special character in use in the filename: ò Is there something specific I need to do for RESTClient to work with this?
Anyone have any idea how this can be fixed?
The issue was related to the 'crack' gem. I've used this for a long time. Apparently, as of Ruby 1.9, the parse method has been available on the standard JSON class. When I switched to using this by changing
response_json = Crack::JSON.parse(response_raw)
to
response_json = JSON.parse(response_raw)
The issue went away.