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
|
module Micropub
|
||||||
autoload :Github, File.expand_path('micropub/github.rb', __dir__)
|
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,
|
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__)
|
autoload :Webserver, File.expand_path('micropub/webserver.rb', __dir__)
|
||||||
end
|
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'
|
||||||
require 'sinatra/contrib'
|
require 'sinatra/namespace'
|
||||||
|
require 'sinatra/json'
|
||||||
|
|
||||||
module Micropub
|
module Micropub
|
||||||
class Webserver < Sinatra::Base
|
class Webserver < Sinatra::Base
|
||||||
@ -55,7 +56,7 @@ module Micropub
|
|||||||
def valid_token?
|
def valid_token?
|
||||||
token = Indieauth::Token.new(endpoints.token_endpoint)
|
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"]
|
auth_token ||= params["access_token"]
|
||||||
|
|
||||||
token.validate(auth_token)
|
token.validate(auth_token)
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
require "test_helper"
|
require "test_helper"
|
||||||
|
|
||||||
require "micropub/models/post_json_parser"
|
require "micropub/post_json_parser"
|
||||||
|
|
||||||
describe PostJSONParser do
|
describe Micropub::PostJSONParser do
|
||||||
describe "#params" do
|
describe "#params" do
|
||||||
it "parses basic attributes" do
|
it "parses basic attributes" do
|
||||||
json = <<~JS
|
json = <<~JS
|
||||||
@ -14,7 +14,7 @@ describe PostJSONParser do
|
|||||||
}
|
}
|
||||||
JS
|
JS
|
||||||
|
|
||||||
parser = PostJSONParser.new(json)
|
parser = Micropub::PostJSONParser.new(json)
|
||||||
|
|
||||||
_(parser.params).must_equal(
|
_(parser.params).must_equal(
|
||||||
{ "content" => "Hello, World!" }
|
{ "content" => "Hello, World!" }
|
||||||
@ -33,7 +33,7 @@ describe PostJSONParser do
|
|||||||
}
|
}
|
||||||
JS
|
JS
|
||||||
|
|
||||||
parser = PostJSONParser.new(json)
|
parser = Micropub::PostJSONParser.new(json)
|
||||||
|
|
||||||
_(parser.params).must_equal(
|
_(parser.params).must_equal(
|
||||||
{ "content" => { "text" => "Hello, World!" } }
|
{ "content" => { "text" => "Hello, World!" } }
|
||||||
@ -52,7 +52,7 @@ describe PostJSONParser do
|
|||||||
}
|
}
|
||||||
JS
|
JS
|
||||||
|
|
||||||
parser = PostJSONParser.new(json)
|
parser = Micropub::PostJSONParser.new(json)
|
||||||
|
|
||||||
_(parser.params).must_equal(
|
_(parser.params).must_equal(
|
||||||
{ "content" => { "html" => "<p>Hello, World!</p>" } }
|
{ "content" => { "html" => "<p>Hello, World!</p>" } }
|
||||||
@ -69,7 +69,7 @@ describe PostJSONParser do
|
|||||||
}
|
}
|
||||||
JS
|
JS
|
||||||
|
|
||||||
parser = PostJSONParser.new(json)
|
parser = Micropub::PostJSONParser.new(json)
|
||||||
|
|
||||||
_(parser.params).must_equal(
|
_(parser.params).must_equal(
|
||||||
{ "category" => ["test"] }
|
{ "category" => ["test"] }
|
||||||
|
|||||||
@ -1,23 +1,23 @@
|
|||||||
require "test_helper"
|
require "test_helper"
|
||||||
|
|
||||||
require "micropub/models/post"
|
require "micropub/post"
|
||||||
|
|
||||||
describe Post do
|
describe Micropub::Post do
|
||||||
describe "#id" do
|
describe "#id" do
|
||||||
it "parameterizes the title" 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"
|
_(post.id).must_equal "my-amazing-post"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "parameterizes the post content if there's no title" do
|
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"
|
_(post.id).must_equal "hello-world"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "parameterizes the truncated post content" do
|
it "parameterizes the truncated post content" do
|
||||||
post = Post.new(
|
post = Micropub::Post.new(
|
||||||
"content" => <<~CONTENT
|
"content" => <<~CONTENT
|
||||||
Your bones don't break, mine do. That's clear. Your cells react to
|
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.
|
bacteria and viruses differently than mine. You don't get sick, I do.
|
||||||
@ -34,13 +34,13 @@ describe Post do
|
|||||||
|
|
||||||
describe "#title" do
|
describe "#title" do
|
||||||
it "returns the 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"
|
_(post.title).must_equal "My great post"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns the content if there's no title" do
|
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?"
|
_(post.title).must_equal "My money's in that office, right?"
|
||||||
end
|
end
|
||||||
@ -48,7 +48,7 @@ describe Post do
|
|||||||
|
|
||||||
describe "#path" do
|
describe "#path" do
|
||||||
it "returns the new path of the post" do
|
it "returns the new path of the post" do
|
||||||
post = Post.new(
|
post = Micropub::Post.new(
|
||||||
"published" => "2019/02/09",
|
"published" => "2019/02/09",
|
||||||
"content" => "My great post.",
|
"content" => "My great post.",
|
||||||
)
|
)
|
||||||
@ -59,13 +59,13 @@ describe Post do
|
|||||||
|
|
||||||
describe "#truncated_content" do
|
describe "#truncated_content" do
|
||||||
it "returns the content if it's six words or less" 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?"
|
_(post.truncated_content).must_equal "My money's in that office, right?"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "truncates the content to six words" do
|
it "truncates the content to six words" do
|
||||||
post = Post.new(
|
post = Micropub::Post.new(
|
||||||
"content" => <<~CONTENT
|
"content" => <<~CONTENT
|
||||||
Your bones don't break, mine do. That's clear. Your cells react to
|
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.
|
bacteria and viruses differently than mine. You don't get sick, I do.
|
||||||
@ -80,7 +80,7 @@ describe Post do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "adds an ellipsis if the content is truncated in a sentence" do
|
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 "\
|
"content" => "The path of the righteous man is beset on all sides by "\
|
||||||
"the iniquities of the selfish and the tyranny of evil "\
|
"the iniquities of the selfish and the tyranny of evil "\
|
||||||
"men.",
|
"men.",
|
||||||
@ -92,13 +92,13 @@ describe Post do
|
|||||||
|
|
||||||
describe "#published" do
|
describe "#published" do
|
||||||
it "returns the parsed date" 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)
|
_(post.published).must_equal DateTime.new(2019, 11, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns todays date if no date is passed" do
|
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
|
_(post.published.to_s).must_equal DateTime.now.to_s
|
||||||
end
|
end
|
||||||
@ -106,19 +106,19 @@ describe Post do
|
|||||||
|
|
||||||
describe "#categories" do
|
describe "#categories" do
|
||||||
it "returns the list of 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"]
|
_(post.categories).must_equal ["shows", "pilot"]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns an empty array if there are no categories" do
|
it "returns an empty array if there are no categories" do
|
||||||
post = Post.new
|
post = Micropub::Post.new
|
||||||
|
|
||||||
_(post.categories).must_equal []
|
_(post.categories).must_equal []
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns an array with a single category" do
|
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"]
|
_(post.categories).must_equal ["fried-chicken"]
|
||||||
end
|
end
|
||||||
@ -126,25 +126,25 @@ describe Post do
|
|||||||
|
|
||||||
describe "#content" do
|
describe "#content" do
|
||||||
it "returns the 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!"
|
_(post.content).must_equal "Hello, World!"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "accepts content in an object" do
|
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!"
|
_(post.content).must_equal "Hello, World!"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "converts HTML content to Markdown" do
|
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!"
|
_(post.content.strip).must_equal "Hello, World!"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "converts complex HTML content to Markdown" do
|
it "converts complex HTML content to Markdown" do
|
||||||
post = Post.new(
|
post = Micropub::Post.new(
|
||||||
"content" => {
|
"content" => {
|
||||||
"html" => <<~HTML,
|
"html" => <<~HTML,
|
||||||
<p>This is a test post, with some lists and stuff.</p>
|
<p>This is a test post, with some lists and stuff.</p>
|
||||||
@ -186,7 +186,7 @@ describe Post do
|
|||||||
|
|
||||||
describe "#post_content" do
|
describe "#post_content" do
|
||||||
it "returns a post formatted for hugo" do
|
it "returns a post formatted for hugo" do
|
||||||
post = Post.new(
|
post = Micropub::Post.new(
|
||||||
"content" => "Hallo, Earth!",
|
"content" => "Hallo, Earth!",
|
||||||
"published" => "2019-11-12",
|
"published" => "2019-11-12",
|
||||||
"category" => ["one", "two", "three"],
|
"category" => ["one", "two", "three"],
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user