Class: Joplin::Note

Inherits:
Object
  • Object
show all
Defined in:
lib/joplin.rb

Defined Under Namespace

Classes: NotFound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, parent_id: nil) ⇒ Note

Returns a new instance of Note.



105
106
107
108
109
110
111
112
# File 'lib/joplin.rb', line 105

def initialize(id: nil, parent_id: nil)
  @id = id
  @parent_id = parent_id
  return unless id

  url = "#{Joplin.uri}/notes/#{id}?token=#{Joplin.token}&fields=title,body,id,parent_id"
  parse HTTP.get url
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



102
103
104
# File 'lib/joplin.rb', line 102

def body
  @body
end

#idObject (readonly)

Returns the value of attribute id.



103
104
105
# File 'lib/joplin.rb', line 103

def id
  @id
end

#parent_idObject

Returns the value of attribute parent_id.



102
103
104
# File 'lib/joplin.rb', line 102

def parent_id
  @parent_id
end

#titleObject

Returns the value of attribute title.



102
103
104
# File 'lib/joplin.rb', line 102

def title
  @title
end

Class Method Details

.allObject



166
167
168
169
170
171
172
173
# File 'lib/joplin.rb', line 166

def self.all
  url = "#{Joplin.uri}/notes/?token=#{Joplin.token}&fields=id"
  res = HTTP.get url
  parsed = JSON.parse res.body
  parsed.map do |note|
    Note.new note['id']
  end
end

Instance Method Details

#delete!Object



175
176
177
178
179
# File 'lib/joplin.rb', line 175

def delete!
  url = "#{Joplin.uri}/notes/#{@id}?token=#{Joplin.token}"
  response = HTTP.delete url
  response.status == 200
end

#prepare_body_for_writing_by_fixing_resourcesObject



126
127
128
129
130
# File 'lib/joplin.rb', line 126

def prepare_body_for_writing_by_fixing_resources
  prepared = String(body)
  prepared.each_line do |line|
  end
end

#resourcesObject

Raises:



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/joplin.rb', line 114

def resources
  url = "#{Joplin.uri}/notes/#{id}/resources?token=#{Joplin.token}&fields=id,filename"
  response = HTTP.get url
  raise Error, "#{response}" if response.code != 200

  parsed = JSON.parse response.body
  parsed['items'].map do |resource_data|
    id = resource_data['id']
    Resource.new id
  end
end

#save!Object



151
152
153
154
155
156
157
158
159
160
# File 'lib/joplin.rb', line 151

def save!
  if @id
    url = "#{Joplin.uri}/notes/#{@id}?token=#{Joplin.token}"
    response = HTTP.put url, body: to_json
    return response.status == 200
  end

  url = "#{Joplin.uri}/notes/?token=#{Joplin.token}"
  parse HTTP.post url, body: to_json
end

#to_json(*_args) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/joplin.rb', line 143

def to_json(*_args)
  {
    title:,
    body:,
    parent_id:
  }.to_json
end

#to_sObject



162
163
164
# File 'lib/joplin.rb', line 162

def to_s
  %(id: #{id} title: #{title} parent_id: #{parent_id} body: #{body})
end

#write(path = nil) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/joplin.rb', line 132

def write(path = nil)
  dir = path || title
  FileUtils.mkdir_p "#{dir}/resources"
  body_to_write = String(body) # make a copy
  resources.each do |resource|
    resource.write "#{dir}/resources/#{resource.id}"
    body_to_write.gsub!(%r{:/#{resource.id}}, "./resources/#{resource.id}")
  end
  IO.write "#{dir}/#{title}.md", body_to_write
end