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

API for Dashboard #329

Closed
wants to merge 4 commits into from
Closed
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
8 changes: 8 additions & 0 deletions lib/jekyll-admin/apiable.rb
Original file line number Diff line number Diff line change
@@ -47,6 +47,10 @@ def to_api(include_content: false)
output["name"] = basename
end

if is_a?(Jekyll::Page) || is_a?(Jekyll::Document)
output["modified_at"] = mtime
end

output
end

@@ -70,6 +74,10 @@ def name
@name
end

def mtime
@mtime ||= File.stat(file_path).mtime
end

def file_contents
@file_contents ||= File.read(file_path, file_read_options) if file_exists?
end
3 changes: 2 additions & 1 deletion lib/jekyll-admin/server.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module JekyllAdmin
class Server < Sinatra::Base
ROUTES = %w(collections configuration data pages static_files).freeze
ROUTES = %w(collections configuration dashboard data pages static_files).freeze
include JekyllAdmin::PathHelper
include JekyllAdmin::FileHelper

@@ -86,6 +86,7 @@ def namespace

require "jekyll-admin/server/collection"
require "jekyll-admin/server/configuration"
require "jekyll-admin/server/dashboard"
require "jekyll-admin/server/data"
require "jekyll-admin/server/page"
require "jekyll-admin/server/static_file"
45 changes: 45 additions & 0 deletions lib/jekyll-admin/server/dashboard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module JekyllAdmin
class Server < Sinatra::Base
namespace "/dashboard" do
get do
json site.site_payload.to_h.merge({
"site" => dashboard_site_payload,
})
end

private

def dashboard_site_payload
output = site.site_payload["site"].to_h.merge({
"data_files" => data_files,
"html_pages" => named_html_pages,
"pages" => recent_pages.map { |page| page.to_api["path"] },
"posts" => paths_to_posts,
"collections" => site.collections.map { |c| c[0] },
"watch" => false, # manually override value set by --watch
})

output.delete("documents")
output.delete("data")

output
end

def named_html_pages
site.pages.select(&:html?).map { |page| page.to_api["name"] }
end

def recent_pages
site.pages.sort_by! { |page| page.to_api["modified_at"] }.reverse
end

def paths_to_posts
site.posts.docs.map { |post| post.relative_path.sub("_posts/", "") }
end

def data_files
DataFile.all.map { |d| d.to_api["relative_path"].sub("/_data/", "") }
end
end
end
end
26 changes: 26 additions & 0 deletions spec/jekyll-admin/server/dashboard_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
describe "dashboard" do
include Rack::Test::Methods

def app
JekyllAdmin::Server
end

it "returns the modified site payload" do
get "/dashboard"
expect(last_response).to be_ok
expect(last_response_parsed["site"]["posts"]).to eql(
[
"2016-01-01-test-post.md",
"test/2016-01-02-test2.md",
"2016-02-01-test-post-2.md",
"2016-03-01-test-post-3.md",
"more posts/2016-04-01-post-within-subdirectory.md",
"more posts/some more posts/2016-05-01-a-test-post-within-subdirectory.md",
"more posts/some more posts/2016-05-02-another-test-post-within-subdirectory.md",
]
)
expect(last_response_parsed["site"]["pages"]).to include("assets/style.scss")
expect(last_response_parsed["site"]["data"]).to eql(nil)
expect(last_response_parsed["site"]["documents"]).to eql(nil)
end
end