# -*- coding: utf-8 -*- require 'log4r/outputter/evernoteoutputter' require 'evernote_oauth' module Log4ever VERSION = '0.1.1' class TypeError < StandardError; end module ShiftAge DAILY = 1 WEEKLY = 2 MONTHLY = 3 end class Evernote @@note_store = nil def initialize(auth_token, is_sandbox = false) if @@note_store.nil? @@auth_token = auth_token @@note_store = EvernoteOAuth::Client.new({ :token => auth_token, :sandbox => is_sandbox }).note_store end end # get registered notebook or create new notebook # search the notebook under the stack if stack_name specific def notebook Notebook.new end # get registered note or create new note def note(notebook) Note.new(notebook) end def tag(note) Tag.new(note) end # encode for evernote internal charset def to_ascii(str) str.force_encoding("ASCII-8BIT") unless str.nil? end end class Notebook < Evernote def initialize; 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 = @@note_store.listNotebooks(@@auth_token) if @notebooks.nil? @notebook = nil @notebook_name = notebook_name @stack_name = stack_name @notebooks.each do |notebook| notebook_name = to_ascii(notebook_name) stack_name = to_ascii(stack_name) 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 create(notebook_name, stack_name) if @notebook.nil? @notebook end # get newest notebook def get!(notebook_name, stack_name = nil) clear get(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 = @@note_store.createNotebook(@@auth_token, notebook) Log4r::Logger.log_internal { "Create notebook: #{stack_name}/#{notebook_name}" } @notebook rescue => e Log4r::Logger.log_internal { e.message } nil end end # notebook guid def guid; @notebook.guid end # clear notebook object def clear @notebooks = @@note_store.listNotebooks(@@auth_token) @notebook = nil end end class Note < Evernote XML_TEMPLATE_BYTE = 237 def initialize(notebook) return unless @params.nil? || @params.empty? @params = {} @notebook = notebook #@tag = Tag.new if !@notebook.kind_of? ::Evernote::EDAM::Type::Notebook raise TypeError, "Expected kind of Notebook, got #{@notebook.class}", caller elsif !@notebook.respond_to? 'guid' raise NoMethodError, "#{@notebook.class} do not has method: guid", caller end end # content size def size content.bytesize > 0 ? content.bytesize - XML_TEMPLATE_BYTE : 0 end # note guid def guid; @note.guid end # set new title def title=(str) @params[:title] = to_ascii(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 = "