From 02ade372650b8ecca9aceb8150aabab67d3b5d0e Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Wed, 21 Jun 2023 21:26:01 +1000 Subject: [PATCH] Send bookmarks to Raindrop --- app/commands/posts/create_bookmark.rb | 4 +++- app/templates/more/index.html.slim | 5 +++-- config/providers/syndication.rb | 2 +- lib/adamantium/syndication/raindrop.rb | 25 ++++++++++++++++++++----- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/app/commands/posts/create_bookmark.rb b/app/commands/posts/create_bookmark.rb index 46e2a22..7ce34a3 100644 --- a/app/commands/posts/create_bookmark.rb +++ b/app/commands/posts/create_bookmark.rb @@ -5,7 +5,8 @@ module Adamantium module Posts class CreateBookmark < Command include Deps["repos.post_repo", - syndicate: "commands.posts.syndicate" + syndicate: "commands.posts.syndicate", + raindrop: "syndication.raindrop" ] include Dry::Monads[:result] @@ -14,6 +15,7 @@ module Adamantium created_bookmark = post_repo.create(bookmark) syndicate.call(created_bookmark.id, bookmark) + raindrop.call(post: created_bookmark) Success(created_bookmark) end diff --git a/app/templates/more/index.html.slim b/app/templates/more/index.html.slim index 0965e33..f8f62a2 100644 --- a/app/templates/more/index.html.slim +++ b/app/templates/more/index.html.slim @@ -3,14 +3,15 @@ div class="mb-12 max-w-prose mx-auto text-gray-800 dark:text-gray-200" h2 class="text-xl" Explore posts - div class="grid grid-cols-4 grid-flow-col gap-2 mb-6" + div class="grid grid-cols-2 gap-2 mb-6" a class="block p-1 border border-blue-200 bg-blue-200 text-blue-900 hover:bg-blue-300 text-center rounded-lg" href="/tags" 🔖 By tag / a class="block p-1 border border-blue-200 bg-blue-300 text-blue-900 hover:bg-blue-200 text-center rounded-lg" href="/years" 🗓️ By year a class="block p-1 border border-blue-200 bg-blue-200 text-blue-900 hover:bg-blue-300 text-center rounded-lg" href="/posts" 🪧 All posts + a class="block p-1 border border-blue-200 bg-blue-200 text-blue-900 hover:bg-blue-300 text-center rounded-lg" href="/tagged/weekly" 🔄 Week posts h2 class="text-xl" Explore everything else - div class="grid grid-cols-4 grid-flow-col gap-2 mb-6" + div class="grid grid-cols-2 gap-2 mb-6" a class="block p-1 border border-blue-200 bg-blue-200 text-blue-900 hover:bg-blue-300 text-center rounded-lg" href="/colophon" 🧱 Colophon a class="block p-1 border border-blue-200 bg-blue-200 text-blue-900 hover:bg-blue-300 text-center rounded-lg" href="/hikes" 🥾 Hikes a class="block p-1 border border-blue-200 bg-blue-200 text-blue-900 hover:bg-blue-300 text-center rounded-lg" href="/movies" 🍿 Movies diff --git a/config/providers/syndication.rb b/config/providers/syndication.rb index 6a27d96..550c9a4 100644 --- a/config/providers/syndication.rb +++ b/config/providers/syndication.rb @@ -1,7 +1,7 @@ Hanami.app.register_provider :syndication, namespace: true do start do register "mastodon", Adamantium::Syndication::Mastodon.new - register "raindrop", Adamantium::Syndication::Raindrop.new(api_key: target["settings"].raindrop_api_key) + register "raindrop", Adamantium::Syndication::Raindrop.new(api_key: "Bearer #{target["settings"].raindrop_api_key}") register "dayone", Adamantium::Syndication::Dayone.new( username: target["settings"].smtp_username, password: target["settings"].smtp_password, diff --git a/lib/adamantium/syndication/raindrop.rb b/lib/adamantium/syndication/raindrop.rb index 244229b..89e5e66 100644 --- a/lib/adamantium/syndication/raindrop.rb +++ b/lib/adamantium/syndication/raindrop.rb @@ -11,12 +11,27 @@ module Adamantium end def call(post:) - if response.code == 200 - result = JSON.parse(response, symbolize_name: true) - result[:item] - Success(href) + uri = URI('https://api.raindrop.io/rest/v1/raindrop') + + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_PEER + dict = { + "title" => post[:name], + "link" => post[:url] + } + body = JSON.dump(dict) + req = Net::HTTP::Post.new(uri) + req.add_field "Authorization", api_key + req.add_field "Content-Type", "application/json; charset=utf-8" + req.body = body + + response = http.request(req) + + if response.code.to_s == "200" + Success() else - Failure(:failed_to_post_to_pinboard) + Failure(:failed_to_post_to_raindrop) end end end