0

Here is my method:

def get_video_duration
  @time ||= Panda.get("/videos/#{@video.panda_id}/metadata.json")["duration"]
  format_duration
end

I need to write this method "better" in wrapping it with a begin, rescue block so that @time could be nil depending on the response from the API.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189

2 Answers2

2

Yes, possible using inline rescue clause.

def get_video_duration
    @time ||= Panda.get("/videos/#{@video.panda_id}/metadata.json")["duration"] rescue nil
    format_duration
end

Or better explicitly do it.

def get_video_duration
  @time ||= Panda.get("/videos/#{@video.panda_id}/metadata.json")["duration"]
rescue YourException
  @time = nil
  format_duration
end
Roman Kiselenko
  • 43,210
  • 9
  • 91
  • 103
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
1

Maybe break it down with an additional method :

def fetch_video_duration

  Panda.get("/videos/#{@video.panda_id}/metadata.json")["duration"]

  rescue
    return nil
end

def get_video_duration
  @time ||= fetch_video_duration

  format_duration
end
SciPhi
  • 2,585
  • 1
  • 18
  • 19