Class: JekyllBaserowHeadlessCMS::Generator

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/jekyll_baserow_headless_cms/generator.rb

Overview

Jekyll Generator that fetches data from Baserow tables

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object



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
41
42
43
# File 'lib/jekyll_baserow_headless_cms/generator.rb', line 12

def generate(site)
  @site = site
  @config = site.config['baserow'] || {}
  @collections_config = @config['collections'] || {}

  unless @config['enabled'] != false
    Jekyll.logger.info 'BaserowCMS:', 'Plugin disabled in configuration'
    return
  end

  unless ENV['BASEROW_TOKEN']
    Jekyll.logger.info 'BaserowCMS:', 'No BASEROW_TOKEN found, using collections fallback'
    use_all_collections_fallback
    return
  end

  Jekyll.logger.info 'BaserowCMS:', 'Fetching data from Baserow API...'

  begin
    @client = BaserowClient.new(ENV.fetch('BASEROW_TOKEN', nil), base_url: ENV.fetch('BASEROW_API_URL', nil))

    @collections_config.each do |collection_name, collection_config|
      fetch_collection_data(collection_name, collection_config)
    end

    Jekyll.logger.info 'BaserowCMS:', 'All data fetched successfully'
  rescue StandardError => e
    Jekyll.logger.error 'BaserowCMS:', "Error fetching data: #{e.message}"
    Jekyll.logger.warn 'BaserowCMS:', 'Falling back to collections'
    use_all_collections_fallback
  end
end