Class: NotionRails::Service

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

Instance Method Summary collapse

Constructor Details

#initializeService

Returns a new instance of Service.



5
6
7
# File 'lib/notion_rails/service.rb', line 5

def initialize
  @client = Notion::Client.new(token: NotionRails.config.notion_api_token)
end

Instance Method Details

#default_query(tag: nil, slug: nil) ⇒ Object



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
# File 'lib/notion_rails/service.rb', line 9

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

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

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

  query
end

#default_sortingObject



40
41
42
43
44
45
# File 'lib/notion_rails/service.rb', line 40

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

#get_article(id) ⇒ Object



53
54
55
56
57
# File 'lib/notion_rails/service.rb', line 53

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

#get_articles(tag: nil, slug: nil, page_size: 10) ⇒ Object



47
48
49
50
51
# File 'lib/notion_rails/service.rb', line 47

def get_articles(tag: nil, slug: nil, page_size: 10)
  __get_articles(tag: tag, slug: slug, page_size: page_size)['results'].map do |page|
    NotionRails::BasePage.new(page)
  end
end

#get_blocks(id) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/notion_rails/service.rb', line 59

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_image?(block)
    base_block = NotionRails::BaseBlock.new(block)
    base_block.children = get_blocks(base_block.id) if base_block.has_children
    # Notion returns same list items as different blocks so we have to do some processing to have them be related
    # TODO: Separate this into a function, add support for bulleted items.
    #       Currently bulleted items render fine, but they do it in separate ul blocks
    #       Make them appear in the same ul block as numbered_items appear in the same ol block
    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

#refresh_image?(data) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
# File 'lib/notion_rails/service.rb', line 90

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