Initial commit
This commit is contained in:
13
lib/adamantium/command.rb
Normal file
13
lib/adamantium/command.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
# auto_register: false
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "dry-matcher"
|
||||
require "dry/matcher/result_matcher"
|
||||
require "dry-monads"
|
||||
|
||||
module Adamantium
|
||||
class Command
|
||||
include Dry::Matcher.for(:call, with: Dry::Matcher::ResultMatcher)
|
||||
include Dry::Monads[:result]
|
||||
end
|
||||
end
|
14
lib/adamantium/context.rb
Normal file
14
lib/adamantium/context.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
module Adamantium
|
||||
class Context < Hanami::View::Context
|
||||
def initialize(**options)
|
||||
@options = options
|
||||
super(**options)
|
||||
end
|
||||
|
||||
def link_active? path
|
||||
# TODO: waiting for Hanami View to be released
|
||||
# to access current_path
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
85
lib/adamantium/micropub_request_parser.rb
Normal file
85
lib/adamantium/micropub_request_parser.rb
Normal file
@@ -0,0 +1,85 @@
|
||||
require "securerandom"
|
||||
|
||||
module Adamantium
|
||||
class MicropubRequestParser
|
||||
include Deps["post_utilities.slugify", "repos.post_repo"]
|
||||
|
||||
def call(params:)
|
||||
return nil if params.key?(:action)
|
||||
|
||||
cont_type = content_type(params)
|
||||
req_type = request_type(params)
|
||||
req_params = parse_params(req_type, cont_type, params)
|
||||
|
||||
if cont_type == :bookmark
|
||||
return Entities::BookmarkRequest.new(req_params)
|
||||
end
|
||||
|
||||
Entities::PostRequest.new(req_params)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def slug(name:, default_slug:)
|
||||
return default_slug if default_slug
|
||||
|
||||
slugify.call(
|
||||
text: name,
|
||||
checker: post_repo.method(:slug_exists?)
|
||||
)
|
||||
end
|
||||
|
||||
def content_type(params)
|
||||
return :bookmark if params[:"bookmark-of"]
|
||||
:post
|
||||
end
|
||||
|
||||
def request_type(params)
|
||||
if params[:h] == "entry"
|
||||
return :form
|
||||
end
|
||||
|
||||
if params[:type]&.include?("h-entry")
|
||||
return :json
|
||||
end
|
||||
|
||||
nil
|
||||
end
|
||||
|
||||
def parse_params(req_type, post_type, params)
|
||||
new_params = {}
|
||||
new_params[:h] = "entry"
|
||||
new_params[:post_type] = post_type
|
||||
new_params[:action] = params[:action]
|
||||
|
||||
publish_time = params[:published_at] || Time.now
|
||||
|
||||
if req_type == :json
|
||||
new_params[:published_at] = (params[:"post-status"] == "draft") ? nil : publish_time
|
||||
new_params[:category] = params[:properties][:category] || []
|
||||
new_params[:name] = params[:properties][:name] && params[:properties][:name].first
|
||||
new_params[:content] = params[:properties][:content]&.first&.tr("\n", " ")
|
||||
new_params[:slug] = params[:slug]
|
||||
|
||||
else
|
||||
new_params[:name] = params[:name]
|
||||
new_params[:published_at] = (params[:"post-status"] == "draft") ? nil : publish_time
|
||||
new_params[:category] = params[:category] || []
|
||||
|
||||
content = if params[:content]
|
||||
if params[:content].is_a?(Hash) && params[:content][:html]
|
||||
params[:content][:html]
|
||||
else
|
||||
params[:content]
|
||||
end
|
||||
end
|
||||
|
||||
new_params[:content] = content
|
||||
end
|
||||
new_params[:url] = params[:"bookmark-of"]
|
||||
new_params[:slug] = slug(name: new_params[:name], default_slug: params[:slug])
|
||||
|
||||
new_params
|
||||
end
|
||||
end
|
||||
end
|
14
lib/adamantium/middleware/process_params.rb
Normal file
14
lib/adamantium/middleware/process_params.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
module Adamantium
|
||||
module Middleware
|
||||
class ProcessParams
|
||||
def initialize(app)
|
||||
@app = app
|
||||
end
|
||||
|
||||
def call(env)
|
||||
# NOOP for now.
|
||||
@app.call(env)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
18
lib/adamantium/persistence/relations/post_tags.rb
Normal file
18
lib/adamantium/persistence/relations/post_tags.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Adamantium
|
||||
module Persistence
|
||||
module Relations
|
||||
class PostTags < ROM::Relation[:sql]
|
||||
schema :post_tags, infer: true do
|
||||
associations do
|
||||
belongs_to :post
|
||||
belongs_to :tag
|
||||
end
|
||||
end
|
||||
|
||||
auto_struct(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
22
lib/adamantium/persistence/relations/posts.rb
Normal file
22
lib/adamantium/persistence/relations/posts.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Adamantium
|
||||
module Persistence
|
||||
module Relations
|
||||
class Posts < ROM::Relation[:sql]
|
||||
schema :posts, infer: true do
|
||||
associations do
|
||||
has_many :post_tags
|
||||
has_many :tags, through: :post_tags
|
||||
end
|
||||
end
|
||||
|
||||
auto_struct(true)
|
||||
|
||||
def published
|
||||
where(self[:published_at] <= Time.now)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
18
lib/adamantium/persistence/relations/tags.rb
Normal file
18
lib/adamantium/persistence/relations/tags.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Adamantium
|
||||
module Persistence
|
||||
module Relations
|
||||
class Tags < ROM::Relation[:sql]
|
||||
schema :tags, infer: true do
|
||||
associations do
|
||||
belongs_to :post_tag
|
||||
belongs_to :post, through: :post_tag
|
||||
end
|
||||
end
|
||||
|
||||
auto_struct(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
21
lib/adamantium/renderer/markdown.rb
Normal file
21
lib/adamantium/renderer/markdown.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
require "redcarpet"
|
||||
|
||||
module Adamantium
|
||||
module Renderer
|
||||
class Markdown
|
||||
attr_accessor :markdown
|
||||
|
||||
def initialize
|
||||
renderer = Redcarpet::Render::HTML.new({})
|
||||
extensions = {
|
||||
fenced_code_blocks: true
|
||||
}
|
||||
@markdown = Redcarpet::Markdown.new(renderer, extensions)
|
||||
end
|
||||
|
||||
def call(content:)
|
||||
markdown.render(content)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
10
lib/adamantium/repo.rb
Normal file
10
lib/adamantium/repo.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
# auto_register: false
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "rom-repository"
|
||||
|
||||
module Adamantium
|
||||
class Repo < ROM::Repository::Root
|
||||
include Deps[container: "persistence.rom"]
|
||||
end
|
||||
end
|
20
lib/adamantium/slug_creator.rb
Normal file
20
lib/adamantium/slug_creator.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
require "babosa"
|
||||
require "securerandom"
|
||||
|
||||
module Adamantium
|
||||
class SlugCreator
|
||||
def call(text:, checker:)
|
||||
input_slug = (text != "" && !text.nil?) ? text.to_slug.normalize.to_s : SecureRandom.uuid
|
||||
slug = input_slug
|
||||
|
||||
suffix = 1
|
||||
|
||||
while checker.call(slug)
|
||||
slug = "#{input_slug}-#{suffix}"
|
||||
suffix += 1
|
||||
end
|
||||
|
||||
slug
|
||||
end
|
||||
end
|
||||
end
|
11
lib/adamantium/types.rb
Normal file
11
lib/adamantium/types.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "dry/types"
|
||||
|
||||
module Adamantium
|
||||
Types = Dry.Types
|
||||
|
||||
module Types
|
||||
# Define your custom types here
|
||||
end
|
||||
end
|
10
lib/adamantium/view.rb
Normal file
10
lib/adamantium/view.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
# auto_register: false
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "hanami/view"
|
||||
|
||||
module Adamantium
|
||||
class View < Hanami::View
|
||||
config.default_context = Adamantium::Context.new
|
||||
end
|
||||
end
|
0
lib/tasks/.keep
Normal file
0
lib/tasks/.keep
Normal file
Reference in New Issue
Block a user