Class: Jekyll::AirtableFetcher

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll/airtable_fetcher.rb

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/jekyll/airtable_fetcher.rb', line 12

def generate(site)
  jekyll_config = Hashie::Mash.new(site.config) 
  return false if should_generate_be_prevented?(jekyll_config)
  # For storing hashes of attachments that will be saved to the data file
  @attachments_hash = {}
  setup_directories

  client = setting_up_airtable_client

  @table_names.each do |table_name|
    records = client.list_records(table_name: table_name)
    next if records.size == 0
    
    if records.class.name == 'Hash'
      error = records["error"]
      if error.present?
        puts error['message']
        next
      end
    end

    directory_name = "collections/_" + to_snake(table_name)
    Dir.mkdir(directory_name) unless File.exists?(directory_name)

    records.each do |record|
      fields    = record['fields']
      uid       = record['id']
      out_file  = create_page_for_the_record(directory_name, uid, fields)

      fields.each do |key, value|
        snake_key = to_snake(key)

        if is_a_long_text?(jekyll_config, table_name, key)
          write_long_text_to_file(snake_key, value, out_file)
          next
        end

        if value.class.name == 'Array'
          out_file.puts("#{snake_key}:")
          write_array_values(out_file, value)
        else
          value = stringify_value_if_necessary(value)

          out_file.puts("#{snake_key}: #{value}")
        end
      end

      out_file.puts(front_matter_mark)
      
      out_file.close               
    end
  end

  write_attachments_data_file if @attachments_hash.keys.size > 0
end