diff options
| author | Kip Cole | 2012-10-04 22:02:50 +0800 |
|---|---|---|
| committer | Kip Cole | 2012-10-04 22:02:50 +0800 |
| commit | 2f0967708d34e4dd1e3c7d4c4793489e3920eeaa (patch) | |
| tree | 00c6d1a7c96fae6892b6dd22cf8f9ea25e05e521 | |
| parent | b69494646c7042f85530f8194da448740e967a73 (diff) | |
| download | evernote-2f0967708d34e4dd1e3c7d4c4793489e3920eeaa.tar.bz2 | |
Add ruby idiomatic class wrappers for note store. Still work in progress, but functional
| -rw-r--r-- | lib/evernote/note_store.rb | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/lib/evernote/note_store.rb b/lib/evernote/note_store.rb index 5c8f225..b8005a3 100644 --- a/lib/evernote/note_store.rb +++ b/lib/evernote/note_store.rb @@ -1,5 +1,7 @@ module Evernote 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) @@ -7,9 +9,132 @@ module Evernote @access_token = access_token end + def notebooks(*args) + @notebooks ||= list_notebooks(*args).inject([]) do |books, book| + books.push Notebook.new(self, book) + books + end + 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 notes + @notes || all + end + + def all(rows = max) + @filter = NoteFilter.new + @filter.notebook_guid = notebook.guid + @notes = wrap_notes(notestore.find_notes(filter, offset, rows).notes) + end + + def updated_since(time, rows = max) + @filter = NoteFilter.new + @filter.notebook_guid = notebook.guid + @filter.words = "updated:#{time.strftime(DATE_FORMAT)}" + @notes = wrap_notes(notestore.find_notes(filter, offset, rows).notes) + end + + def created_since(time, rows = max) + @filter = NoteFilter.new + @filter.notebook_guid = notebook.guid + @filter.words = "created:#{time.strftime(DATE_FORMAT)}" + @notes = wrap_notes(notestore.find_notes(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) + notes + end + end + + end + + class Note + attr_reader :notestore, :note + + def initialize(notestore, note) + @notestore = notestore + @note = note + end + + def content(options = :all) + @content ||= notestore.get_note(*args_from(options).unshift(note.guid)) + end + + def enml + content.content + end + + 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 + note.resources || [] + end + + def method_missing(name, *args, &block) + @note.send(name.to_s.camelize(:lower), *args, &block) + end + + private + def args_from(options) + if options == :all + return true, true, false, false + elsif options == :enml + return true, false, false, false + else + return true, false, false, false + end + end + + end + + class NoteFilter + attr_reader :filter + + def initialize + @filter = Evernote::EDAM::NoteStore::NoteFilter.new + end + + def method_missing(name, *args, &block) + @filter.send(name.to_s.camelize(:lower), *args, &block) + end + end + + end |
