memo.log

技術情報の雑なメモ

【Ruby】MicroCMSで記事を全件取得するスニペット

# return [Array<OpenStruct>]
def all_contents
  limit = 10
  offset_number = 0
  contents = []

  loop do
    result = MicroCMS.list(
                "[MUST EDIT]",
                {
                  offset: offset_number,
                  limit: limit,
                }
              )

    total_count = result.total_count
    contents << result.contents

    if total_count >= offset_number + 1
      offset_number += limit
    else
      break
    end
  end

  contents.flatten
end