class Joplin::Note

Attributes

body[RW]
id[R]
title[RW]

Public Class Methods

all() click to toggle source
# File lib/joplin.rb, line 125
def self.all
    url = "#{Joplin::uri}/notes/?token=#{Joplin::token}&fields=id"
    res = Faraday.get url
    parsed = JSON.parse res.body
    parsed.map do |note|
      Note.new note['id']
    end
end
new(id=nil) click to toggle source
# File lib/joplin.rb, line 82
def initialize(id=nil)

  @id = id
  if id
    url = "#{Joplin::uri}/notes/#{id}?token=#{Joplin::token}&fields=title,body,id"
    parse Faraday.get url
  end
end

Public Instance Methods

resources() click to toggle source
# File lib/joplin.rb, line 91
def resources
    url = "#{Joplin::uri}/notes/#{id}/resources?token=#{Joplin::token}&fields=id"
    res = Faraday.get url
    parsed = JSON.parse res.body
    parsed.map do |resource_data|
      id = resource_data['id']
      Resource.new id
    end
end
save!() click to toggle source
# File lib/joplin.rb, line 108
def save!
  if @id
    url = "#{Joplin::uri}/notes/#{@id}?token=#{Joplin::token}"
    response = Faraday.put url, self.to_json
    return response.status == 200
  end

    url = "#{Joplin::uri}/notes/?token=#{Joplin::token}"
    parse Faraday.post url, self.to_json
end
to_json() click to toggle source
# File lib/joplin.rb, line 101
def to_json
  {
    title: @title,
    body: @body
  }.to_json
end
to_s() click to toggle source
# File lib/joplin.rb, line 119
    def to_s
      """id: #{self.id}
title: #{self.title}
body: #{self.body}"""
    end

Private Instance Methods

parse(response) click to toggle source
# File lib/joplin.rb, line 135
def parse response
  if response.body.empty?
    raise "No note found with id #{@id}"
  end
  note = JSON.parse response.body
  if response.status != 200
    raise Error.new note["error"]
  end
  @body = note['body']
  @title = note['title']
  @id = note["id"]
end