Class: NotionTemplate

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

Instance Method Summary collapse

Constructor Details

#initialize(client, title, parent_id:, &block) ⇒ NotionTemplate

Returns a new instance of NotionTemplate.



2
3
4
5
6
7
8
9
# File 'lib/notion_template.rb', line 2

def initialize(client, title, parent_id:, &block)
  @client = client
  @title = title
  @blocks = []
  @parent_id = parent_id
  instance_eval(&block)
  send_to_notion
end

Instance Method Details

#bulleted_list(*items) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/notion_template.rb', line 63

def bulleted_list(*items)
  items.each do |item|
    @blocks << {
      object: "block",
      type: "bulleted_list_item",
      bulleted_list_item: {
        rich_text: [{ 
          type: "text", 
          text: { content: item } 
        }]
      }
    }
  end
end

#callout(text, color = "purple") ⇒ Object

Default color set to purple



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/notion_template.rb', line 131

def callout(text, color = "purple") # Default color set to purple
  @blocks << {
    object: "block",
    type: "callout",
    callout: {
      rich_text: [{ 
        type: "text", 
        text: { content: text } 
      }],
      icon: { type: "emoji", emoji: "🔔" }, # Optional icon
      color: "gray_background" # Set the color of the callout
    }
  }
end

#dividerObject



168
169
170
171
172
173
174
# File 'lib/notion_template.rb', line 168

def divider
  @blocks << {
    object: "block",
    type: "divider",
    divider: {}
  }
end

#header(text) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/notion_template.rb', line 11

def header(text)
  @blocks << {
    object: "block",
    type: "heading_1",
    heading_1: {
      rich_text: [{ 
        type: "text", 
        text: { content: text } 
      }]
    }
  }
end

#image(url) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/notion_template.rb', line 120

def image(url)
  @blocks << {
    object: "block",
    type: "image",
    image: {
      type: "external",
      external: { url: url }
    }
  }
end

#numbered_list(*items) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/notion_template.rb', line 78

def numbered_list(*items)
  items.each do |item|
    @blocks << {
      object: "block",
      type: "numbered_list_item",
      numbered_list_item: {
        rich_text: [{ 
          type: "text", 
          text: { content: item } 
        }]
      }
    }
  end
end

#paragraph(text) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/notion_template.rb', line 50

def paragraph(text)
  @blocks << {
    object: "block",
    type: "paragraph",
    paragraph: {
      rich_text: [{ 
        type: "text", 
        text: { content: text } 
      }]
    }
  }
end

#quote(text) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/notion_template.rb', line 107

def quote(text)
  @blocks << {
    object: "block",
    type: "quote",
    quote: {
      rich_text: [{ 
        type: "text", 
        text: { content: text } 
      }]
    }
  }
end

#send_to_notionObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/notion_template.rb', line 184

def send_to_notion
  page_content = {
    parent: { page_id: @parent_id }, # Using the dynamic parent_id
    properties: {
      title: [
        {
          type: "text",
          text: { content: @title }
        }
      ]
    },
    children: @blocks
  }

  @client.create_page(page_content)
end

#subheader(text) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/notion_template.rb', line 24

def subheader(text)
  @blocks << {
    object: "block",
    type: "heading_2",
    heading_2: {
      rich_text: [{ 
        type: "text", 
        text: { content: text } 
      }]
    }
  }
end

#subsubheader(text) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/notion_template.rb', line 37

def subsubheader(text)
  @blocks << {
    object: "block",
    type: "heading_3",
    heading_3: {
      rich_text: [{ 
        type: "text", 
        text: { content: text } 
      }]
    }
  }
end

#table_of_contentsObject



176
177
178
179
180
181
182
# File 'lib/notion_template.rb', line 176

def table_of_contents
  @blocks << {
    object: "block",
    type: "table_of_contents",
    table_of_contents: {}
  }
end

#to_do(text, checked: false) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/notion_template.rb', line 93

def to_do(text, checked: false)
  @blocks << {
    object: "block",
    type: "to_do",
    to_do: {
      rich_text: [{ 
        type: "text", 
        text: { content: text } 
      }],
      checked: checked
    }
  }
end

#toggle(text, &block) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/notion_template.rb', line 146

def toggle(text, &block)
  toggle_block = {
    object: "block",
    type: "toggle",
    toggle: {
      rich_text: [{ 
        type: "text", 
        text: { content: text } 
      }],
      children: []
    }
  }

  if block_given?
    # Capture nested blocks
    instance_eval(&block)
    toggle_block[:toggle][:children] = @blocks
  end
  
  @blocks << toggle_block
end