Class: Airtable::Linker::Generator

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

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object



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
# File 'lib/jekyll-airtable-import/linker.rb', line 79

def generate(site)
  return unless site.config['airtable']
  return unless ENV['AIRTABLE_API_KEY']
  # return if site.config["airtable_no_links"]
  @site = site
  @conf = site.config['airtable']
  @conf.each do |name, conf|
    Jekyll.logger.debug "Airtable:", "Linking #{name} up"
    data = site.data[name]
    collection = site.collections[name]
    if data
      site.data[name] = data.map do |rec|
        if rec.is_a?(Jekyll::Document)
          link_record(rec.data)
        else
          link_record(rec)
        end
      end
    end
    if collection
      collection.docs.each do |doc|
        linked = link_record(doc.data)
        # puts linked
        doc.merge_data!(linked)
      end
    end
  end
end

#is_lookup(field) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/jekyll-airtable-import/linker.rb', line 8

def is_lookup(field)
  if field.is_a?(Array) and field.length > 0
    field.each do |item|
      return false unless item[0,3] == "rec"
    end
    return true
  end
  return false
end


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
# File 'lib/jekyll-airtable-import/linker.rb', line 51

def link_record(record)
  # if record.is_a?(Jekyll::Document)
  #   data = record.data
  # else
  #   data = record
  # end
  record.map do |key, val|
    next unless is_lookup(val)
    Jekyll.logger.debug "Airtable:", "Linker: trying to find match for this id: #{val[0]}, labeled as #{key}"
    match = lookup_airtable_id(val[0])
    next unless match
    Jekyll.logger.debug "Airtable:",  "Linker: matched #{key} with #{match['name']}"
    # puts match.to_yaml
    # new_field = []
    if match['type'] == 'data'
      matched_records = match['data'].select {|i| val.include?(i['airtable_id'])}
    end
    if match['type'] == 'collection'
      matched_records =  match['collection'].docs.select {|i| val.include?(i.data['airtable_id']) }
    end
    # puts matched_records.to_yaml
    # new_field
    record[key] = matched_records
  end
  # puts record.to_yaml
  record
end

#lookup_airtable_id(id) ⇒ Object



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
# File 'lib/jekyll-airtable-import/linker.rb', line 18

def lookup_airtable_id(id)
  # lookup = nil
  @conf.each do |name, conf|
    col = @site.collections[name] || nil
    data = @site.data[name] || nil
    if conf['collection'] and col
      matched_record = col.docs.select {|i| i.data['airtable_id'] == id}
      if matched_record.length > 0
        lookup = {
          'type' => 'collection',
          'record' => matched_record,
          'name' => name,
          'collection' => col
        }
        return lookup
      end
    end
    if data and not conf['collection']
      matched_record = data.select {|i| i['airtable_id'] == id}
      if matched_record.length > 0
        lookup = {
          'type' => 'data',
          'record' => matched_record,
          'name' => name,
          'data' => data
        }
        return lookup
      end
    end
  end
  return nil
end