Class: Synotion::Syncer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Syncer

Returns a new instance of Syncer.



7
8
9
10
11
# File 'lib/synotion/syncer.rb', line 7

def initialize(options = {})
  @config = build_config(options)
  @config.validate!
  @client = Client.new(@config.notion_api_key)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/synotion/syncer.rb', line 5

def client
  @client
end

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/synotion/syncer.rb', line 5

def config
  @config
end

Instance Method Details

#sync(markdown_file_path, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
# File 'lib/synotion/syncer.rb', line 13

def sync(markdown_file_path, options = {})
  raise ArgumentError, "File not found: #{markdown_file_path}" unless File.exist?(markdown_file_path)

  markdown_content = File.read(markdown_file_path)
  identifier = options[:unique_value] || markdown_file_path

  sync_content(markdown_content, identifier, options.merge(filename: markdown_file_path))
end

#sync_content(markdown_content, identifier, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/synotion/syncer.rb', line 22

def sync_content(markdown_content, identifier, options = {})
  mode = options[:mode] || config.update_mode
  database_id = options[:database_id] || config.database_id
  page_id = options[:page_id] || config.page_id

  converter = MarkdownConverter.new(markdown_content)
  blocks = converter.to_notion_blocks
  title = options[:title] || extract_title(converter, options[:filename])

  if page_id
    sync_to_page(page_id, blocks, mode, title)
  elsif database_id
    unique_property = options[:unique_property] || config.unique_property
    sync_to_database(database_id, identifier, blocks, mode, title, unique_property, options)
  else
    raise ConfigurationError, 'Either page_id or database_id must be specified'
  end
end