Module: JekyllBaserowHeadlessCMS::FieldExtractors

Defined in:
lib/jekyll_baserow_headless_cms/field_extractors.rb

Overview

Module for extracting values from Baserow field types

Unlike Notion, Baserow returns row data as a flat hash (with user_field_names=true) where each key is already the field's value in its natural JSON shape. The declared field_type tells us how to interpret that shape.

Class Method Summary collapse

Class Method Details

.extract(row, field_name, field_type) ⇒ Object

Extract a field value based on its type

Parameters:

  • row (Hash)

    A row hash from the Baserow API

  • field_name (String)

    The name of the field

  • field_type (String)

    The expected type of the field

Returns:

  • (Object)

    The extracted value



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
# File 'lib/jekyll_baserow_headless_cms/field_extractors.rb', line 18

def extract(row, field_name, field_type)
  return nil unless row.key?(field_name)

  value = row[field_name]

  case field_type
  when 'text', 'long_text'
    extract_text(value)
  when 'number', 'rating', 'count'
    extract_number(value)
  when 'boolean'
    extract_boolean?(value)
  when 'date', 'created_on', 'last_modified'
    extract_date(value)
  when 'single_select'
    extract_single_select(value)
  when 'multi_select'
    extract_multi_select(value)
  when 'url'
    extract_url(value)
  when 'email'
    extract_email(value)
  when 'phone_number'
    extract_phone_number(value)
  when 'link_row'
    extract_link_row(value)
  when 'lookup', 'formula_array'
    extract_lookup(value)
  when 'formula'
    extract_formula(value)
  when 'file'
    extract_file(value)
  end
end

.extract_all(row, fields_config) ⇒ Hash

Extract all fields from a Baserow row based on configuration

Parameters:

  • row (Hash)

    A row hash from the Baserow API

  • fields_config (Array<Hash>)

    Configuration for each field

Returns:

  • (Hash)

    Extracted fields with normalized keys



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/jekyll_baserow_headless_cms/field_extractors.rb', line 57

def extract_all(row, fields_config)
  item = {}

  fields_config.each do |field_config|
    field_name = field_config['name']
    field_type = field_config['type']
    field_key = field_config['key'] || normalize_key(field_name)

    value = extract(row, field_name, field_type)
    item[field_key] = value
  end

  # Use 'title' as the main identifier, fall back to 'name'
  item['title'] ||= item['name']

  item
end

.extract_boolean?(value) ⇒ Boolean

Boolean field

Returns:

  • (Boolean)


102
103
104
# File 'lib/jekyll_baserow_headless_cms/field_extractors.rb', line 102

def extract_boolean?(value)
  value == true
end

.extract_date(value) ⇒ Object

Date / created on / last modified field (returned as an ISO 8601 string)



107
108
109
110
111
# File 'lib/jekyll_baserow_headless_cms/field_extractors.rb', line 107

def extract_date(value)
  return nil if value.nil? || value == ''

  value.to_s
end

.extract_email(value) ⇒ Object

Email field



135
136
137
138
139
# File 'lib/jekyll_baserow_headless_cms/field_extractors.rb', line 135

def extract_email(value)
  return nil if value.nil? || value == ''

  value.to_s
end

.extract_file(value) ⇒ Object

File field: array of { "url" => .., "name" => .., "mime_type" => .., ... }



174
175
176
177
178
179
180
# File 'lib/jekyll_baserow_headless_cms/field_extractors.rb', line 174

def extract_file(value)
  return [] unless value.is_a?(Array)

  value.map do |file|
    { 'name' => file['name'], 'url' => file['url'], 'type' => file['mime_type'] }.compact
  end
end

.extract_formula(value) ⇒ Object

Formula field returning a scalar value (string, number, boolean or date)



169
170
171
# File 'lib/jekyll_baserow_headless_cms/field_extractors.rb', line 169

def extract_formula(value)
  value
end

Link to table field: array of { "id" => .., "value" => .. }



149
150
151
152
153
154
155
# File 'lib/jekyll_baserow_headless_cms/field_extractors.rb', line 149

def extract_link_row(value)
  return [] unless value.is_a?(Array)

  value.map do |linked_row|
    { 'id' => linked_row['id'], 'name' => linked_row['value'] }.compact
  end
end

.extract_lookup(value) ⇒ Object

Lookup field, or a formula field returning an array: array of { "id" => .., "value" => .. } Mirrors Notion rollups: returns a single value if only one, otherwise an array



159
160
161
162
163
164
165
166
# File 'lib/jekyll_baserow_headless_cms/field_extractors.rb', line 159

def extract_lookup(value)
  return nil unless value.is_a?(Array)

  values = value.map { |item| item.is_a?(Hash) ? item['value'] : item }.compact
  return nil if values.empty?

  values.length == 1 ? values.first : values
end

.extract_multi_select(value) ⇒ Object

Multiple select field: array of { "id" => .., "value" => .., "color" => .. }



121
122
123
124
125
# File 'lib/jekyll_baserow_headless_cms/field_extractors.rb', line 121

def extract_multi_select(value)
  return [] unless value.is_a?(Array)

  value.map { |option| option['value'] }.compact
end

.extract_number(value) ⇒ Object

Number / rating / count field (Baserow may return decimal numbers as strings)



90
91
92
93
94
95
96
97
98
99
# File 'lib/jekyll_baserow_headless_cms/field_extractors.rb', line 90

def extract_number(value)
  return nil if value.nil?

  case value
  when Numeric
    value
  when String
    value.include?('.') ? value.to_f : value.to_i
  end
end

.extract_phone_number(value) ⇒ Object

Phone number field



142
143
144
145
146
# File 'lib/jekyll_baserow_headless_cms/field_extractors.rb', line 142

def extract_phone_number(value)
  return nil if value.nil? || value == ''

  value.to_s
end

.extract_single_select(value) ⇒ Object

Single select field: { "id" => 1, "value" => "Backend", "color" => "blue" }



114
115
116
117
118
# File 'lib/jekyll_baserow_headless_cms/field_extractors.rb', line 114

def extract_single_select(value)
  return nil unless value.is_a?(Hash)

  value['value']
end

.extract_text(value) ⇒ Object

Text / long text field



83
84
85
86
87
# File 'lib/jekyll_baserow_headless_cms/field_extractors.rb', line 83

def extract_text(value)
  return nil if value.nil? || value == ''

  value.to_s
end

.extract_url(value) ⇒ Object

URL field



128
129
130
131
132
# File 'lib/jekyll_baserow_headless_cms/field_extractors.rb', line 128

def extract_url(value)
  return nil if value.nil? || value == ''

  value.to_s
end

.normalize_key(name) ⇒ String

Normalize a field name to a valid key

Parameters:

  • name (String)

    The field name

Returns:

  • (String)

    The normalized key



78
79
80
# File 'lib/jekyll_baserow_headless_cms/field_extractors.rb', line 78

def normalize_key(name)
  name.downcase.gsub(/\s+/, '_')
end