Class: EvernoteEditor::Editor
- Inherits:
-
Object
- Object
- EvernoteEditor::Editor
- Defined in:
- lib/evernote_editor/editor.rb
Constant Summary collapse
- CONFIGURATION_FILE =
File.("~/.evned")
Instance Attribute Summary collapse
-
#configuration ⇒ Object
Returns the value of attribute configuration.
Instance Method Summary collapse
-
#blocking_flag ⇒ Object
Patterned from Pry.
- #configure ⇒ Object
- #create_note ⇒ Object
- #edit_note ⇒ Object
- #graceful_failure(markdown) ⇒ Object
-
#initialize(*args, opts) ⇒ Editor
constructor
A new instance of Editor.
- #invoke_editor(initial_content = "") ⇒ Object
- #note_markdown(markup) ⇒ Object
- #note_markup(markdown) ⇒ Object
- #open_editor(file_path) ⇒ Object
- #run ⇒ Object
- #search_notes(term = '') ⇒ Object
- #store_editor ⇒ Object
- #store_key ⇒ Object
- #write_configuration ⇒ Object
Constructor Details
#initialize(*args, opts) ⇒ Editor
Returns a new instance of Editor.
16 17 18 19 20 21 22 23 |
# File 'lib/evernote_editor/editor.rb', line 16 def initialize(*args, opts) @title = args.flatten[0] || "" @tags = (args.flatten[1] || '').split(',') @options = opts @sandbox = opts[:sandbox] @mkdout = Redcarpet::Markdown.new(Redcarpet::Render::XHTML, autolink: true, space_after_headers: true, no_intra_emphasis: true) end |
Instance Attribute Details
#configuration ⇒ Object
Returns the value of attribute configuration.
14 15 16 |
# File 'lib/evernote_editor/editor.rb', line 14 def configuration @configuration end |
Instance Method Details
#blocking_flag ⇒ Object
Patterned from Pry
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/evernote_editor/editor.rb', line 155 def blocking_flag case File.basename(@configuration['editor']) when /^[gm]vim/ '--nofork' when /^jedit/ '-wait' when /^mate/, /^subl/ '-w' end end |
#configure ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/evernote_editor/editor.rb', line 30 def configure FileUtils.touch(CONFIGURATION_FILE) unless File.exist?(CONFIGURATION_FILE) #@configuration = YAML::load(File.open(CONFIGURATION_FILE)) || {} @configuration = JSON::load(File.open(CONFIGURATION_FILE)) || {} #@configuration = JSON.parse( IO.read(CONFIGURATION_FILE) || {} ) store_key unless @configuration['token'] store_editor unless @configuration['editor'] end |
#create_note ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/evernote_editor/editor.rb', line 39 def create_note markdown = invoke_editor begin evn_client = EvernoteOAuth::Client.new(token: @configuration['token'], sandbox: @sandbox) note_store = evn_client.note_store note = Evernote::EDAM::Type::Note.new note.title = @title.empty? ? "Untitled note" : @title note.tagNames = @tags unless @tags.empty? note.content = note_markup(markdown) created_note = note_store.createNote(@configuration['token'], note) say "Successfully created new note '#{created_note.title}'" rescue Evernote::EDAM::Error::EDAMSystemException, Evernote::EDAM::Error::EDAMUserException, Evernote::EDAM::Error::EDAMNotFoundException => e say "Sorry, an error occurred saving the note to Evernote (#{e.})" graceful_failure(markdown) end end |
#edit_note ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/evernote_editor/editor.rb', line 67 def edit_note found_notes = search_notes(@title) return unless found_notes if found_notes.empty? say "No notes were found matching '#{@title}'" return end choice = choose do || .prompt = "Which note would you like to edit:" found_notes.each do |n| .choice("#{Time.at(n.updated/1000).strftime('%Y-%m-%d %H:%M')} #{n.title}") do n.guid end end .choice("None") { nil } end return if choice.nil? begin evn_client = EvernoteOAuth::Client.new(token: @configuration['token'], sandbox: @sandbox) note_store = evn_client.note_store note = note_store.getNote(@configuration['token'], choice, true, true, false, false) rescue Evernote::EDAM::Error::EDAMSystemException, Evernote::EDAM::Error::EDAMUserException, Evernote::EDAM::Error::EDAMNotFoundException => e say "Sorry, an error occurred communicating with Evernote (#{e.})" return end markdown = invoke_editor(note_markdown(note.content)) note.content = note_markup(markdown) note.updated = Time.now.to_i * 1000 begin note_store.updateNote(@configuration['token'], note) say "Successfully updated note '#{note.title}'" rescue Evernote::EDAM::Error::EDAMSystemException, Evernote::EDAM::Error::EDAMUserException, Evernote::EDAM::Error::EDAMNotFoundException => e say "Sorry, an error occurred saving the note to Evernote (#{e.})" graceful_failure(markdown) end end |
#graceful_failure(markdown) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/evernote_editor/editor.rb', line 58 def graceful_failure(markdown) say "Here's the markdown you were trying to save:" say "" say "--BEGIN--" say markdown say "--END--" say "" end |
#invoke_editor(initial_content = "") ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/evernote_editor/editor.rb', line 138 def invoke_editor(initial_content = "") file = Tempfile.new(['evned', '.markdown']) file.puts(initial_content) file.flush file.close(false) open_editor(file.path) content = File.read(file.path) file.unlink content end |
#note_markdown(markup) ⇒ Object
134 135 136 |
# File 'lib/evernote_editor/editor.rb', line 134 def note_markdown(markup) ReverseMarkdown.parse markup end |
#note_markup(markdown) ⇒ Object
130 131 132 |
# File 'lib/evernote_editor/editor.rb', line 130 def note_markup(markdown) "<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE en-note SYSTEM 'http://xml.evernote.com/pub/enml2.dtd'><en-note>#{@mkdout.render(markdown)}</en-note>" end |
#open_editor(file_path) ⇒ Object
149 150 151 152 |
# File 'lib/evernote_editor/editor.rb', line 149 def open_editor(file_path) cmd = [@configuration['editor'], blocking_flag, file_path].join(' ') system(cmd) or raise SystemCallError, "`#{cmd}` gave exit status: #{$?.exitstatus}" end |
#run ⇒ Object
25 26 27 28 |
# File 'lib/evernote_editor/editor.rb', line 25 def run configure @options[:edit] ? edit_note : create_note end |
#search_notes(term = '') ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/evernote_editor/editor.rb', line 114 def search_notes(term = '') begin evn_client = EvernoteOAuth::Client.new(token: @configuration['token'], sandbox: @sandbox) note_store = evn_client.note_store note_filter = Evernote::EDAM::NoteStore::NoteFilter.new note_filter.words = term results = note_store.findNotes(@configuration['token'], note_filter, 0, 10).notes rescue Evernote::EDAM::Error::EDAMSystemException, Evernote::EDAM::Error::EDAMUserException, Evernote::EDAM::Error::EDAMNotFoundException => e say "Sorry, an error occurred communicating with Evernote (#{e.inspect})" false end end |
#store_editor ⇒ Object
175 176 177 178 179 |
# File 'lib/evernote_editor/editor.rb', line 175 def store_editor editor_command = ask("Please enter the editor command you would like to use: ") { |q| q.default = `which vim`.strip.chomp } @configuration['editor'] = editor_command write_configuration end |
#store_key ⇒ Object
167 168 169 170 171 172 173 |
# File 'lib/evernote_editor/editor.rb', line 167 def store_key say "You will need a developer token to use this editor." say "More information: http://dev.evernote.com/start/core/authentication.php#devtoken" token = ask("Please enter your developer token: ") { |q| q.default = "none" } @configuration['token'] = token write_configuration end |
#write_configuration ⇒ Object
181 182 183 184 185 |
# File 'lib/evernote_editor/editor.rb', line 181 def write_configuration File.open(CONFIGURATION_FILE, "w") do |file| file.write @configuration.to_json end end |