From 8590cf7be35a6cf5285399d86842d90d4a714ec9 Mon Sep 17 00:00:00 2001 From: Daniel Barber Date: Sun, 17 Nov 2019 11:32:18 -0500 Subject: [PATCH] Re-org and solve config_file warnings --- lib/micropub.rb | 4 +- lib/micropub/models/post.rb | 81 ------------------ lib/micropub/models/post_json_parser.rb | 29 ------- lib/micropub/post.rb | 83 +++++++++++++++++++ lib/micropub/post_json_parser.rb | 31 +++++++ lib/micropub/webserver.rb | 5 +- .../micropub/models/post_json_parser_test.rb | 12 +-- test/lib/micropub/models/post_test.rb | 42 +++++----- 8 files changed, 146 insertions(+), 141 deletions(-) delete mode 100644 lib/micropub/models/post.rb delete mode 100644 lib/micropub/models/post_json_parser.rb create mode 100644 lib/micropub/post.rb create mode 100644 lib/micropub/post_json_parser.rb diff --git a/lib/micropub.rb b/lib/micropub.rb index 778ca09..b306359 100644 --- a/lib/micropub.rb +++ b/lib/micropub.rb @@ -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 diff --git a/lib/micropub/models/post.rb b/lib/micropub/models/post.rb deleted file mode 100644 index 9109c78..0000000 --- a/lib/micropub/models/post.rb +++ /dev/null @@ -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 diff --git a/lib/micropub/models/post_json_parser.rb b/lib/micropub/models/post_json_parser.rb deleted file mode 100644 index 6377690..0000000 --- a/lib/micropub/models/post_json_parser.rb +++ /dev/null @@ -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 diff --git a/lib/micropub/post.rb b/lib/micropub/post.rb new file mode 100644 index 0000000..65cc74f --- /dev/null +++ b/lib/micropub/post.rb @@ -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 diff --git a/lib/micropub/post_json_parser.rb b/lib/micropub/post_json_parser.rb new file mode 100644 index 0000000..66d5eab --- /dev/null +++ b/lib/micropub/post_json_parser.rb @@ -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 diff --git a/lib/micropub/webserver.rb b/lib/micropub/webserver.rb index 45f8ad9..1d79ca4 100644 --- a/lib/micropub/webserver.rb +++ b/lib/micropub/webserver.rb @@ -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) diff --git a/test/lib/micropub/models/post_json_parser_test.rb b/test/lib/micropub/models/post_json_parser_test.rb index 2c147fb..9583896 100644 --- a/test/lib/micropub/models/post_json_parser_test.rb +++ b/test/lib/micropub/models/post_json_parser_test.rb @@ -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" => "

Hello, World!

" } } @@ -69,7 +69,7 @@ describe PostJSONParser do } JS - parser = PostJSONParser.new(json) + parser = Micropub::PostJSONParser.new(json) _(parser.params).must_equal( { "category" => ["test"] } diff --git a/test/lib/micropub/models/post_test.rb b/test/lib/micropub/models/post_test.rb index 9337101..b7281e8 100644 --- a/test/lib/micropub/models/post_test.rb +++ b/test/lib/micropub/models/post_test.rb @@ -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" => "

Hello, World!

" }) + post = Micropub::Post.new("content" => { "html" => "

Hello, World!

" }) _(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,

This is a test post, with some lists and stuff.

@@ -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"],