Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize API Hash objects #528

Merged
merged 3 commits into from
Nov 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/jekyll-admin/apiable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module JekyllAdmin
# additional, API-specific functionality without duplicating logic
module APIable
CONTENT_FIELDS = %w(next previous content excerpt).freeze
API_SCAFFOLD = %w(name path).map { |i| [i, nil] }.to_h.freeze

# Returns a hash suitable for use as an API response.
#
Expand All @@ -20,15 +21,15 @@ module APIable
# 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)
#
# rubocop:disable Metrics/AbcSize
def to_api(include_content: false)
output = hash_for_api
output = output.merge(url_fields)
output = API_SCAFFOLD.merge hash_for_api

# Include content, if requested, otherwise remove it
if include_content
output = output.merge(content_fields)
output.merge!(content_fields)
else
CONTENT_FIELDS.each { |field| output.delete(field) }
end
Expand All @@ -41,6 +42,8 @@ def to_api(include_content: false)
# array with each rendered document, which we don't want.
if is_a?(Jekyll::Collection)
output.delete("docs")
output["name"] = label
output["path"] = relative_directory
output["entries_url"] = entries_url
end

Expand All @@ -51,8 +54,10 @@ def to_api(include_content: false)

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

output.merge!(url_fields)
output
end
# rubocop:enable Metrics/AbcSize

private

Expand Down