Json 使用HttpParty rubygem发出api请求时如何处理分页

Json 使用HttpParty rubygem发出api请求时如何处理分页,json,ruby,get,jekyll,httparty,Json,Ruby,Get,Jekyll,Httparty,我正试图为我的Jekyll站点编写一个插件,该插件将一个排序的JSON响应写入数据文件,但在处理响应中的项目限制时遇到了问题。基本响应如下所示: { "current_page": 1, "per_page": 50, "total_pages": 19, "url": "https://web.consonance.app/api/v2/products.json", "products": [ { ... "pub_date": "2017

我正试图为我的Jekyll站点编写一个插件,该插件将一个排序的JSON响应写入数据文件,但在处理响应中的项目限制时遇到了问题。基本响应如下所示:

{
  "current_page": 1,
  "per_page": 50,
  "total_pages": 19,
  "url": "https://web.consonance.app/api/v2/products.json",
  "products": [
    {
      ...
      "pub_date": "2017-05-01"
      ...
    },
    ...
  ]
}
require 'rubygems'
require 'httparty'
require 'json'

# http request to api

url = 'https://web.consonance.app/api/v2/products.json'
query = {
}
headers = {
  Authorization: "Token token=**************"
}
response = HTTParty.get(url, query: query, headers: headers)

# convert response to hash array

hash = JSON.parse(response.body)

# reverse sort product in products by date published

hash_sorted = hash['products'].sort_by! { |o| o['pub_date'] }.reverse!

# open _data file and write json response

File.open("./_data/products.json", "w") { |file| 
  file.puts JSON.pretty_generate(hash_sorted)
}
其中,
products
是一组我想按出版日期排序的书籍(
pub\u date
)。没有
下一个页面
url,只有
当前页面
总页面
。我的插件非常基本。它使用
httparty
发出API请求,然后根据
pub\u date
将响应作为哈希进行排序。到目前为止,看起来是这样的:

{
  "current_page": 1,
  "per_page": 50,
  "total_pages": 19,
  "url": "https://web.consonance.app/api/v2/products.json",
  "products": [
    {
      ...
      "pub_date": "2017-05-01"
      ...
    },
    ...
  ]
}
require 'rubygems'
require 'httparty'
require 'json'

# http request to api

url = 'https://web.consonance.app/api/v2/products.json'
query = {
}
headers = {
  Authorization: "Token token=**************"
}
response = HTTParty.get(url, query: query, headers: headers)

# convert response to hash array

hash = JSON.parse(response.body)

# reverse sort product in products by date published

hash_sorted = hash['products'].sort_by! { |o| o['pub_date'] }.reverse!

# open _data file and write json response

File.open("./_data/products.json", "w") { |file| 
  file.puts JSON.pretty_generate(hash_sorted)
}
这给了我一个JSON响应,它按照
pub_date
对前50项进行反向排序。我的问题是:

如何从整个数组中获取最近出版的书籍(
产品
),而不仅仅是前50个结果

我已经尝试将每页的限制提高到最大值,即500项,但书籍的总数量超过一页,因此我仍然存在分页问题

记录了请求的可用参数


注意:我使用的是
httparty
,因为我发现提出请求很容易,但我愿意使用其他gem/方法。我刚刚开始学习Ruby,请耐心听我说。

使用
当前页面
总页面
作为条件,我在响应页面中循环,并将所有产品分配到一个数组:

while current_page <= total_pages do

  url = 'https://web.consonance.app/api/v2/products.json'
  query = {
    'page' => "#{current_page}",
  }
  request = HTTParty.get(url, query: query)

  hash = JSON.parse(request.body)

  hash['products'].each do |item|
    product_array.push(item)
  end

  current_page += 1

end
当前页面“#{current_page}”时,
}
request=HTTParty.get(url,查询:query)
hash=JSON.parse(request.body)
散列['products')。每个do |项|
产品阵列推送(项目)
结束
当前页面+=1
结束

分页算法非常简单。。。如果结果长度等于限制,则转到下一页。在您的例子中,它只是一个循环,您可以在其中增加'current_page'参数^aka
“https://demo.consonance.app/api/v2/products.json?page=2“
您还可以提供一个
页面大小
,该页面允许您增加最大退货量。遍历页面的速度更快,虽然50可能是最大值。500是最大值,但仍然需要处理额外的页面。我将把它作为一个查询参数,但如果它将加快请求的速度。