Class: K2e::Note

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

Constant Summary collapse

OFFSET =
0
MAX_RESULT_NUM =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, content:, notebook_name:) ⇒ Note

Returns a new instance of Note.



22
23
24
25
26
27
# File 'lib/k2e/evernote.rb', line 22

def initialize title:, content:, notebook_name:
  @note_store = K2e::EvernoteClient.new.note_store
  @title = title
  @content = content
  @notebook = find_notebook notebook_name
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



17
18
19
# File 'lib/k2e/evernote.rb', line 17

def content
  @content
end

#note_storeObject (readonly)

Returns the value of attribute note_store.



17
18
19
# File 'lib/k2e/evernote.rb', line 17

def note_store
  @note_store
end

#notebookObject (readonly)

Returns the value of attribute notebook.



17
18
19
# File 'lib/k2e/evernote.rb', line 17

def notebook
  @notebook
end

#titleObject (readonly)

Returns the value of attribute title.



17
18
19
# File 'lib/k2e/evernote.rb', line 17

def title
  @title
end

Instance Method Details

#updateObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/k2e/evernote.rb', line 29

def update
  new_note = Evernote::EDAM::Type::Note.new

  new_note.title = title

  filter = Evernote::EDAM::NoteStore::NoteFilter.new words: title
  filter.notebookGuid = notebook.guid
  found_note = note_store.findNotes(K2e::EvernoteClient.new.token, filter, OFFSET, MAX_RESULT_NUM).notes.first

  if found_note&.title == new_note.title
    full_note_content = note_store.getNote(K2e::EvernoteClient.new.token, found_note.guid, true, true, false, false)
    new_note.guid = found_note.guid
    new_note.content = full_note_content.content.gsub /<\/en-note>/ , "#{content}</en-note>"
    note_store.updateNote(K2e::EvernoteClient.new.token, new_note)
  else
    new_note.content = format_xml content
    new_note.notebookGuid = notebook.guid
    note_store.createNote new_note
  end

  new_note
rescue Evernote::EDAM::Error::EDAMUserException => e
  puts "EDAMUserException: #{e}"
end