Class: Joplin::Notebook

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil) ⇒ Notebook

Returns a new instance of Notebook.



198
199
200
201
202
203
204
205
# File 'lib/joplin.rb', line 198

def initialize(id = nil)
  @id = id
  return unless id

  url = "#{Joplin.uri}/folders/#{id}?token=#{Joplin.token}"
  res = HTTP.get url
  parsed = JSON.parse res.body
end

Class Method Details

.allObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/joplin.rb', line 230

def self.all
  notebooks = []
  page = 1

  loop do
    url = "#{Joplin.uri}/folders?token=#{Joplin.token}&page=#{page}"
    response = HTTP.get(url)

    raise Error, "Failed to fetch notebooks: #{response}" if response.status != 200

    data = JSON.parse(response.body)
    items = data['items']

    break if items.empty?

    # Collect Notebook instances
    items.each { |notebook_data| notebooks << Notebook.new(notebook_data['id']) }

    page += 1
  end

  notebooks
end

Instance Method Details

#notesObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/joplin.rb', line 207

def notes
  notes = []
  page = 1

  loop do
    url = "#{Joplin.uri}/folders/#{@id}/notes?token=#{Joplin.token}&page=#{page}"
    response = HTTP.get(url)

    raise Error, "Failed to fetch notes: #{response}" if response.status != 200

    data = JSON.parse(response.body)

    items = data['items']
    items.each { |note_data| notes << Joplin::Note.new(id: note_data['id']) }

    break unless data['has_more']

    page += 1
  end

  notes
end