Module: AnkiConnect::Client::Notes
- Included in:
- AnkiConnect::Client
- Defined in:
- lib/anki_connect/notes.rb
Overview
Methods to create, update, query, and manage notes (which generate cards).
Instance Method Summary collapse
-
#add_note(deck_name:, note_type_name:, fields:, tags: [], media: nil, options: nil) ⇒ Integer
Creates a new note.
-
#add_notes(notes) ⇒ Array<Integer>
Creates multiple notes.
-
#add_tags(note_ids, tags) ⇒ nil
Adds tags to notes.
-
#change_note_type(id, note_type_name:, fields:, tags:) ⇒ nil
Changes a note's note type.
-
#clear_unused_tags ⇒ nil
Removes unused tags from collection.
-
#delete_notes(note_ids) ⇒ nil
Deletes notes and all associated cards.
-
#note_addability(note) ⇒ Hash
Gets addability details for one note.
-
#note_addability_details(notes) ⇒ Array<Hash>
Gets addability details for each note, preserving input order.
-
#note_addability_statuses(notes) ⇒ Array<Boolean>
Gets addability status for each note, preserving input order.
-
#note_addable?(note) ⇒ Boolean
Checks if one note can be added.
-
#note_modification_times(note_ids) ⇒ Array<Hash>
Gets modification times for notes.
-
#note_tags(note_id) ⇒ Array<String>
Gets tags for a note.
-
#notes(note_ids: nil, query: nil) ⇒ Array<Hash>
Gets detailed information about notes.
-
#remove_tags(note_ids, tags) ⇒ nil
Removes tags from notes.
-
#remove_unused_note_types ⇒ nil
Removes note types that are not used by any notes.
-
#replace_tag(from:, to:, note_ids: nil) ⇒ nil
Replaces a tag with another.
-
#search_notes(query) ⇒ Array<Integer>
Searches for notes matching a query.
-
#tags ⇒ Array<String>
Gets all tags in collection.
-
#update_note(id, fields: nil, tags: nil, media: nil) ⇒ nil
Updates a note's fields, tags, or media.
-
#update_note_fields(id, fields:, media: nil) ⇒ nil
Updates a note's fields and optional media.
-
#update_note_tags(id, tags) ⇒ nil
Replaces all tags on a note.
Instance Method Details
#add_note(deck_name:, note_type_name:, fields:, tags: [], media: nil, options: nil) ⇒ Integer
Creates a new note.
16 17 18 19 20 21 |
# File 'lib/anki_connect/notes.rb', line 16 def add_note(deck_name:, note_type_name:, fields:, tags: [], media: nil, options: nil) note = { deck_name: deck_name, note_type_name: note_type_name, fields: fields, tags: } note[:media] = media unless media.nil? note[:options] = unless .nil? request(:addNote, note: normalize_note(note)) end |
#add_notes(notes) ⇒ Array<Integer>
Creates multiple notes.
The operation is all-or-nothing; AnkiConnect rolls back all additions if any note fails.
29 30 31 |
# File 'lib/anki_connect/notes.rb', line 29 def add_notes(notes) request(:addNotes, notes: notes.map { |note| normalize_note(note) }) end |
#add_tags(note_ids, tags) ⇒ nil
Adds tags to notes.
131 132 133 |
# File 'lib/anki_connect/notes.rb', line 131 def (note_ids, ) request(:addTags, notes: note_ids, tags: normalize_tag_query()) end |
#change_note_type(id, note_type_name:, fields:, tags:) ⇒ nil
Changes a note's note type.
All fields not supplied are cleared when the note type changes.
114 115 116 |
# File 'lib/anki_connect/notes.rb', line 114 def change_note_type(id, note_type_name:, fields:, tags:) request(:updateNoteModel, note: { id: id, modelName: note_type_name, fields: fields, tags: }) end |
#clear_unused_tags ⇒ nil
Removes unused tags from collection.
154 155 156 |
# File 'lib/anki_connect/notes.rb', line 154 def request(:clearUnusedTags) end |
#delete_notes(note_ids) ⇒ nil
Deletes notes and all associated cards.
206 207 208 |
# File 'lib/anki_connect/notes.rb', line 206 def delete_notes(note_ids) request(:deleteNotes, notes: note_ids) end |
#note_addability(note) ⇒ Hash
Gets addability details for one note.
45 46 47 |
# File 'lib/anki_connect/notes.rb', line 45 def note_addability(note) request(:canAddNoteWithErrorDetail, note: normalize_note(note)) end |
#note_addability_details(notes) ⇒ Array<Hash>
Gets addability details for each note, preserving input order.
61 62 63 |
# File 'lib/anki_connect/notes.rb', line 61 def note_addability_details(notes) request(:canAddNotesWithErrorDetail, notes: notes.map { |note| normalize_note(note) }) end |
#note_addability_statuses(notes) ⇒ Array<Boolean>
Gets addability status for each note, preserving input order.
53 54 55 |
# File 'lib/anki_connect/notes.rb', line 53 def note_addability_statuses(notes) request(:canAddNotes, notes: notes.map { |note| normalize_note(note) }) end |
#note_addable?(note) ⇒ Boolean
Checks if one note can be added.
37 38 39 |
# File 'lib/anki_connect/notes.rb', line 37 def note_addable?(note) request(:canAddNote, note: normalize_note(note)) end |
#note_modification_times(note_ids) ⇒ Array<Hash>
Gets modification times for notes.
198 199 200 |
# File 'lib/anki_connect/notes.rb', line 198 def note_modification_times(note_ids) request(:notesModTime, notes: note_ids) end |
#note_tags(note_id) ⇒ Array<String>
Gets tags for a note.
122 123 124 |
# File 'lib/anki_connect/notes.rb', line 122 def (note_id) request(:getNoteTags, note: note_id) end |
#notes(note_ids: nil, query: nil) ⇒ Array<Hash>
Gets detailed information about notes.
185 186 187 188 189 190 191 192 |
# File 'lib/anki_connect/notes.rb', line 185 def notes(note_ids: nil, query: nil) unless [note_ids, query].count { |selector| !selector.nil? } == 1 raise ArgumentError, 'provide exactly one of note_ids or query' end params = note_ids.nil? ? { query: query } : { notes: note_ids } request(:notesInfo, **params) end |
#remove_tags(note_ids, tags) ⇒ nil
Removes tags from notes.
140 141 142 |
# File 'lib/anki_connect/notes.rb', line 140 def (note_ids, ) request(:removeTags, notes: note_ids, tags: normalize_tag_query()) end |
#remove_unused_note_types ⇒ nil
Removes note types that are not used by any notes. The upstream action name is misleading; it does not remove notes with empty fields.
214 215 216 |
# File 'lib/anki_connect/notes.rb', line 214 def remove_unused_note_types request(:removeEmptyNotes) end |
#replace_tag(from:, to:, note_ids: nil) ⇒ nil
Replaces a tag with another.
164 165 166 167 168 169 170 |
# File 'lib/anki_connect/notes.rb', line 164 def replace_tag(from:, to:, note_ids: nil) if note_ids request(:replaceTags, notes: note_ids, tag_to_replace: from, replace_with_tag: to) else request(:replaceTagsInAllNotes, tag_to_replace: from, replace_with_tag: to) end end |
#search_notes(query) ⇒ Array<Integer>
Searches for notes matching a query.
176 177 178 |
# File 'lib/anki_connect/notes.rb', line 176 def search_notes(query) request(:findNotes, query: query) end |
#tags ⇒ Array<String>
Gets all tags in collection.
147 148 149 |
# File 'lib/anki_connect/notes.rb', line 147 def request(:getTags) end |
#update_note(id, fields: nil, tags: nil, media: nil) ⇒ nil
Updates a note's fields, tags, or media.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/anki_connect/notes.rb', line 72 def update_note(id, fields: nil, tags: nil, media: nil) if fields.nil? && .nil? && media.nil? raise ArgumentError, 'provide fields, tags, or media to update' end note = { id: id } note[:fields] = fields || {} unless fields.nil? && media.nil? note[:tags] = unless .nil? merge_media!(note, media) unless media.nil? request(:updateNote, note: note) end |
#update_note_fields(id, fields:, media: nil) ⇒ nil
Updates a note's fields and optional media.
90 91 92 93 94 |
# File 'lib/anki_connect/notes.rb', line 90 def update_note_fields(id, fields:, media: nil) note = { id: id, fields: fields } merge_media!(note, media) unless media.nil? request(:updateNoteFields, note: note) end |
#update_note_tags(id, tags) ⇒ nil
Replaces all tags on a note.
101 102 103 |
# File 'lib/anki_connect/notes.rb', line 101 def (id, ) request(:updateNoteTags, note: id, tags: ()) end |