I have the following if else statement in a lib method:
begin
url = URI.parse("url"+ product )
@response = JSON.parse(Net::HTTP.get_response(url).body)
url2 = URI.parse("url" + product)
@response1 = JSON.parse(Net::HTTP.get_response(url2).body)
if @response != nil
@url = @response["product"]["url"]
image_url1 = @response["product"]["image_url"]
@title = @response["product"]["title"]
else
@url = @response1["ProductUrl"]
@image_url = @response1["ImagePath"]
@title = @response1["ProductName"]
end
rescue
next
end
Currently this method is only checking url and @response, and is simply skipping if @response = nil. How do I check if @response is nil, and if so, use url2 and @response1 and save the variables returned from that response. If @response is not nil, save the variables returned from @respoonse.
@response and @response1 are different APIs
I think this is what you want. The key is that to check for nil, use @response.nil? Another problem is see is that you get the same url if it fails the first time – not sure why you would ask for the same url again and treat it differently if it failed the first time.