From 19fe581c79ac4a5891f979a831c49de477e97d5b Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Fri, 3 Feb 2023 11:16:31 +1100 Subject: [PATCH] Add source response --- Gemfile | 1 + Gemfile.lock | 8 ++++++ app/actions/site/config.rb | 6 ++++- app/commands/posts/update.rb | 8 +++--- app/queries/posts/microformat_post.rb | 35 +++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 app/queries/posts/microformat_post.rb diff --git a/Gemfile b/Gemfile index 97a0608..3f0fa57 100644 --- a/Gemfile +++ b/Gemfile @@ -26,6 +26,7 @@ gem "builder" gem "httparty" gem "redcarpet" +gem "reverse_markdown" gem "rexml" gem "babosa" gem "pinboard", github: "dnitza/pinboard", branch: "master" diff --git a/Gemfile.lock b/Gemfile.lock index 7b62892..71e0760 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -203,6 +203,10 @@ GEM net-ssh (>= 2.6.5, < 8.0.0) net-ssh (7.0.1) nio4r (2.5.8) + nokogiri (1.14.1-x86_64-darwin) + racc (~> 1.4) + nokogiri (1.14.1-x86_64-linux) + racc (~> 1.4) notiffany (0.1.3) nenv (~> 0.1) shellany (~> 0.0) @@ -215,6 +219,7 @@ GEM method_source (~> 1.0) puma (6.0.1) nio4r (~> 2.0) + racc (1.6.2) rack (2.2.5) rack-test (2.0.2) rack (>= 1.3) @@ -225,6 +230,8 @@ GEM ffi (~> 1.0) redcarpet (3.5.1) regexp_parser (2.6.1) + reverse_markdown (2.1.1) + nokogiri rexml (3.2.5) rom (5.3.0) rom-changeset (~> 5.3, >= 5.3.0) @@ -342,6 +349,7 @@ DEPENDENCIES rack-test rake redcarpet + reverse_markdown rexml rom-factory rom-sql diff --git a/app/actions/site/config.rb b/app/actions/site/config.rb index deb85b5..eee8f31 100644 --- a/app/actions/site/config.rb +++ b/app/actions/site/config.rb @@ -2,7 +2,7 @@ module Adamantium module Actions module Site class Config < Action - include Deps["settings", "views.site.home"] + include Deps["settings", "views.site.home", "queries.posts.microformat_post"] before :authenticate! def handle(req, res) @@ -46,6 +46,10 @@ module Adamantium } ] }.to_json + elsif req.params[:q] == "source" + res.status = 200 + res.content_type = "Application/JSON" + res.body = microformat_post.call(url: req.params[:url], properties: req.params[:properties]).to_json else res.render home end diff --git a/app/commands/posts/update.rb b/app/commands/posts/update.rb index 0cc099e..808e4f2 100644 --- a/app/commands/posts/update.rb +++ b/app/commands/posts/update.rb @@ -2,10 +2,12 @@ module Adamantium module Commands module Posts class Update < Command - def call(params) - slug = URI(params[:url]).path.split("/").last + include Deps["repos.post_repo"] - post_repo.update(slug, params) + def call(params:) + slug = URI(params[:url]).path.split("/").last + post = post_repo.fetch!(slug) + post_repo.update(post.id, params) end end end diff --git a/app/queries/posts/microformat_post.rb b/app/queries/posts/microformat_post.rb new file mode 100644 index 0000000..f070d39 --- /dev/null +++ b/app/queries/posts/microformat_post.rb @@ -0,0 +1,35 @@ +require "reverse_markdown" + +module Adamantium + module Queries + module Posts + class MicroformatPost + include Deps["repos.post_repo"] + + def call(url:, properties:) + slug = URI(url).path.split("/").last + + post = post_repo.fetch!(slug) + markdown_content = ReverseMarkdown.convert(post.content, unknown_tags: :raise, github_flavored: true).to_s + + if properties.nil? || properties.empty? + return { + type: ["h-entry"], + properties: { + published: [post.published_at], + content: [markdown_content], + category: post.tags.map { |t| t.label.to_s } + } + } + end + + result = {properties: {}} + result[:properties][:published] = [post.published_at] if properties.include? "published" + result[:properties][:content] = [markdown_content] if properties.include? "content" + result[:properties][:category] = post.tags.map { |t| t.label.to_s } if properties.include? "category" + result + end + end + end + end +end