diff --git a/Gemfile b/Gemfile index 7715492..117fdb0 100644 --- a/Gemfile +++ b/Gemfile @@ -13,3 +13,5 @@ gem "faraday" gem "github_api" gem "indieweb-endpoints" + +gem "minitest" diff --git a/Gemfile.lock b/Gemfile.lock index 2095daa..1920fd9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -44,6 +44,7 @@ GEM link-header-parser (0.2.0) absolutely (~> 3.0) mini_portile2 (2.4.0) + minitest (5.13.0) multi_json (1.14.1) multi_xml (0.6.0) multipart-post (2.1.1) @@ -90,6 +91,7 @@ DEPENDENCIES faraday github_api indieweb-endpoints + minitest puma sinatra sinatra-contrib diff --git a/lib/micropub/models/post.rb b/lib/micropub/models/post.rb index 025f24c..36893fb 100644 --- a/lib/micropub/models/post.rb +++ b/lib/micropub/models/post.rb @@ -1,14 +1,12 @@ +require "date" + class Post attr_accessor :params - def initialize(params) + def initialize(params = {}) @params = params end - def path - "/blog/#{published.strftime("%Y/%m/%d")}/#{id}" - end - def id title. downcase. @@ -21,6 +19,10 @@ class Post params["title"] || truncated_content end + def path + "/blog/#{published.strftime("%Y/%m/%d")}/#{id}" + end + def truncated_content content[0..content.index(".")]. split(" "). diff --git a/test/lib/micropub/models/post_test.rb b/test/lib/micropub/models/post_test.rb new file mode 100644 index 0000000..f278e18 --- /dev/null +++ b/test/lib/micropub/models/post_test.rb @@ -0,0 +1,156 @@ +require "minitest/autorun" +require_relative "../../../../lib/micropub/models/post" + +describe Post do + describe "#id" do + it "parameterizes the title" do + post = 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.id).must_equal "hello-world" + end + + it "parameterizes the truncated post content" do + post = 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. + That's also clear. But for some reason, you and I react the exact + same way to water. We swallow it too fast, we choke. We get some in + our lungs, we drown. However unreal it may seem, we are connected, + you and I. We're on the same curve, just on opposite ends. + CONTENT + ) + + _(post.id).must_equal "your-bones-don-t-break-mine-do" + end + end + + describe "#title" do + it "returns the title" do + post = 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.title).must_equal "My money's in that office, right?" + end + end + + describe "#path" do + it "returns the new path of the post" do + post = Post.new( + "published" => "2019/02/09", + "content" => "My great post.", + ) + + _(post.path).must_equal "/blog/2019/02/09/my-great-post" + end + end + + 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.truncated_content).must_equal "My money's in that office, right?" + end + + it "truncates the content to six words" do + post = 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. + That's also clear. But for some reason, you and I react the exact + same way to water. We swallow it too fast, we choke. We get some in + our lungs, we drown. However unreal it may seem, we are connected, + you and I. We're on the same curve, just on opposite ends. + CONTENT + ) + + _(post.truncated_content).must_equal "Your bones don't break, mine do." + end + + it "adds an ellipsis if the content is truncated in a sentence" do + post = 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.", + ) + + _(post.truncated_content).must_equal "The path of the righteous man…" + end + end + + describe "#published" do + it "returns the parsed date" do + post = 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.published.to_s).must_equal DateTime.now.to_s + end + end + + describe "#categories" do + it "returns the list of categories" do + post = 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.categories).must_equal [] + end + + it "returns an array with a single category" do + post = Post.new("category" => "fried-chicken") + + _(post.categories).must_equal ["fried-chicken"] + end + end + + describe "#content" do + it "returns the content" do + post = Post.new("content" => "Hello, World!") + + _(post.content).must_equal "Hello, World!" + end + end + + describe "#post_content" do + it "returns a post formatted for hugo" do + post = Post.new( + "content" => "Hallo, Earth!", + "published" => "2019-11-12", + "category" => ["one", "two", "three"], + ) + + _(post.post_content).must_equal <<~POST + --- + date: '2019-11-12T00:00:00+00:00' + layout: micropost + categories: + - one + - two + - three + --- + + Hallo, Earth! + POST + end + end +end