Parsing the data inside a ruby collection object

193 Views Asked by At

i am getting a response in the form of an object (#<Restforce::Collection:0x000000000362d0d8>), when i hit a REST API. How can i format the response to say JSON, to parse it?

Below is the result, if i do a p (to inspect) on the variable

<Restforce::Collection:0x0000000003427180 @client=#<Restforce::Data::Client @options={:api_version=>"38.0", :username=>"someusername", :password=>"aPassword", :security_token=>nil, :client_id=>"3MVG99E3Ry5mM6xW8sB_otst", :client_secret=>"3831247029", :host=>"test.salesforce.com", :oauth_token=>"00D0n000C7KUwYNXFiEaUfAuC_HNgIJgb4AlztyNTphw44U28ab", :refresh_token=>nil, :instance_url=>"https://adc.d67.my.salesforce.com", :cache=>nil, :authentication_retries=>3, :compress=>nil, :mashify=>nil, :timeout=>nil, :adapter=>:net_http, :proxy_uri=>nil, :authentication_callback=>nil, :ssl=>{}, :request_headers=>nil, :logger=>#<Logger:0x000000000341fae8 @level=0, @progname=nil, @default_formatter=#<Logger::Formatter:0x000000000341fa98 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x000000000341f9f8 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x000000000341f980>>>, :log_level=>:debug}>, @raw_page={"totalSize"=>25, "done"=>true, "records"=>[{"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyOGAQ"}, "Id"=>"0AT0n0000005cyOGAQ", "EventType"=>"ApexCallout", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyPGAQ"}, "Id"=>"0AT0n0000005cyPGAQ", "EventType"=>"ApexExecution", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyQGAQ"}, "Id"=>"0AT0n0000005cyQGAQ", "EventType"=>"ApexTrigger", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyRGAQ"}, "Id"=>"0AT0n0000005cyRGAQ", "EventType"=>"API", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cySGAQ"}, "Id"=>"0AT0n0000005cySGAQ", "EventType"=>"AsyncReportRun", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyTGAQ"}, "Id"=>"0AT0n0000005cyTGAQ", "EventType"=>"ChangeSetOperation", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyUGAQ"}, "Id"=>"0AT0n0000005cyUGAQ", "EventType"=>"ContentDocumentLink", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyVGAQ"}, "Id"=>"0AT0n0000005cyVGAQ", "EventType"=>"ContentTransfer", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyWGAQ"}, "Id"=>"0AT0n0000005cyWGAQ", "EventType"=>"Dashboard", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyXGAQ"}, "Id"=>"0AT0n0000005cyXGAQ", "EventType"=>"DocumentAttachmentDownloads", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyYGAQ"}, "Id"=>"0AT0n0000005cyYGAQ", "EventType"=>"LightningError", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyZGAQ"}, "Id"=>"0AT0n0000005cyZGAQ", "EventType"=>"LightningInteraction", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyaGAA"}, "Id"=>"0AT0n0000005cyaGAA", "EventType"=>"LightningPageView", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cybGAA"}, "Id"=>"0AT0n0000005cybGAA", "EventType"=>"LightningPerformance", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cycGAA"}, "Id"=>"0AT0n0000005cycGAA", "EventType"=>"LoginAs", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cydGAA"}, "Id"=>"0AT0n0000005cydGAA", "EventType"=>"Login", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyeGAA"}, "Id"=>"0AT0n0000005cyeGAA", "EventType"=>"Logout", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyfGAA"}, "Id"=>"0AT0n0000005cyfGAA", "EventType"=>"MetadataApiOperation", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cygGAA"}, "Id"=>"0AT0n0000005cygGAA", "EventType"=>"QueuedExecution", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyhGAA"}, "Id"=>"0AT0n0000005cyhGAA", "EventType"=>"Report", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyiGAA"}, "Id"=>"0AT0n0000005cyiGAA", "EventType"=>"RestApi", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyjGAA"}, "Id"=>"0AT0n0000005cyjGAA", "EventType"=>"SearchClick", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cykGAA"}, "Id"=>"0AT0n0000005cykGAA", "EventType"=>"Search", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cylGAA"}, "Id"=>"0AT0n0000005cylGAA", "EventType"=>"URI", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cymGAA"}, "Id"=>"0AT0n0000005cymGAA", "EventType"=>"VisualforceRequest", "LogDate"=>"2017-10-12T00:00:00.000+0000"}]}>

UPDATE:

the returned collection have this structure of nested hash and the data that i am looking for is in @raw_page variable. How i can i extract this values to loop through them?

#<Restforce::Collection:0x0000000003698798 @client=#<Restforce::Data::Client @options={:api_version=>"38.0", :username=>"sabc", :password=>"passws", :security_token=>nil,:log_level=>:debug}>,@raw_page={"totalSize"=>25, "done"=>true, "records"=>[{"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyOGAQ"}, "Id"=>"0AT0n0000005cyOGAQ", "EventType"=>"ApexCallout", "LogDate"=>"2017-10-12T00:00:00.000+0000"}, {"attributes"=>{"type"=>"EventLogFile", "url"=>"/services/data/v38.0/sobjects/EventLogFile/0AT0n0000005cyPGAQ"}, "Id"=>"0AT0n0000005cyPGAQ", "EventType"=>"ApexExecution", "LogDate"=>"2017-10-12T00:00:00.000+0000"}]}>
1

There are 1 best solutions below

1
Sergio Tulentsev On

Restforce already did all the parsing for you. Just use the prepared objects. Example from the gem's README:

accounts = client.query("select Id, Something__c from Account where Id = 'someid'")
# => #<Restforce::Collection >

account = accounts.first
# => #<Restforce::SObject >

account.sobject_type
# => 'Account'

account.Id
# => "someid"