Class: Airtable::Generator

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/jekyll-airtable-import.rb

Overview

Generates Jekyll::Collection s and Data from Airtable bases.

See tippingpointuk.github.io/jekyll-airtable-import for more.

Instance Method Summary collapse

Instance Method Details

#download_attachment(at_attachment) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jekyll-airtable-import.rb', line 15

def download_attachment(at_attachment)
  ext = ".#{at_attachment['type'].split('/')[-1]}" if at_attachment['type'].is_a? String
  ext ||= ''
  file_name = "#{at_attachment['filename']}-#{at_attachment['id']}#{ext}"
  
  Dir.mkdir "#{Dir.pwd}/assets" unless Dir.exists? "#{Dir.pwd}/assets"
  assets_dir = Dir.mkdir "#{Dir.pwd}/assets/airtable" unless Dir.exists? "#{Dir.pwd}/assets/airtable"
  new_path = "#{Dir.pwd}/assets/airtable/#{file_name}"
  return "/assets/airtable/#{file_name}" if File.exists? new_path

  attachment = URI.open(at_attachment['url'])
  IO.copy_stream(attachment, new_path)
  new_file = Jekyll::StaticFile.new(@site, @site.source, '/assets/airtable/', file_name)
  @site.static_files << new_file

  new_file.url
end

#generate(site) ⇒ Object



58
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/jekyll-airtable-import.rb', line 58

def generate(site)
  @site = site
  @log_name = "Airtable:"
  return unless site.config['airtable']

  @site = site
  # Get API key from environment
  if ENV['AIRTABLE_API_KEY']
    api_key = ENV['AIRTABLE_API_KEY']
  else
    Jekyll.logger.warn @log_name, "No API key found. Make sure your key is available as AIRTABLE_API_KEY in the local environment => Ruby ENV hash."
    return
  end
  # Pass in api key to client
  @client = Airtable::Client.new(api_key)
  @app_id = nil
  @table_id = nil
  @view_id = nil
  site.config['airtable'].each do |name, conf|
    conf ||= Hash.new
    if conf['app']
      # Only update app if conf does
      @app_id = conf['app'][0..3] == 'ENV_' ? ENV[conf['app'][4..-1]] : conf['app']
    end
    unless @app_id
      Jekyll.logger.warn @log_name, "No app ID for Airtable import of #{name}"
      next
    end
    if conf['table']
      @table_id = conf['table'][0..3] == 'ENV_' ? ENV[conf['table'][4..-1]] : conf['table']
    end
    unless @table_id
      Jekyll.logger.warn @log_name, "No table ID for Airtable import of #{name}"
      next
    end
    if conf['view']
      @view_id = conf['view'][0..3] == 'ENV_' ? ENV[conf['view'][4..-1]] : conf['view']
    end
    Jekyll.logger.debug @log_name, "Importing #{name} from https://airtable.com/#{@app_id}/#{@table_id}/#{conf['view']}"
    # Pass in the app key and table name
    @table = @client.table(@app_id, @table_id)
    # Get records where the Published field is checked
    @records = @table.all(:view => @view_id,:fields => conf['fields'])
    Jekyll.logger.debug @log_name, "Found #{@records.length} records to import for #{name}"
    # Extract data to a hash
    data = @records.map { |record| record.attributes }
    # puts data
    parsed_data = parse_airtable_data(data)
    if conf['collection']
      if conf['collection'].is_a?(Hash)
        collection_conf = conf['collection']
      end
      collection_conf ||= Hash.new
      content_field = collection_conf['content'] || 'content'
      slug_fields = [collection_conf['slug'] || "slug", "slug", "title","name"]
      layout = collection_conf['layout'] || name.singularize
      if site.collections[name]
        new_collection = site.collections[name]
      else
        new_collection = Jekyll::Collection.new(site, name)
      end
      parsed_data.each_with_index do |item, index|
        content = item[conf['collection']['content'] || 'content'] if conf['collection'].is_a?(Hash)
        #puts content
        for slug_field in slug_fields
          if item[slug_field] and !item[slug_field].is_a?(Array)
            slug = Jekyll::Utils.slugify(item[slug_field])
            break
          end
        end
        slug ||= Jekyll::Utils.slugify("#{name}#{index}")
        path = File.join(site.source, "_#{name}", "#{slug}.md")
        doc = Jekyll::Document.new(path, collection: new_collection, site: site)
        item.merge!({
          'layout' => layout,
          'slug' => slug,
          'airtable_id' => item['id']
        })
        doc.merge_data!(item.except('id'))

        doc.content = content
        new_collection.docs << doc
      end
      site.collections[name] = new_collection
    else
      site.data[name] = data
      ## Combine existing collections (of the same name) into the imported data
      if conf['combine'] and site.collections.key?(name)

        site.collections[name].docs.each do |document|
          site.data[name].append(document)
        end
      end
    end
  end
end

#parse_airtable_data(data) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jekyll-airtable-import.rb', line 33

def parse_airtable_data(data)
  data_parse = []
  data.each do |item|
    # Extract attachments to just their URL
    item.each do |key,val|
      if val.kind_of?(Array)
        if val[0]['url']
          if val.length == 1
            item[key] = download_attachment(val[0])
          else
            item[key] = []
            val.each do | asset |
              item[key] << download_attachment(asset)
            end
          end
        end
      end
    end
    item['airtable_id'] = item['id']
    data_parse.push(item)
  end

  data_parse
end