Class: EvernoteUploader

Inherits:
Object
  • Object
show all
Includes:
EvernoteUploaderHelper
Defined in:
lib/evernote_uploader.rb,
lib/evernote_uploader/version.rb

Defined Under Namespace

Classes: TokenError

Constant Summary collapse

EvernoteHost =
"www.evernote.com"
EvernoteUrl =
"https://#{EvernoteHost}/edam/user/"
VERSION =
"0.0.3"

Instance Method Summary collapse

Methods included from EvernoteUploaderHelper

#create_note, #detect_mime_type

Constructor Details

#initialize(filenames, options) ⇒ EvernoteUploader

Returns a new instance of EvernoteUploader.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/evernote_uploader.rb', line 17

def initialize filenames, options
  @filenames = filenames
  @options   = options

  check_evernote_token

  evernote_api

  get_notebooks
  check_notebook

  @note = create_note(@options, @filenames, @notebook)
end

Instance Method Details

#check_evernote_tokenObject

Raises:



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/evernote_uploader.rb', line 43

def check_evernote_token
  if @options[:token]
    if File.exist?(File.expand_path(@options[:token]))
      filename = File.expand_path(@options[:token])
      @token = File.open(filename) {|f| f.gets }.chomp
    else
      @token = @options[:token]
    end
  end

  raise TokenError, "Token is not specified properly" unless @token
end

#check_notebookObject



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/evernote_uploader.rb', line 80

def check_notebook
  notebooks = @note_store.listNotebooks(@token)
  note_names = notebooks.map {|nb| nb.name }

  unless note_names.include?(@options[:notebook])
    puts "Note book \"#{@options[:notebook]}\" doesn't exist -- exit"
    exit
  else
    index = note_names.index(@options[:notebook])
    @notebook = notebooks[index]
  end
end

#evernote_apiObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/evernote_uploader.rb', line 56

def evernote_api
  user_store_transport = Thrift::HTTPClientTransport.new(EvernoteUrl)
  user_store_protocol  = Thrift::BinaryProtocol.new(user_store_transport)
  @user_store          =
    Evernote::EDAM::UserStore::UserStore::Client.new(user_store_protocol)

  version_ok =
    @user_store.checkVersion("Evernote EDAMTest (Ruby)",
                            Evernote::EDAM::UserStore::EDAM_VERSION_MAJOR,
                            Evernote::EDAM::UserStore::EDAM_VERSION_MINOR)
  unless version_ok
    puts "Evernote API version not up-to-date"
    exit
  end
end

#get_notebooksObject



72
73
74
75
76
77
78
# File 'lib/evernote_uploader.rb', line 72

def get_notebooks
  note_store_url       = @user_store.getNoteStoreUrl(@token)
  note_store_transport = Thrift::HTTPClientTransport.new(note_store_url)
  note_store_protocol  = Thrift::BinaryProtocol.new(note_store_transport)
  @note_store          =
    Evernote::EDAM::NoteStore::NoteStore::Client.new(note_store_protocol)
end

#uploadObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/evernote_uploader.rb', line 31

def upload
  puts "uploading #{@filenames.join(", ")}"
  puts "Notebook: #{@notebook.name}"
  puts "Title: #{@note.title}"
  puts "tags #{@options[:tags]}"
  puts "..."

  @note_store.createNote(@token, @note)

  puts "done!"
end