Class: NotionToHtml::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/notion_to_html/service.rb

Class Method Summary collapse

Class Method Details

.default_query(name: nil, description: nil, tag: nil, slug: nil) ⇒ Array<Hash>

Generates the default query for fetching pages

Parameters:

  • name (String, nil) (defaults to: nil)

    The name to filter pages by, or nil to not filter

  • description (String, nil) (defaults to: nil)

    The description to filter pages by, or nil not filter

  • tag (String, nil) (defaults to: nil)

    The tag to filter pages by, or nil to include all tags

  • slug (String, nil) (defaults to: nil)

    The slug to filter pages by, or nil to include all slugs

Returns:

  • (Array<Hash>)

    The default query to be used in the database query



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/notion_to_html/service.rb', line 18

def default_query(name: nil, description: nil, tag: nil, slug: nil)
  query = [
    {
      property: 'public',
      checkbox: {
        equals: true
      }
    }
  ]
  or_query = []

  if tag
    query.push({
      property: 'tags',
      multi_select: {
        contains: tag
      }
    })
  end

  if slug
    or_query.push({
      property: 'slug',
      rich_text: {
        equals: slug
      }
    })
  end

  if name
    or_query.push({
      property: 'name',
      title: {
        contains: name
      }
    })
  end

  if description
    or_query.push({
      property: 'description',
      rich_text: {
        contains: description
      }
    })
  end

  query.push('or': or_query) unless or_query.blank?
  query
end

.default_sortingHash

Provides the default sorting order for fetching pages

Returns:

  • (Hash)

    The default sorting criteria for database queries



71
72
73
74
75
76
# File 'lib/notion_to_html/service.rb', line 71

def default_sorting
  {
    property: 'published',
    direction: 'descending'
  }
end

.get_blocks(id) ⇒ Array<NotionToHtml::BaseBlock>

Fetches blocks associated with a given page ID

Parameters:

  • id (String)

    The ID of the page whose blocks are to be fetched

Returns:



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/notion_to_html/service.rb', line 109

def get_blocks(id)
  blocks = __get_blocks(id)
  parent_list_block_index = nil
  results = []
  blocks['results'].each_with_index do |block, index|
    block = refresh_block(block['id']) if refresh_block?(block)
    base_block = NotionToHtml::BaseBlock.new(block)
    base_block.children = get_blocks(base_block.id) if base_block.has_children
    if %w[numbered_list_item].include? base_block.type
      siblings = !parent_list_block_index.nil? &&
                 index != parent_list_block_index &&
                 base_block.type == results[parent_list_block_index]&.type &&
                 base_block.parent == results[parent_list_block_index]&.parent
      if siblings
        results[parent_list_block_index].siblings << base_block
        next
      else
        parent_list_block_index = results.length
      end
    else
      parent_list_block_index = nil
    end
    results << base_block
  end
  results
end

.get_page(id) ⇒ NotionToHtml::Page

Fetches a single page by its ID

Parameters:

  • id (String)

    The ID of the page to fetch

Returns:



100
101
102
103
104
# File 'lib/notion_to_html/service.rb', line 100

def get_page(id)
  base_page = NotionToHtml::BasePage.new(__get_page(id))
  base_blocks = get_blocks(id)
  NotionToHtml::Page.new(base_page, base_blocks)
end

.get_pages(name: nil, description: nil, tag: nil, slug: nil, page_size: 10) ⇒ Array<NotionToHtml::BasePage>

Fetches a list of pages from Notion based on provided filters

Parameters:

  • name (String, nil) (defaults to: nil)

    The name to filter pages by, or nil to not filter

  • description (String, nil) (defaults to: nil)

    The description to filter pages by, or nil not filter

  • tag (String, nil) (defaults to: nil)

    The tag to filter pages by, or nil to include all tags

  • slug (String, nil) (defaults to: nil)

    The slug to filter pages by, or nil to include all slugs

  • page_size (Integer) (defaults to: 10)

    The number of pages to fetch per page

Returns:



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/notion_to_html/service.rb', line 85

def get_pages(name: nil, description: nil, tag: nil, slug: nil, page_size: 10)
  __get_pages(
    name: name,
    description: description,
    tag: tag,
    slug: slug,
    page_size: page_size
  )['results'].map do |page|
    NotionToHtml::BasePage.new(page)
  end
end

.refresh_block?(block) ⇒ Boolean

Determines if a block needs to be refreshed based on its type and expiry time

Parameters:

  • block (Hash)

    The block data to check

Returns:

  • (Boolean)

    True if the block needs to be refreshed, false otherwise



139
140
141
# File 'lib/notion_to_html/service.rb', line 139

def refresh_block?(block)
  refresh_image?(block) || refresh_video?(block)
end

.refresh_image?(data) ⇒ Boolean

Determines if an image block needs to be refreshed based on its expiry time

Parameters:

  • data (Hash)

    The data of the image block

Returns:

  • (Boolean)

    True if the image needs to be refreshed, false otherwise



146
147
148
149
150
151
152
# File 'lib/notion_to_html/service.rb', line 146

def refresh_image?(data)
  return false unless data['type'] == 'image'
  return false unless data.dig('image', 'type') == 'file'

  expiry_time = data.dig('image', 'file', 'expiry_time')
  expiry_time.to_datetime.past?
end

.refresh_video?(data) ⇒ Boolean

Determines if a video block needs to be refreshed based on its expiry time

Parameters:

  • data (Hash)

    The data of the video block

Returns:

  • (Boolean)

    True if the video needs to be refreshed, false otherwise



157
158
159
160
161
162
163
# File 'lib/notion_to_html/service.rb', line 157

def refresh_video?(data)
  return false unless data['type'] == 'video'
  return false unless data.dig('video', 'type') == 'file'

  expiry_time = data.dig('video', 'file', 'expiry_time')
  expiry_time.to_datetime.past?
end