Class: Airtable::Generator
- Inherits:
-
Jekyll::Generator
- Object
- Jekyll::Generator
- Airtable::Generator
- Defined in:
- lib/jekyll-airtable-import.rb
Overview
Generates Jekyll Collections and Data from Airtable bases.
See tippingpointuk.github.io/jekyll-airtable-import for more.
Instance Method Summary collapse
Instance Method Details
#generate(site) ⇒ Object
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 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/jekyll-airtable-import.rb', line 27 def generate(site) return unless site.config['airtable'] # Get API key from environment if ENV['AIRTABLE_API_KEY'] api_key = ENV['AIRTABLE_API_KEY'] else warn "No airtable api key found. Make sure your key is available as AIRTABLE_API_KEY in the local environment." end # Pass in api key to client @client = Airtable::Client.new(api_key) site.config['airtable'].each do |name, conf| # Pass in the app key and table name @table = @client.table(conf['app'], conf['table']) # Get records where the Published field is checked @records = @table.all(:view => conf['view'],:fields => conf['fields']) # Extract data to a hash data = @records.map { |record| record.attributes } parsed_data = parse_data(data) if conf['collection'] slug_field = conf['collection']['slug'] layout = conf['collection']['layout'] if site.collections[name] new_collection = site.collections[name] else new_collection = Jekyll::Collection.new(site, name) end # new_collection = Jekyll::Collection.new(site, name) parsed_data.each do |item| if item[slug_field] and item[slug_field] != '' content = item[conf['collection']['content']] #puts content slug = Jekyll::Utils.slugify(item[slug_field]) path = File.join(site.source, "_#{name}", "#{slug}.md") doc = Jekyll::Document.new(path, collection: new_collection, site: site) item.merge!({ 'layout' => layout, 'slug' => slug }) doc.merge_data!(item.except('id')) doc.content = content new_collection.docs << doc end end site.collections[name] = new_collection else site.data[name] = 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_data(data) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/jekyll-airtable-import.rb', line 12 def parse_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'] item[key] = val[0]['url'] end end end data_parse.push(item) end data_parse end |