ZAICO APIで在庫データが1000件を超える場合、1000件ごとに分割してデータを返します。
1001件目以降の在庫データはページネーションにて取得可能です。
また、ページネーション機能では下記のHTTPヘッダを参照することができます。
- Linkヘッダ
現在のページ、前のページ、次のページ、最後のページを返します。
- Total-Count
全在庫データの件数を返します。
サンプルプログラム
require 'net/http' require 'rest-client' require 'json' # APIの設定 $zaicoapi_token = "YOUR_TOKEN_HERE" $zaicoapi_url = "https://web.zaico.co.jp/api/v1/inventories" # 在庫データ一覧取得 def get_inventories() # ページ数の取得 pages = parse_link_header($zaicoapi_url, headers={'Authorization' => 'Bearer ' + $zaicoapi_token}) total_pages = pages[:last] # 全ページ取得 puts "在庫データ一覧" for i in 1..total_pages.to_i pagenation_url = $zaicoapi_url + "?page=#{i}" inventories = RestClient.get(pagenation_url, headers={'Authorization' => 'Bearer ' + $zaicoapi_token}) puts "=================================================================" puts "#{i}ページ目" puts pagenation_url puts "=================================================================" puts "------------------" JSON.parse(inventories.body).each do |inventory| puts "Inventory ID: " + inventory["id"].to_s puts "Title: " + inventory["title"] puts "Quantity: " + inventory["quantity"].to_s puts "------------------" end puts "" end end def parse_link_header(url, headers={}) response = RestClient.get(url, headers) pages = Hash.new parts = response.headers[:link].split(',') # Parse each part into a named link parts.each do |part, index| section = part.split(';') page_number = section[0].match(/page=(\d+).*$/)[1] name = section[1][/rel="(.*)"/,1].to_sym pages[name] = page_number end return pages end if __FILE__ == $0 get_inventories() end
詳細な応答についてはZAICO APIドキュメントをご確認ください。