Class: PaperlessService::Evernote

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

Instance Method Summary collapse

Constructor Details

#initializeEvernote

Returns a new instance of Evernote.



13
14
15
16
# File 'lib/paperless/services/evernote.rb', line 13

def initialize
    @app = app(PaperlessService::EVERNOTE)
    @app.activate
end

Instance Method Details

#create(options) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/paperless/services/evernote.rb', line 18

def create(options)
  destination = options[:destination]
  date        = options[:date]
  from_file   = options[:file]
  title       = options[:title] || File.basename(from_file)
  tags        = options[:tags]
  text_ext    = options[:text_ext]

  create_options = { :created => date }
  file_ext = File.extname(from_file)
  file_dir = File.dirname(from_file)
  file_name = File.basename(from_file)

  if file_name != title
    new_filename = File.join(file_dir, title + file_ext)
    File.rename(from_file, new_filename)
    from_file = new_filename
  end

  if text_ext.index file_ext.gsub!(/\./,'')
    puts "Adding text note into Evernote"
    create_options[:with_text] = File.open(from_file, "rb") {|io| io.read}
  else
    if file_ext.match(/md$/i)
      # If this is a mardown file insert it into Evernote as html
      puts "Converting Markdown to HTML"
      text = File.open(from_file, "rb") {|io| io.read}
      create_options[:with_html] = Markdown.new(text).to_html
    else
      # Create a note from a file and let Evernote choose how to attach the file
      puts "Adding note into Evernote"
      create_options[:from_file] = MacTypes::FileURL.path(from_file)
    end
  end

  create_options[:tags] = tags if tags.length > 0
  create_options[:notebook] = destination if destination

  @app.create_note(create_options)

  if options[:delete]
    FileUtils.rm from_file, :force => true
  end

  @app.synchronize
end