Class: BridgetownNotion::NotionPostsFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/bridgetown_notion/notion_posts_factory.rb

Class Method Summary collapse

Class Method Details

.create_posts(config) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bridgetown_notion/notion_posts_factory.rb', line 6

def self.create_posts(config)
  notion_key = config.notion_key
  notion_db_id = config.notion_db_id

  Notion.configure do |notion|
    notion.token = notion_key
  end

  notion_client = Notion::Client.new

  result = []

  notion_client.database_query(database_id: notion_db_id) do |posts_page|
    posts_page.results.each do |post|
      notion_client.block_children(block_id: post.id) do |blocks_page|
        post["blocks"] = [*post["blocks"], *blocks_page.results]
      end

      parsed_post = BridgetownNotion::Parsers::PostParser.parse(post)

      next if parsed_post.published_at.nil? || parsed_post.title.nil?

      result.push({
        categories: parsed_post.categories,
        content: parsed_post.content,
        is_published: parsed_post.is_published,
        slug: parsed_post.slug,
        tags: parsed_post.tags,
        title: parsed_post.title,
      })
    end
  end

  result
end