From e129dc5b3f70005e8b33a62f66e673a9c0d8084c Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 30 Dec 2015 18:41:49 -0800 Subject: Include 'kipcole9/evernote' note_store.rb directly Copy 'note_store.rb' from 'kipcole9/evernote' directly into my Evernote service file. I was getting a `LoadError` when trying to load the 'evernote' gem so to get moving quickly I'm just including the code directly in this app. We can figure out the issue with the gem later. Updating `NotesController` to interface with the 'evernote' gem classes and get notebooks using its API. --- Gemfile | 2 +- Gemfile.lock | 12 -- app/controllers/notes_controller.rb | 9 +- app/services/evernote.rb | 240 +++++++++++++++++++++++++++++++++--- 4 files changed, 232 insertions(+), 31 deletions(-) diff --git a/Gemfile b/Gemfile index 6aa946a..f218f0c 100644 --- a/Gemfile +++ b/Gemfile @@ -39,7 +39,7 @@ gem 'omniauth-evernote' gem 'evernote-thrift' -gem 'evernote', '1.3.0', :github => 'kipcole9/evernote' +# gem 'evernote', '1.3.0', :github => 'kipcole9/evernote' group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console diff --git a/Gemfile.lock b/Gemfile.lock index bf6ddb7..5d9f4fe 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,11 +1,3 @@ -GIT - remote: git://github.com/kipcole9/evernote.git - revision: 6395982f491e1278ee9cfdea5de006e229d25347 - specs: - evernote (1.3.0) - activesupport - thrift_client (>= 0.9.1) - GEM remote: https://rubygems.org/ specs: @@ -145,9 +137,6 @@ GEM sqlite3 (1.3.11) thor (0.19.1) thread_safe (0.3.5) - thrift (0.9.3.0) - thrift_client (0.9.3) - thrift (~> 0.9.0) tilt (2.0.1) turbolinks (2.5.3) coffee-rails @@ -168,7 +157,6 @@ PLATFORMS DEPENDENCIES byebug coffee-rails (~> 4.1.0) - evernote (= 1.3.0)! evernote-thrift jbuilder (~> 2.0) jquery-rails diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb index bf7f5ab..6b40ca3 100644 --- a/app/controllers/notes_controller.rb +++ b/app/controllers/notes_controller.rb @@ -5,9 +5,14 @@ class NotesController < ApplicationController auth_token = 'S=s1:U=91e2f:E=151f92661a2:C=151f4000220:P=185:A=evernotesandbox199:V=2:H=5777502290baf1ae1b36ad6254592258' notestore_url = 'https://sandbox.evernote.com/shard/s1/notestore' - e = EvernoteService.new(auth_token, notestore_url) - @notebooks = e.notebooks + # e = EvernoteService.new(auth_token, notestore_url) + # @notebooks = e.notebooks # @notes = @notebooks.collect { |b| e.notes_from_notebook(b) } + + access_token = auth_token + note_store = EvernoteService::NoteStore.new(notestore_url, access_token) + + @notebooks = note_store.list_notebooks end end diff --git a/app/services/evernote.rb b/app/services/evernote.rb index 75f72aa..22703b5 100644 --- a/app/services/evernote.rb +++ b/app/services/evernote.rb @@ -1,23 +1,231 @@ -class EvernoteService - def initialize(auth_token, notestore_url) - @auth_token = auth_token - @notestore_url = notestore_url +# class EvernoteService +# def initialize(auth_token, notestore_url) +# @auth_token = auth_token +# @notestore_url = notestore_url +# +# noteStoreTransport = Thrift::HTTPClientTransport.new(@notestore_url) +# noteStoreProtocol = Thrift::BinaryProtocol.new(noteStoreTransport) +# @note_store = Evernote::EDAM::NoteStore::NoteStore::Client.new(noteStoreProtocol) +# end +# +# def notebooks +# @note_store.listNotebooks(@auth_token) +# end +# +# def notes_from_notebook(notebook) +# @note_store.findNotesMetadata( +# @auth_token, +# Evernote::EDAM::NoteStore::NoteFilter.new(:notebookGuid => notebook.guid), +# 0, +# 50, +# ) +# end +# end - noteStoreTransport = Thrift::HTTPClientTransport.new(@notestore_url) - noteStoreProtocol = Thrift::BinaryProtocol.new(noteStoreTransport) - @note_store = Evernote::EDAM::NoteStore::NoteStore::Client.new(noteStoreProtocol) + +# ============================================================================== +# Copyright (c) 2011 Chris Sepic +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +# +module EvernoteService + class NoteStore + attr_reader :access_token, :notestore + + def initialize(notestore_url, access_token) + notestore_transport = Thrift::HTTPClientTransport.new(notestore_url) + notestore_protocol = Thrift::BinaryProtocol.new(notestore_transport) + @notestore = Evernote::EDAM::NoteStore::NoteStore::Client.new(notestore_protocol) + @access_token = access_token + end + + def notebooks(*args) + @notebooks ||= list_notebooks(*args).inject([]) do |books, book| + books.push Notebook.new(self, book) + end + end + + def update_count + sync_state.updateCount + end + + def sync_state + @sync_state ||= self.get_sync_state + end + + # Camelize the method names for ruby consistency and push the access_token to the front of the args array + def method_missing(name, *args, &block) + @notestore.send(name.to_s.camelize(:lower), *(args.unshift(@access_token)), &block) + end end + + class Notebook + attr_reader :notestore, :notebook, :offset, :max, :filter + DATE_FORMAT = '%Y%m%dT%H%M%S%z' + + def initialize(notestore, notebook) + @notestore = notestore + @notebook = notebook + @offset = 0 + @max = 100 + end - def notebooks - @note_store.listNotebooks(@auth_token) + def notes + @notes || all + end + + # TODO turn filter params into options + # where(:created => 'since', :updated => 'since', :words => '.....') + # ie. mimic the AR interface as much as practical + def all(rows = max) + @filter = NoteFilter.new + @filter.notebook_guid = notebook.guid + @notes = wrap_notes(notestore.find_notes(filter.filter, offset, rows).notes) + end + + def updated_since(time, rows = max) + @filter = NoteFilter.new(:notebook_guid => notebook.guid, :words => "updated:#{time.strftime(DATE_FORMAT)}") + @notes = wrap_notes(notestore.find_notes(filter.filter, offset, rows).notes) + end + + def created_since(time, rows = max) + @filter = NoteFilter.new(:notebook_guid => notebook.guid, :words => "created:#{time.strftime(DATE_FORMAT)}") + @notes = wrap_notes(notestore.find_notes(filter.filter, offset, rows).notes) + end + + def method_missing(name, *args, &block) + @notebook.send(name.to_s.camelize(:lower), *args, &block) + end + + private + def wrap_notes(notes) + notes.inject([]) do |notes, note| + notes.push Note.new(notestore, note) + end + end + end + + class Note + attr_reader :notestore, :note + + def initialize(notestore, note) + @notestore = notestore + @note = note + end + + def content(options = :enml) + @content ||= content!(options) + end + + def content!(options = :enml) + @content = notestore.get_note(*args_from(options).unshift(note.guid)) + end + + def enml + content.content + end - def notes_from_notebook(notebook) - @note_store.findNotesMetadata( - @auth_token, - Evernote::EDAM::NoteStore::NoteFilter.new(:notebookGuid => notebook.guid), - 0, - 50, - ) + def created + @created ||= Time.at(note.created / 1000) + end + + def updated + @updated ||= Time.at(note.updated / 1000) + end + + def latitude + note.attributes.latitude + end + + def longitude + note.attributes.longitude + end + + def resources + @resources ||= note.resources.inject([]) do |resources, resource| + resources.push Resource.new(notestore, resource) + end rescue [] + end + + def tags + @tags ||= notestore.get_note_tag_names(note.guid) + end + + def method_missing(name, *args, &block) + @note.send(name.to_s.camelize(:lower), *args, &block) + end + + private + # Arguments are with_data, with_recognition, with_attributes, with_alternate_data + def args_from(options) + options = :enml unless [:enml, :all].include?(options) + case options + when :all + [true, true, false, false] + when :enml + [true, false, false, false] + end + end + end + + class Resource + attr_reader :notestore, :resource + WITH_DATA = true + WITH_RECOGNITION = false + WITH_ATTRIBUTES = false + WITH_ALTERNATE_DATA = false + + def initialize(notestore, resource) + @notestore = notestore + @resource = resource + end + + def body + @body ||= resource.data.body || + notestore.get_resource(self.guid, WITH_DATA, WITH_RECOGNITION, WITH_ATTRIBUTES, WITH_ALTERNATE_DATA).data.body + end + + def mime_type + defined?(Mime::Type) ? Mime::Type.lookup(self.mime) : self.mime + end + + def method_missing(name, *args, &block) + @resource.send(name.to_s.camelize(:lower), *args, &block) + end + end + + class NoteFilter + attr_reader :filter + + def initialize(options = {}) + @filter = Evernote::EDAM::NoteStore::NoteFilter.new + options.each do |method, value| + send "#{method}=", value + end + end + + def method_missing(name, *args, &block) + @filter.send(name.to_s.camelize(:lower), *args, &block) + end end + + end -- cgit v1.2.3