aboutsummaryrefslogtreecommitdiffstats
path: root/app/services/evernote.rb
blob: 22703b5563d57548f0f991f6e33ba0edc002c6d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# 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


# ==============================================================================
# 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 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 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