jekyll-airtable 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 395f32572de790075218a8054c2f2d85edfb7d26
4
- data.tar.gz: f991aff018e93d7c909fbfc8afb22555a41f4604
3
+ metadata.gz: b5d3193ed1ef6eb2ff3a561eac88eda5304ee6de
4
+ data.tar.gz: aa1721c7f178f12ff151ced96e49a0bb29397161
5
5
  SHA512:
6
- metadata.gz: bec4a8ec0e745078d43279e4faa5a0e919a4d3394ac1a8575337dc7c0465868d8786550d5ec059d05a58e082edeea28b2b041b552a0dc805479da6deb7a74393
7
- data.tar.gz: 3ad0bc15fb63dbf0fc34b7837d50da2e6f8a797b470be20099bd1577cbc380c4c8655b0870d2ed3e52f9a6ac302dc1e3f8d422d0bf73a0208bca7704cbd0572a
6
+ metadata.gz: 2b0e29168c98a7b926b400f0e2ddf7cd7b21f9041961bfc8744aa037690292246bf75101d2f134da8570e2590d999275abcecf00f031620e9e47ecb4a7c9760e
7
+ data.tar.gz: 42f9c568fef5fafc2dfeff8ddbeb3a25754dce5473a985a4df509a04220156800ff2d2a7742be6d028511306137698b6e4655786643eee357d9e4b38d89b4aa1
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jekyll-airtable (0.2.0)
4
+ jekyll-airtable (0.3.0)
5
5
  faraday (~> 0.11.0)
6
6
  faraday_middleware (~> 0.10.1)
7
7
  jekyll (~> 3.3)
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module Airtable
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -11,19 +11,21 @@ module Jekyll
11
11
  return false if site.config['AIRTABLE_API_KEY'].nil? || site.config['AIRTABLE_API_KEY'] == ''
12
12
  return false if site.config['SYNC_WITH_AIRTABLE'] == 'false'
13
13
 
14
+ setup_directories
15
+ # For storing hashes of attachments that will be saved to the data file
16
+ @attachments_hash = {}
17
+
14
18
  Airtable.configure do |config|
15
19
  config.api_key = site.config['AIRTABLE_API_KEY']
16
20
  end
17
21
 
18
- client = Airtable.client(base_uid: site.config['AIRTABLE_BASE_UID'])
22
+ client = Airtable.client(base_uid: site.config['AIRTABLE_BASE_UID'])
19
23
 
20
24
  site.config['AIRTABLE_TABLE_NAMES'].each do |table_name|
21
25
  records = client.list_records(table_name: table_name)
22
26
  next if records.size == 0
23
27
 
24
- converted_table_name = to_snake(table_name)
25
- directory_name = "collections/_" + converted_table_name
26
-
28
+ directory_name = "collections/_" + to_snake(table_name)
27
29
  Dir.mkdir(directory_name) unless File.exists?(directory_name)
28
30
 
29
31
  records.each do |record|
@@ -34,9 +36,10 @@ module Jekyll
34
36
  # However, if the record has field called 'slug', it will be used instead
35
37
  pkey = fields.keys.first
36
38
  slug = fields['slug'].nil? ? fields[pkey] : fields['slug']
39
+ filename = to_snake(slug) + '.md'
37
40
  uid = record['id']
38
41
 
39
- out_file = File.new("#{directory_name}/#{slug}.md", "w")
42
+ out_file = File.new("#{directory_name}/#{filename}", "w")
40
43
  out_file.puts(front_matter_mark)
41
44
  out_file.puts("uid: #{uid}")
42
45
 
@@ -48,6 +51,7 @@ module Jekyll
48
51
  write_array_values(out_file, value)
49
52
  else
50
53
  value = stringify_value_if_necessary(value)
54
+
51
55
  out_file.puts("#{snake_key}: #{value}")
52
56
  end
53
57
  end
@@ -57,6 +61,8 @@ module Jekyll
57
61
  out_file.close
58
62
  end
59
63
  end
64
+
65
+ write_attachments_data_file
60
66
  end
61
67
 
62
68
  private
@@ -71,15 +77,49 @@ module Jekyll
71
77
 
72
78
  def write_array_values(file, array)
73
79
  array.each do |element|
74
- # - { name: 'Low', color: '#306d8c' }
75
- value = stringify_value_if_necessary(element)
76
- file.puts(" - #{value}")
80
+ if is_this_is_an_attachment?(element)
81
+ # Store only the uid of the attachment in the front matter
82
+ value = element['id']
83
+
84
+ # Store the hash into the hash of hashes
85
+ @attachments_hash[value] = element
86
+ else
87
+ # - { name: 'Low', color: '#306d8c' }
88
+ value = stringify_value_if_necessary(element)
89
+ end
90
+
91
+ file.puts(" - #{value}")
77
92
  end
78
93
  end
79
94
 
80
95
  def stringify_value_if_necessary(value)
81
- return "'#{value}'" if value.include?(':')
82
- value
96
+ begin
97
+ return "'#{value}'" if value.include?(':')
98
+
99
+ value
100
+ rescue NoMethodError
101
+ value
102
+ end
103
+ end
104
+
105
+ def is_this_is_an_attachment?(value)
106
+ is_hash = value.class.name == 'Hash'
107
+ return false unless is_hash
108
+
109
+ value.keys.map(&:to_s).include?('filename')
110
+ end
111
+
112
+ def setup_directories
113
+ Dir.mkdir('_data') unless File.exists?('_data')
114
+ Dir.mkdir('_data/airtable') unless File.exists?('_data/airtable')
115
+
116
+ Dir.mkdir('collections') unless File.exists?('collections')
117
+ end
118
+
119
+ def write_attachments_data_file
120
+ File.open("_data/airtable/attachments.yml", "w") do |f|
121
+ f.write(@attachments_hash.to_yaml)
122
+ end
83
123
  end
84
124
  end
85
125
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-airtable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Galih Muhammad