module BuntoAdmin::APIable
Abstract module to be included in Convertible and Document to provide additional, API-specific functionality without duplicating logic
Constants
- CONTENT_FIELDS
Public Instance Methods
to_api(include_content: false)
click to toggle source
Returns a hash suitable for use as an API response.
For Documents and Pages:
-
Adds the file's raw content to the `raw_content` field
-
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 metadata
Returns a hash (which can then be to_json'd)
# File lib/bunto-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?(Bunto::Collection) output.delete("docs") output["entries_url"] = entries_url end if is_a?(Bunto::Document) output["name"] = basename end if is_a?(Bunto::StaticFile) output["from_theme"] = from_theme_gem? end output end
Private Instance Methods
content_fields()
click to toggle source
Returns a hash of content fields for inclusion in the API output
# File lib/bunto-admin/apiable.rb, line 127 def content_fields output = {} # Include file content-related fields if is_a?(Bunto::StaticFile) output["encoded_content"] = encoded_content elsif is_a?(BuntoAdmin::DataFile) output["content"] = content output["raw_content"] = raw_content else output["raw_content"] = raw_content output["front_matter"] = front_matter end # Include next and previous documents non-recursively if is_a?(Bunto::Document) %w(next previous).each do |direction| method = "#{direction}_doc".to_sym doc = public_send(method) output[direction] = doc.to_api if doc end end output end
encoded_content()
click to toggle source
# File lib/bunto-admin/apiable.rb, line 107 def encoded_content @encoded_content ||= Base64.encode64(file_contents) if file_exists? end
file_contents()
click to toggle source
# File lib/bunto-admin/apiable.rb, line 81 def file_contents @file_contents ||= File.read(file_path, file_read_options) if file_exists? end
file_exists?()
click to toggle source
# File lib/bunto-admin/apiable.rb, line 111 def file_exists? return @file_exists if defined? @file_exists @file_exists = File.exist?(file_path) end
file_path()
click to toggle source
Pages don't have a hash method, but Documents do
# File lib/bunto-admin/apiable.rb, line 60 def file_path if is_a?(Bunto::Document) path else File.join(@base, @dir, name) end end
file_read_options()
click to toggle source
# File lib/bunto-admin/apiable.rb, line 85 def file_read_options Bunto::Utils.merged_file_read_opts(site, {}) end
from_theme_gem?()
click to toggle source
# File lib/bunto-admin/apiable.rb, line 68 def from_theme_gem? @base == site.in_theme_dir end
front_matter()
click to toggle source
# File lib/bunto-admin/apiable.rb, line 89 def front_matter return unless file_exists? @front_matter ||= if file_contents =~ Bunto::Document::YAML_FRONT_MATTER_REGEXP SafeYAML.load(Regexp.last_match(1)) else {} end end
hash_for_api()
click to toggle source
Base hash from which to generate the API output
# File lib/bunto-admin/apiable.rb, line 117 def hash_for_api output = to_liquid if output.respond_to?(:hash_for_json) output.hash_for_json else output.to_h end end
name()
click to toggle source
# File lib/bunto-admin/apiable.rb, line 77 def name @name end
raw_content()
click to toggle source
# File lib/bunto-admin/apiable.rb, line 98 def raw_content return unless file_exists? @raw_content ||= if file_contents =~ Bunto::Document::YAML_FRONT_MATTER_REGEXP $POSTMATCH else file_contents end end
site()
click to toggle source
StaticFiles don't have `attr_accesor` set for @site or @name
# File lib/bunto-admin/apiable.rb, line 73 def site @site end
url_fields()
click to toggle source
# File lib/bunto-admin/apiable.rb, line 153 def url_fields return {} unless respond_to?(:http_url) && respond_to?(:api_url) { "http_url" => http_url, "api_url" => api_url, } end