diff --git a/app/commands/posts/send_webmentions.rb b/app/commands/posts/send_webmentions.rb new file mode 100644 index 0000000..494a734 --- /dev/null +++ b/app/commands/posts/send_webmentions.rb @@ -0,0 +1,23 @@ +require "httparty" + +module Adamantium + module Commands + module Posts + class SendWebmentions + include Deps["settings", "post_utilities.link_finder"] + + def call(post_content:, post_url:) + source = post_url + links = link_finder.call(post_content) + links.each do |target| + HTTParty.post(settings.webmention_service, { + token: settings.webmention_token, + source: source, + target: target + }) + end + end + end + end + end +end diff --git a/config/providers/post_utilities.rb b/config/providers/post_utilities.rb index da44ad1..b99c9c0 100644 --- a/config/providers/post_utilities.rb +++ b/config/providers/post_utilities.rb @@ -3,5 +3,6 @@ Hanami.app.register_provider :post_utilities, namespace: true do start do register "slugify", Adamantium::SlugCreator.new + register "link_finder", Adamantium::LinkFinder.new end end diff --git a/spec/adamantium/unit/link_finder_spec.rb b/spec/adamantium/unit/link_finder_spec.rb new file mode 100644 index 0000000..2c7f9e4 --- /dev/null +++ b/spec/adamantium/unit/link_finder_spec.rb @@ -0,0 +1,11 @@ +require "spec_helper" + +RSpec.describe Adamantium::LinkFinder do + subject { described_class.new } + + it "finds allowed links in a string" do + text = "Check out some of these cool websites: https://google.com https://example.com or even http://twitter.com and https://pinboard.in!" + + expect(subject.call(text)).to eq ["https://example.com", "https://pinboard.in"] + end +end