Add source response

This commit is contained in:
2023-02-03 11:16:31 +11:00
parent dd31388c2c
commit 19fe581c79
5 changed files with 54 additions and 4 deletions

View File

@@ -26,6 +26,7 @@ gem "builder"
gem "httparty" gem "httparty"
gem "redcarpet" gem "redcarpet"
gem "reverse_markdown"
gem "rexml" gem "rexml"
gem "babosa" gem "babosa"
gem "pinboard", github: "dnitza/pinboard", branch: "master" gem "pinboard", github: "dnitza/pinboard", branch: "master"

View File

@@ -203,6 +203,10 @@ GEM
net-ssh (>= 2.6.5, < 8.0.0) net-ssh (>= 2.6.5, < 8.0.0)
net-ssh (7.0.1) net-ssh (7.0.1)
nio4r (2.5.8) 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) notiffany (0.1.3)
nenv (~> 0.1) nenv (~> 0.1)
shellany (~> 0.0) shellany (~> 0.0)
@@ -215,6 +219,7 @@ GEM
method_source (~> 1.0) method_source (~> 1.0)
puma (6.0.1) puma (6.0.1)
nio4r (~> 2.0) nio4r (~> 2.0)
racc (1.6.2)
rack (2.2.5) rack (2.2.5)
rack-test (2.0.2) rack-test (2.0.2)
rack (>= 1.3) rack (>= 1.3)
@@ -225,6 +230,8 @@ GEM
ffi (~> 1.0) ffi (~> 1.0)
redcarpet (3.5.1) redcarpet (3.5.1)
regexp_parser (2.6.1) regexp_parser (2.6.1)
reverse_markdown (2.1.1)
nokogiri
rexml (3.2.5) rexml (3.2.5)
rom (5.3.0) rom (5.3.0)
rom-changeset (~> 5.3, >= 5.3.0) rom-changeset (~> 5.3, >= 5.3.0)
@@ -342,6 +349,7 @@ DEPENDENCIES
rack-test rack-test
rake rake
redcarpet redcarpet
reverse_markdown
rexml rexml
rom-factory rom-factory
rom-sql rom-sql

View File

@@ -2,7 +2,7 @@ module Adamantium
module Actions module Actions
module Site module Site
class Config < Action class Config < Action
include Deps["settings", "views.site.home"] include Deps["settings", "views.site.home", "queries.posts.microformat_post"]
before :authenticate! before :authenticate!
def handle(req, res) def handle(req, res)
@@ -46,6 +46,10 @@ module Adamantium
} }
] ]
}.to_json }.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 else
res.render home res.render home
end end

View File

@@ -2,10 +2,12 @@ module Adamantium
module Commands module Commands
module Posts module Posts
class Update < Command class Update < Command
def call(params) include Deps["repos.post_repo"]
slug = URI(params[:url]).path.split("/").last
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 end
end end

View File

@@ -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