Class: EvernoteEditor::Editor

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

Constant Summary collapse

CONFIGURATION_FILE =
File.expand_path("~/.evned")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, opts) ⇒ Editor

Returns a new instance of Editor.



17
18
19
20
21
22
23
24
25
# File 'lib/evernote_editor/editor.rb', line 17

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)
  @notebooks = []
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



15
16
17
# File 'lib/evernote_editor/editor.rb', line 15

def configuration
  @configuration
end

Instance Method Details

#blocking_flagObject

Patterned from Pry



158
159
160
161
162
163
164
165
166
167
# File 'lib/evernote_editor/editor.rb', line 158

def blocking_flag
  case File.basename(@configuration['editor'])
  when /^[gm]vim/
    '--nofork'
  when /^jedit/
    '-wait'
  when /^mate/, /^subl/
    '-w'
  end
end

#configureObject



32
33
34
35
36
37
38
39
# File 'lib/evernote_editor/editor.rb', line 32

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_noteObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/evernote_editor/editor.rb', line 41

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.message})"
    graceful_failure(markdown)
  end
end

#edit_noteObject



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
113
114
# File 'lib/evernote_editor/editor.rb', line 69

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 |menu|
    menu.prompt = "Which note would you like to edit:"
    found_notes.each do |n|
      menu.choice("#{Time.at(n.updated/1000).strftime('%Y %b %d %H:%M')} [#{lookup_notebook_name(n.notebookGuid)}] #{n.title}") do
        n.guid
      end
    end
    menu.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.message})"
    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.message})"
    graceful_failure(markdown)
  end

end

#graceful_failure(markdown) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/evernote_editor/editor.rb', line 60

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



141
142
143
144
145
146
147
148
149
150
# File 'lib/evernote_editor/editor.rb', line 141

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

#lookup_notebook_name(guid) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/evernote_editor/editor.rb', line 190

def lookup_notebook_name(guid)
  if @notebooks.empty?
    begin
      evn_client = EvernoteOAuth::Client.new(token: @configuration['token'], sandbox: @sandbox)
      note_store = evn_client.note_store
      @notebooks = note_store.listNotebooks
    rescue Evernote::EDAM::Error::EDAMSystemException,
           Evernote::EDAM::Error::EDAMUserException,
           Evernote::EDAM::Error::EDAMNotFoundException => e
      return "unknown notebook"
    end
  end
  @notebooks.select {|n| n.guid == guid}.first.name
end

#note_markdown(markup) ⇒ Object



136
137
138
139
# File 'lib/evernote_editor/editor.rb', line 136

def note_markdown(markup)
  markup = Sanitize.clean(markup, Sanitize::Config::RELAXED)
  ReverseMarkdown.convert markup.strip
end

#note_markup(markdown) ⇒ Object



132
133
134
# File 'lib/evernote_editor/editor.rb', line 132

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



152
153
154
155
# File 'lib/evernote_editor/editor.rb', line 152

def open_editor(file_path)
  cmd = [@configuration['editor'], blocking_flag, file_path].join(' ')
  system(cmd) or raise SystemCallError, "`#{cmd}` gave exit status: #{$?.exitstatus}"
end

#runObject



27
28
29
30
# File 'lib/evernote_editor/editor.rb', line 27

def run
  configure
  @options[:edit] ? edit_note : create_note
end

#search_notes(term = '') ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/evernote_editor/editor.rb', line 116

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_editorObject



178
179
180
181
182
# File 'lib/evernote_editor/editor.rb', line 178

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_keyObject



170
171
172
173
174
175
176
# File 'lib/evernote_editor/editor.rb', line 170

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_configurationObject



184
185
186
187
188
# File 'lib/evernote_editor/editor.rb', line 184

def write_configuration
  File.open(CONFIGURATION_FILE, "w") do |file|
    file.write @configuration.to_json
  end
end