我正在构建一个平台,用于显示来自Zendesk API的图表中的数据。我遇到了麻烦,因为一次只能拨打100条记录。如何从此资源中提取多页记录?
以下是我用来拨打电话的代码:
require 'net/http'
require 'uri'
require 'json'
#imports User data from the zendesk api and populates the database with it.
uri = URI.parse("https://samplesupport.zendesk.com/api/v2/users.json")
request = Net::HTTP::Get.new(uri)
request.content_type = "application/json"
request.basic_auth("sampleguy@sample.com", "samplepass")
req_options = {
use_ssl: uri.scheme == "https",
}
@response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
puts @response.body
puts @response.message
puts @response.code
这适用于调用一个页面'资源...任何使用我的脚本抓取多个页面的帮助将不胜感激。谢谢!
答案 0 :(得分:1)
根据ZenDesk's documentation,他们会在有效负载中返回next_page
个属性。因此,您应该检查它的存在,然后再次查询它是否存在。根据需要重复。
require 'json'
# setup to query for the first page
results = JSON.parse(@response.body)
users = results['users'] #to get the users
if results['next_page']
# Do another query to results['next_page'] URL and add to users list