Module: JekyllAdmin::APIable

Included in:
DataFile, Directory
Defined in:
lib/jekyll-admin/apiable.rb

Overview

Abstract module to be included in Convertible and Document to provide additional, API-specific functionality without duplicating logic

Constant Summary collapse

CONTENT_FIELDS =
%w(next previous content excerpt).freeze

Instance Method Summary collapse

Instance Method Details

#to_api(include_content: false) ⇒ Object

Returns a hash suitable for use as an API response.

For Documents and Pages:

  1. Adds the file’s raw content to the ‘raw_content` field

  2. Adds the file’s raw YAML front matter to the ‘front_matter` field

For Static Files it addes the Base64 ‘encoded_content` field

Options:

include_content - if true, includes the content in the respond, false by default

to support mapping on indexes where we only want 

Returns a hash (which can then be to_json’d)



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
52
53
54
55
56
57
# File 'lib/jekyll-admin/apiable.rb', line 24

def to_api(include_content: false)
  output = hash_for_api
  output = output.merge(url_fields)

  # Include content, if requested, otherwise remove it
  if include_content
    output = output.merge(content_fields)
  else
    CONTENT_FIELDS.each { |field| output.delete(field) }
  end

  # Documents have duplicate output and content fields, Pages do not
  # Since it's an API, use `content` in both for consistency
  output.delete("output")

  # By default, calling to_liquid on a collection will return a docs
  # array with each rendered document, which we don't want.
  if is_a?(Jekyll::Collection)
    output.delete("docs")
    output["entries_url"] = entries_url
  end

  if is_a?(Jekyll::Document)
    output["name"] = basename
    output["modified_time"] = mtime
    output["relative_path"] = relative_path.sub("_drafts/", "") if draft?
  end

  if is_a?(Jekyll::StaticFile)
    output["from_theme"] = from_theme_gem?
  end

  output
end