commit 406b641391aa92bea883399ab0dd23267092b10d
parent 9235ad1df7540456fb4cd3acf222568b629818e8
Author: Alex Balgavy <a.balgavy@gmail.com>
Date: Sun, 5 Apr 2020 21:35:27 +0200
joplin interface in ruby
Former-commit-id: 6f7f43d735ab09f517a0fedcc3049296e8000f66
Diffstat:
1 file changed, 56 insertions(+), 0 deletions(-)
diff --git a/scripts/joplin-interface b/scripts/joplin-interface
@@ -0,0 +1,56 @@
+#!/usr/bin/env ruby
+require 'faraday'
+require 'json'
+
+class JoplinApi
+ def get endpoint, params=nil, headers=nil
+ if params
+ resp = Faraday.get(@uri+endpoint, {token: @token}.merge(params), headers)
+ else
+ resp = Faraday.get(@uri+endpoint, nil, headers)
+ end
+
+ if resp.success?
+ if resp.body
+ begin
+ JSON.parse(resp.body)
+ rescue JSON::ParserError
+ {}
+ end
+ else
+ nil
+ end
+ end
+ end
+
+ def get_notes
+ notes = get("/folders/#{@notebook}/notes")
+ notes.map do |note|
+ { title: note["title"],
+ id: note["id"],
+ body: (get("/notes/#{note["id"]}", {fields: "body"}))["body"]
+ }
+ end
+ end
+
+ def initialize notebook
+ @token = ENV["JOPLIN_API_KEY"]
+ @uri = "http://localhost:41184"
+
+ parts = notebook.split "/"
+ folders = get("/folders")
+ current = 0
+ part = parts[current]
+ notebook = folders.find { |x| x["title"] == part }
+
+ until part == parts.last
+ folders = notebook["children"]
+ current += 1
+ part = parts[current]
+ notebook = folders.find { |x| x["title"] == part }
+ end
+
+ @notebook = notebook["id"]
+
+ end
+end