mirror of
https://github.com/danbee/micropub.git
synced 2025-03-04 08:59:13 +00:00
Re-org and solve config_file warnings
This commit is contained in:
parent
fcdb5e4e34
commit
8590cf7be3
@ -2,8 +2,8 @@ autoload :Indieauth, File.expand_path('indieauth.rb', __dir__)
|
||||
|
||||
module Micropub
|
||||
autoload :Github, File.expand_path('micropub/github.rb', __dir__)
|
||||
autoload :Post, File.expand_path('micropub/models/post.rb', __dir__)
|
||||
autoload :Post, File.expand_path('micropub/post.rb', __dir__)
|
||||
autoload :PostJSONParser,
|
||||
File.expand_path('micropub/models/post_json_parser.rb', __dir__)
|
||||
File.expand_path('micropub/post_json_parser.rb', __dir__)
|
||||
autoload :Webserver, File.expand_path('micropub/webserver.rb', __dir__)
|
||||
end
|
||||
|
||||
@ -1,81 +0,0 @@
|
||||
require "date"
|
||||
require "kramdown"
|
||||
|
||||
class Post
|
||||
attr_accessor :params
|
||||
|
||||
def initialize(params = {})
|
||||
@params = params
|
||||
end
|
||||
|
||||
def id
|
||||
title.
|
||||
downcase.
|
||||
gsub(/[^a-z]+/, " ").
|
||||
strip.
|
||||
gsub(" ", "-")
|
||||
end
|
||||
|
||||
def title
|
||||
params["title"] || truncated_content
|
||||
end
|
||||
|
||||
def path
|
||||
"/blog/#{published.strftime("%Y/%m/%d")}/#{id}"
|
||||
end
|
||||
|
||||
def truncated_content
|
||||
content[0..content.index(".")].
|
||||
split(" ").
|
||||
take(6).
|
||||
join(" ").
|
||||
gsub(/([a-z])$/, "\\1…")
|
||||
end
|
||||
|
||||
def published
|
||||
if params["published"]
|
||||
DateTime.parse(params["published"])
|
||||
else
|
||||
DateTime.now
|
||||
end
|
||||
end
|
||||
|
||||
def categories
|
||||
if params["category"].is_a?(Array)
|
||||
params["category"] || []
|
||||
else
|
||||
[params["category"]].compact
|
||||
end
|
||||
end
|
||||
|
||||
def content
|
||||
if params["content"].is_a?(Hash)
|
||||
content_from_hash
|
||||
else
|
||||
params["content"]
|
||||
end
|
||||
end
|
||||
|
||||
def content_from_hash
|
||||
if params["content"]["text"]
|
||||
params["content"]["text"]
|
||||
elsif params["content"]["html"]
|
||||
Kramdown::Document.
|
||||
new(params["content"]["html"], html_to_native: true).
|
||||
to_kramdown
|
||||
end
|
||||
end
|
||||
|
||||
def post_content
|
||||
<<~POST
|
||||
---
|
||||
date: '#{published.rfc3339}'
|
||||
layout: micropost
|
||||
categories:
|
||||
#{categories.map { |category| "- #{category}\n" }.join.strip}
|
||||
---
|
||||
|
||||
#{content}
|
||||
POST
|
||||
end
|
||||
end
|
||||
@ -1,29 +0,0 @@
|
||||
class PostJSONParser
|
||||
def initialize(json)
|
||||
@data = JSON.parse(json)
|
||||
end
|
||||
|
||||
def params
|
||||
{
|
||||
"title" => title,
|
||||
"content" => content,
|
||||
"category" => category,
|
||||
}.compact
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_accessor :data
|
||||
|
||||
def title
|
||||
data.dig("properties", "title", 0)
|
||||
end
|
||||
|
||||
def content
|
||||
data.dig("properties", "content", 0)
|
||||
end
|
||||
|
||||
def category
|
||||
data.dig("properties", "category")
|
||||
end
|
||||
end
|
||||
83
lib/micropub/post.rb
Normal file
83
lib/micropub/post.rb
Normal file
@ -0,0 +1,83 @@
|
||||
require "date"
|
||||
require "kramdown"
|
||||
|
||||
module Micropub
|
||||
class Post
|
||||
attr_accessor :params
|
||||
|
||||
def initialize(params = {})
|
||||
@params = params
|
||||
end
|
||||
|
||||
def id
|
||||
title.
|
||||
downcase.
|
||||
gsub(/[^a-z]+/, " ").
|
||||
strip.
|
||||
gsub(" ", "-")
|
||||
end
|
||||
|
||||
def title
|
||||
params["title"] || truncated_content
|
||||
end
|
||||
|
||||
def path
|
||||
"/blog/#{published.strftime("%Y/%m/%d")}/#{id}"
|
||||
end
|
||||
|
||||
def truncated_content
|
||||
content[0..content.index(".")].
|
||||
split(" ").
|
||||
take(6).
|
||||
join(" ").
|
||||
gsub(/([a-z])$/, "\\1…")
|
||||
end
|
||||
|
||||
def published
|
||||
if params["published"]
|
||||
DateTime.parse(params["published"])
|
||||
else
|
||||
DateTime.now
|
||||
end
|
||||
end
|
||||
|
||||
def categories
|
||||
if params["category"].is_a?(Array)
|
||||
params["category"] || []
|
||||
else
|
||||
[params["category"]].compact
|
||||
end
|
||||
end
|
||||
|
||||
def content
|
||||
if params["content"].is_a?(Hash)
|
||||
content_from_hash
|
||||
else
|
||||
params["content"]
|
||||
end
|
||||
end
|
||||
|
||||
def content_from_hash
|
||||
if params["content"]["text"]
|
||||
params["content"]["text"]
|
||||
elsif params["content"]["html"]
|
||||
Kramdown::Document.
|
||||
new(params["content"]["html"], html_to_native: true).
|
||||
to_kramdown
|
||||
end
|
||||
end
|
||||
|
||||
def post_content
|
||||
<<~POST
|
||||
---
|
||||
date: '#{published.rfc3339}'
|
||||
layout: micropost
|
||||
categories:
|
||||
#{categories.map { |category| "- #{category}\n" }.join.strip}
|
||||
---
|
||||
|
||||
#{content}
|
||||
POST
|
||||
end
|
||||
end
|
||||
end
|
||||
31
lib/micropub/post_json_parser.rb
Normal file
31
lib/micropub/post_json_parser.rb
Normal file
@ -0,0 +1,31 @@
|
||||
module Micropub
|
||||
class PostJSONParser
|
||||
def initialize(json)
|
||||
@data = JSON.parse(json)
|
||||
end
|
||||
|
||||
def params
|
||||
{
|
||||
"title" => title,
|
||||
"content" => content,
|
||||
"category" => category,
|
||||
}.compact
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_accessor :data
|
||||
|
||||
def title
|
||||
data.dig("properties", "title", 0)
|
||||
end
|
||||
|
||||
def content
|
||||
data.dig("properties", "content", 0)
|
||||
end
|
||||
|
||||
def category
|
||||
data.dig("properties", "category")
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,5 +1,6 @@
|
||||
require 'sinatra'
|
||||
require 'sinatra/contrib'
|
||||
require 'sinatra/namespace'
|
||||
require 'sinatra/json'
|
||||
|
||||
module Micropub
|
||||
class Webserver < Sinatra::Base
|
||||
@ -55,7 +56,7 @@ module Micropub
|
||||
def valid_token?
|
||||
token = Indieauth::Token.new(endpoints.token_endpoint)
|
||||
|
||||
auth_type, auth_token = request.env["HTTP_AUTHORIZATION"]&.split(" ")
|
||||
_, auth_token = request.env["HTTP_AUTHORIZATION"]&.split(" ")
|
||||
auth_token ||= params["access_token"]
|
||||
|
||||
token.validate(auth_token)
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
require "test_helper"
|
||||
|
||||
require "micropub/models/post_json_parser"
|
||||
require "micropub/post_json_parser"
|
||||
|
||||
describe PostJSONParser do
|
||||
describe Micropub::PostJSONParser do
|
||||
describe "#params" do
|
||||
it "parses basic attributes" do
|
||||
json = <<~JS
|
||||
@ -14,7 +14,7 @@ describe PostJSONParser do
|
||||
}
|
||||
JS
|
||||
|
||||
parser = PostJSONParser.new(json)
|
||||
parser = Micropub::PostJSONParser.new(json)
|
||||
|
||||
_(parser.params).must_equal(
|
||||
{ "content" => "Hello, World!" }
|
||||
@ -33,7 +33,7 @@ describe PostJSONParser do
|
||||
}
|
||||
JS
|
||||
|
||||
parser = PostJSONParser.new(json)
|
||||
parser = Micropub::PostJSONParser.new(json)
|
||||
|
||||
_(parser.params).must_equal(
|
||||
{ "content" => { "text" => "Hello, World!" } }
|
||||
@ -52,7 +52,7 @@ describe PostJSONParser do
|
||||
}
|
||||
JS
|
||||
|
||||
parser = PostJSONParser.new(json)
|
||||
parser = Micropub::PostJSONParser.new(json)
|
||||
|
||||
_(parser.params).must_equal(
|
||||
{ "content" => { "html" => "<p>Hello, World!</p>" } }
|
||||
@ -69,7 +69,7 @@ describe PostJSONParser do
|
||||
}
|
||||
JS
|
||||
|
||||
parser = PostJSONParser.new(json)
|
||||
parser = Micropub::PostJSONParser.new(json)
|
||||
|
||||
_(parser.params).must_equal(
|
||||
{ "category" => ["test"] }
|
||||
|
||||
@ -1,23 +1,23 @@
|
||||
require "test_helper"
|
||||
|
||||
require "micropub/models/post"
|
||||
require "micropub/post"
|
||||
|
||||
describe Post do
|
||||
describe Micropub::Post do
|
||||
describe "#id" do
|
||||
it "parameterizes the title" do
|
||||
post = Post.new("title" => "My amazing post")
|
||||
post = Micropub::Post.new("title" => "My amazing post")
|
||||
|
||||
_(post.id).must_equal "my-amazing-post"
|
||||
end
|
||||
|
||||
it "parameterizes the post content if there's no title" do
|
||||
post = Post.new("content" => "Hello, World!")
|
||||
post = Micropub::Post.new("content" => "Hello, World!")
|
||||
|
||||
_(post.id).must_equal "hello-world"
|
||||
end
|
||||
|
||||
it "parameterizes the truncated post content" do
|
||||
post = Post.new(
|
||||
post = Micropub::Post.new(
|
||||
"content" => <<~CONTENT
|
||||
Your bones don't break, mine do. That's clear. Your cells react to
|
||||
bacteria and viruses differently than mine. You don't get sick, I do.
|
||||
@ -34,13 +34,13 @@ describe Post do
|
||||
|
||||
describe "#title" do
|
||||
it "returns the title" do
|
||||
post = Post.new("title" => "My great post")
|
||||
post = Micropub::Post.new("title" => "My great post")
|
||||
|
||||
_(post.title).must_equal "My great post"
|
||||
end
|
||||
|
||||
it "returns the content if there's no title" do
|
||||
post = Post.new("content" => "My money's in that office, right?")
|
||||
post = Micropub::Post.new("content" => "My money's in that office, right?")
|
||||
|
||||
_(post.title).must_equal "My money's in that office, right?"
|
||||
end
|
||||
@ -48,7 +48,7 @@ describe Post do
|
||||
|
||||
describe "#path" do
|
||||
it "returns the new path of the post" do
|
||||
post = Post.new(
|
||||
post = Micropub::Post.new(
|
||||
"published" => "2019/02/09",
|
||||
"content" => "My great post.",
|
||||
)
|
||||
@ -59,13 +59,13 @@ describe Post do
|
||||
|
||||
describe "#truncated_content" do
|
||||
it "returns the content if it's six words or less" do
|
||||
post = Post.new("content" => "My money's in that office, right?")
|
||||
post = Micropub::Post.new("content" => "My money's in that office, right?")
|
||||
|
||||
_(post.truncated_content).must_equal "My money's in that office, right?"
|
||||
end
|
||||
|
||||
it "truncates the content to six words" do
|
||||
post = Post.new(
|
||||
post = Micropub::Post.new(
|
||||
"content" => <<~CONTENT
|
||||
Your bones don't break, mine do. That's clear. Your cells react to
|
||||
bacteria and viruses differently than mine. You don't get sick, I do.
|
||||
@ -80,7 +80,7 @@ describe Post do
|
||||
end
|
||||
|
||||
it "adds an ellipsis if the content is truncated in a sentence" do
|
||||
post = Post.new(
|
||||
post = Micropub::Post.new(
|
||||
"content" => "The path of the righteous man is beset on all sides by "\
|
||||
"the iniquities of the selfish and the tyranny of evil "\
|
||||
"men.",
|
||||
@ -92,13 +92,13 @@ describe Post do
|
||||
|
||||
describe "#published" do
|
||||
it "returns the parsed date" do
|
||||
post = Post.new("published" => "2019-11-01")
|
||||
post = Micropub::Post.new("published" => "2019-11-01")
|
||||
|
||||
_(post.published).must_equal DateTime.new(2019, 11, 1)
|
||||
end
|
||||
|
||||
it "returns todays date if no date is passed" do
|
||||
post = Post.new
|
||||
post = Micropub::Post.new
|
||||
|
||||
_(post.published.to_s).must_equal DateTime.now.to_s
|
||||
end
|
||||
@ -106,19 +106,19 @@ describe Post do
|
||||
|
||||
describe "#categories" do
|
||||
it "returns the list of categories" do
|
||||
post = Post.new("category" => ["shows", "pilot"])
|
||||
post = Micropub::Post.new("category" => ["shows", "pilot"])
|
||||
|
||||
_(post.categories).must_equal ["shows", "pilot"]
|
||||
end
|
||||
|
||||
it "returns an empty array if there are no categories" do
|
||||
post = Post.new
|
||||
post = Micropub::Post.new
|
||||
|
||||
_(post.categories).must_equal []
|
||||
end
|
||||
|
||||
it "returns an array with a single category" do
|
||||
post = Post.new("category" => "fried-chicken")
|
||||
post = Micropub::Post.new("category" => "fried-chicken")
|
||||
|
||||
_(post.categories).must_equal ["fried-chicken"]
|
||||
end
|
||||
@ -126,25 +126,25 @@ describe Post do
|
||||
|
||||
describe "#content" do
|
||||
it "returns the content" do
|
||||
post = Post.new("content" => "Hello, World!")
|
||||
post = Micropub::Post.new("content" => "Hello, World!")
|
||||
|
||||
_(post.content).must_equal "Hello, World!"
|
||||
end
|
||||
|
||||
it "accepts content in an object" do
|
||||
post = Post.new("content" => { "text" => "Hello, World!" })
|
||||
post = Micropub::Post.new("content" => { "text" => "Hello, World!" })
|
||||
|
||||
_(post.content).must_equal "Hello, World!"
|
||||
end
|
||||
|
||||
it "converts HTML content to Markdown" do
|
||||
post = Post.new("content" => { "html" => "<p>Hello, World!</p>" })
|
||||
post = Micropub::Post.new("content" => { "html" => "<p>Hello, World!</p>" })
|
||||
|
||||
_(post.content.strip).must_equal "Hello, World!"
|
||||
end
|
||||
|
||||
it "converts complex HTML content to Markdown" do
|
||||
post = Post.new(
|
||||
post = Micropub::Post.new(
|
||||
"content" => {
|
||||
"html" => <<~HTML,
|
||||
<p>This is a test post, with some lists and stuff.</p>
|
||||
@ -186,7 +186,7 @@ describe Post do
|
||||
|
||||
describe "#post_content" do
|
||||
it "returns a post formatted for hugo" do
|
||||
post = Post.new(
|
||||
post = Micropub::Post.new(
|
||||
"content" => "Hallo, Earth!",
|
||||
"published" => "2019-11-12",
|
||||
"category" => ["one", "two", "three"],
|
||||
|
||||
Loading…
Reference in New Issue
Block a user