如何在Rails中验证API响应状态

时间:2019-06-04 11:41:21

标签: ruby-on-rails api httparty

我正在使用HTTParty gem在我的Rails应用程序中发出请求。

  1. 发出POST请求以生成报告
  2. 发出GET请求以检查报告状态
  3. 报告状态完成后下载报告

我需要进行上述GET请求,直到报告状态完成。报告状态将需要一些时间来更新状态。

如何有效检查报告状态?

def get_report(id)
// making requests
end

report = get_report(3)
report['status']

1 个答案:

答案 0 :(得分:0)

您应该使用ActiveJob进行这种处理。用法如下。

 class ReportingGenerationJob < ActiveJob::Base
  queue_as :default

  after_perform do |job|
    ReportGenrator.notify_ready_to_download(job.arguments.first)
  end

  def perform(report_id)
    ReportGenerator.generate(report_id) #Your ReportGenerator has to expose api for get the current status
  end
end
相关问题