class BuntoAdmin::Server

Constants

EXTENSIONS

supported extensions, in order of preference, for now, no .csv

ROUTES

Public Instance Methods

collection() click to toggle source
# File lib/bunto-admin/server/collection.rb, line 45
def collection
  collection = site.collections.find { |l, _c| l == params["collection_id"] }
  collection[1] if collection
end
configuration() click to toggle source

Computed configuration, with updates and defaults

# File lib/bunto-admin/server/configuration.rb, line 25
def configuration
  @configuration ||= Bunto.configuration(overrides)
end
configuration_body() click to toggle source

The user's uploaded configuration for updates Instead of extracting `raw_content` directly from the `request_payload`,

assign the data to a new variable and then extract the `raw_content`
from it to circumvent CORS violation in `development` mode.
# File lib/bunto-admin/server/configuration.rb, line 51
def configuration_body
  payload = request_payload
  payload["raw_content"]
end
configuration_path() click to toggle source

Returns the path to the first config file discovered

# File lib/bunto-admin/server/configuration.rb, line 43
def configuration_path
  sanitized_path configuration.config_files(overrides).first
end
data_file_body() click to toggle source
# File lib/bunto-admin/server/data.rb, line 69
def data_file_body
  if !request_payload["raw_content"].to_s.empty?
    request_payload["raw_content"]
  elsif !request_payload["content"].to_s.empty?
    YAML.dump(request_payload["content"]).sub(%r!\A---\n!, "")
  end
end
directory_docs() click to toggle source
# File lib/bunto-admin/server/collection.rb, line 55
def directory_docs
  collection.docs.find_all { |d| File.dirname(d.path) == directory_path }
end
directory_pages() click to toggle source
# File lib/bunto-admin/server/data.rb, line 42
def directory_pages
  DataFile.all.find_all do |p|
    sanitized_path(File.dirname(p.path)) == directory_path
  end
end
directory_paths() click to toggle source

returns relative path of root level directories that contain data files

# File lib/bunto-admin/server/data.rb, line 38
def directory_paths
  DataFile.all.map { |p| File.dirname(p.relative_path).split("/")[0] }.uniq
end
document_id() click to toggle source
# File lib/bunto-admin/server/collection.rb, line 50
def document_id
  path = "#{params["splat"].first}/#{filename}"
  path.gsub(%r!(\d{4})/(\d{2})/(\d{2})/(.*)!, '\1-\2-\3-\4')
end
ensure_collection() click to toggle source
# File lib/bunto-admin/server/collection.rb, line 59
def ensure_collection
  render_404 if collection.nil?
end
ensure_directory() click to toggle source
# File lib/bunto-admin/server/collection.rb, line 63
def ensure_directory
  ensure_collection
  render_404 unless Dir.exist?(directory_path)
end
ensure_html_content() click to toggle source
# File lib/bunto-admin/server/page.rb, line 37
def ensure_html_content
  return if html_content?
  content_type :json
  halt 422, json("error_message" => "Invalid file extension for pages")
end
entries() click to toggle source
# File lib/bunto-admin/server/collection.rb, line 68
def entries
  args = {
    :base         => site.source,
    :content_type => params["collection_id"],
    :splat        => params["splat"].first,
  }
  # get the directories inside the requested directory
  directory = BuntoAdmin::Directory.new(directory_path, args)
  directories = directory.directories
  # merge directories with the documents at the same level
  directories.concat(directory_docs.sort_by(&:date).reverse)
end
file_list_dir(path) click to toggle source
# File lib/bunto-admin/server/static_file.rb, line 50
def file_list_dir(path) end
html_content?() click to toggle source
# File lib/bunto-admin/server/page.rb, line 43
def html_content?
  page = BuntoAdmin::PageWithoutAFile.new(
    site,
    site.source,
    "",
    request_payload["path"] || filename
  )
  page.data = request_payload["front_matter"]
  page.html?
end
overrides() click to toggle source
# File lib/bunto-admin/server/configuration.rb, line 18
def overrides
  {
    "source" => sanitized_path("/"),
  }
end
pages() click to toggle source
# File lib/bunto-admin/server/page.rb, line 54
def pages
  site.pages.select(&:html?)
end
parsed_configuration() click to toggle source

Configuration data, as read by Bunto

# File lib/bunto-admin/server/configuration.rb, line 30
def parsed_configuration
  configuration.read_config_file(configuration_path)
end
raw_configuration() click to toggle source

Raw configuration content, as it sits on disk

# File lib/bunto-admin/server/configuration.rb, line 35
def raw_configuration
  File.read(
    configuration_path,
    Bunto::Utils.merged_file_read_opts(site, {})
  )
end
splats() click to toggle source
# File lib/bunto-admin/server/data.rb, line 77
def splats
  params["splat"] || ["/"]
end
static_file_body() click to toggle source
# File lib/bunto-admin/server/static_file.rb, line 38
def static_file_body
  if !request_payload["raw_content"].to_s.empty?
    request_payload["raw_content"].to_s
  else
    Base64.decode64 request_payload["encoded_content"].to_s
  end
end
static_files() click to toggle source
# File lib/bunto-admin/server/static_file.rb, line 46
def static_files
  site.static_files
end
static_files_for_path() click to toggle source
# File lib/bunto-admin/server/static_file.rb, line 52
def static_files_for_path
  # Joined with / to ensure user can't do partial paths
  base_path = File.join(path, "/")
  static_files.select do |f|
    f.path.start_with? base_path
  end
end

Private Instance Methods

base_url() click to toggle source
# File lib/bunto-admin/server.rb, line 52
def base_url
  "#{request.scheme}://#{request.host_with_port}"
end
document_body() click to toggle source
# File lib/bunto-admin/server.rb, line 60
def document_body
  body = if front_matter && !front_matter.empty?
           YAML.dump(restored_front_matter).strip
             .gsub(": 'null'", ": null") # restore null values
         else
           "---"
         end
  body << "\n---\n\n"
  body << request_payload["raw_content"].to_s
end
Also aliased as: page_body
front_matter() click to toggle source
# File lib/bunto-admin/server.rb, line 56
def front_matter
  request_payload["front_matter"]
end
namespace() click to toggle source
# File lib/bunto-admin/server.rb, line 81
def namespace
  namespace = request.path_info.split("/")[1].to_s.downcase
  namespace if ROUTES.include?(namespace)
end
page_body()
Alias for: document_body
render_404() click to toggle source
# File lib/bunto-admin/server.rb, line 38
def render_404
  status 404
  content_type :json
  halt
end
request_body() click to toggle source
# File lib/bunto-admin/server.rb, line 74
def request_body
  @request_body ||= begin
    request.body.rewind
    request.body.read
  end
end
request_payload() click to toggle source
# File lib/bunto-admin/server.rb, line 44
def request_payload
  @request_payload ||= if request_body.to_s.empty?
                         {}
                       else
                         JSON.parse(request_body)
                       end
end
restored_front_matter() click to toggle source

verbose 'null' values in front matter

# File lib/bunto-admin/server.rb, line 87
def restored_front_matter
  front_matter.map do |key, value|
    value = "null" if value.nil?
    [key, value]
  end.to_h
end
site() click to toggle source
# File lib/bunto-admin/server.rb, line 34
def site
  BuntoAdmin.site
end