# -*- coding: utf-8 -*- require 'log4r/outputter/evernoteoutputter' require 'evernote_oauth' module Log4ever VERSION = '0.1.5' class EvernoteError < StandardError; end module ShiftAge DAILY = 1 WEEKLY = 2 MONTHLY = 3 end class EvernoteAuth attr_reader :auth_token attr_reader :note_store def initialize(auth_token, is_sandbox = false) @auth_token = auth_token @note_store = EvernoteOAuth::Client.new({ :token => auth_token, :sandbox => is_sandbox }).note_store end end class Evernote @@auth_store = nil # Execute authentication to evernote def initialize(auth_token, is_sandbox = false) if @@auth_store.nil? @@auth_store = EvernoteAuth.new(auth_token, is_sandbox) end end # get registered notebook or create new notebook # search the notebook under the stack if stack_name specific def notebook @notebook = Notebook.new(@@auth_store) if @notebook.nil? @notebook end # get registered note or create new note def note(notebook) @note = Note.new(notebook, @@auth_store) if @note.nil? @note end # get registered tag or create new tag def tag(note) @tag = Tag.new(note, @@auth_store) if @tag.nil? @tag end end class Notebook # constructor def initialize(auth_store) @auth_store = auth_store end # get notebook def get(notebook_name, stack_name = nil) # return cache if same notebook and stack return @notebook if @notebook_name == notebook_name && @stack_name == stack_name # get notebook list from evernote @notebooks = @auth_store.note_store.listNotebooks(@auth_store.auth_token) if @notebooks.nil? @notebook = nil @notebook_name = notebook_name @stack_name = stack_name @notebooks.each do |notebook| if notebook.name == notebook_name && notebook.stack == stack_name Log4r::Logger.log_internal { "Get notebook: #{stack_name}/#{notebook_name}" } @notebook = notebook break end end # create new notebook if notebook is nil @notebook || create(notebook_name, stack_name) end # create notebook def create(notebook_name, stack_name = nil) notebook = ::Evernote::EDAM::Type::Notebook.new notebook.name = notebook_name notebook.stack = stack_name begin @notebook = @auth_store.note_store.createNotebook(@auth_store.auth_token, notebook) Log4r::Logger.log_internal { "Create notebook: #{stack_name}/#{notebook_name}" } @notebook rescue => e Log4r::Logger.log_internal { e.message } raise EvernoteError, "Create notebook failed. Probably, already exists notebook of same name.", caller if @notebook.nil? end end # notebook guid def guid; @notebook.guid end # clear notebook object def clear @notebooks = @auth_store.note_store.listNotebooks(@auth_store.auth_token) @notebook = nil end end class Note def initialize(notebook, auth_store) return unless @params.nil? || @params.empty? @params = {} if !notebook.kind_of? ::Evernote::EDAM::Type::Notebook raise EvernoteError, "Expected kind of Notebook, got #{notebook.class}", caller elsif !notebook.respond_to? 'guid' raise NoMethodError, "#{notebook.class} do not has method: guid", caller end @notebook = notebook @auth_store = auth_store end # content size def size content.bytesize end # note guid def guid; @note.guid end # set new title def title=(str) @params[:title] = str end # get tag's guid list def tags get.tagGuids end # set tags def tags=(tagGuids) @params[:tagGuids] = tagGuids end # append content def addContent(text) new_html = "