joplin-interface (1184B)
1 #!/usr/bin/env ruby 2 require 'faraday' 3 require 'json' 4 5 class JoplinApi 6 def get endpoint, params=nil, headers=nil 7 if params 8 resp = Faraday.get(@uri+endpoint, {token: @token}.merge(params), headers) 9 else 10 resp = Faraday.get(@uri+endpoint, nil, headers) 11 end 12 13 if resp.success? 14 if resp.body 15 begin 16 JSON.parse(resp.body) 17 rescue JSON::ParserError 18 {} 19 end 20 else 21 nil 22 end 23 end 24 end 25 26 def get_notes 27 notes = get("/folders/#{@notebook}/notes") 28 notes.map do |note| 29 { title: note["title"], 30 id: note["id"], 31 body: (get("/notes/#{note["id"]}", {fields: "body"}))["body"] 32 } 33 end 34 end 35 36 def initialize notebook 37 @token = ENV["JOPLIN_API_KEY"] 38 @uri = "http://localhost:41184" 39 40 parts = notebook.split "/" 41 folders = get("/folders") 42 current = 0 43 part = parts[current] 44 notebook = folders.find { |x| x["title"] == part } 45 46 until part == parts.last 47 folders = notebook["children"] 48 current += 1 49 part = parts[current] 50 notebook = folders.find { |x| x["title"] == part } 51 end 52 53 @notebook = notebook["id"] 54 55 end 56 end