I am running a http request by using the open-uri method. The following error message is intermittently getting rather than the expected output.
An error of type NoMethodError happened, message is undefined method `[]' for nil:NilClass
er ?
And my code is given below,
request_uri = "my url here"
request_query = ''
url = "#{request_uri}#{request_query}"
begin
buffer = open(request_uri,
'app-key' => ENV['API_KEY'],
'app-token' => ENV['API_TOKEN'],
'trail-Token' => ENV['TRAIL_TOKEN']).read
parsed = JSON.parse(buffer)
events = parsed['events']
event = Array(events).last
message = event['message']
otp = message[-5,4]
print "otp", otp
rescue Exception => ex
puts "An error of type #{ex.class} happened, message is #{ex.message}"
retry
end
puts "otp returned", otp
Since I am new to ruby don't know why the response is getting like this. Once I get the response properly the next time when I run the script er ? is the response. Someone please give a helping hand to fix the issue.