0

I 'm trying to build a Rails API client. There is an api where I can receive my data as json, which works great so far.

Now I am trying to do some timeout handling but I don't know how. I mean literally. How should I even use timeout handling?

I saw something in a tutorial which I translated for my used gem "net/http" but I cannot imagine that this has even any effect.

Here is my controller code:

require 'net/http'

class OverviewController < ApplicationController
  def api_key
    ENV["API_KEY"]
  end

  def handle_timeouts
    begin
      yield
    rescue Net::OpenTimeout, Net::ReadTimeout
      {}
    end
  end


  def index
    handle_timeouts do
      url = "https://example.com/api/#{ api_key }"
      uri = URI(url)
      response = Net::HTTP.get(uri) 
      @url_debug = url
      @my_hash = response
    end
  end
end
tadman
  • 208,517
  • 23
  • 234
  • 262
mrdmr
  • 13
  • 4
  • 1
    If you're doing a lot of work with JSON-type APIs you'll want to look at [Faraday](https://github.com/lostisland/faraday) and avoid Net::HTTP. The Net::HTTP library is provided as a really basic, bare-bones, no fuss interface that's intended to be used as a last resort. – tadman Aug 15 '17 at 23:01
  • Thanks @tadman. I looked at faraday and changed my api call to faraday. Works great. Now I am struggling with the part about saving the data locally. But thank you! – mrdmr Aug 29 '17 at 16:10
  • Good to hear. If you've got a new question, see if you can post it. – tadman Aug 29 '17 at 16:11

0 Answers0