Class: Everdone::Sync

Inherits:
Object
  • Object
show all
Defined in:
lib/everdone/sync.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, todoist) ⇒ Sync

Returns a new instance of Sync.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/everdone/sync.rb', line 11

def initialize(config, todoist)
    @config = config
    @todoist = todoist
    @data = {}
    @current_item_id = nil
    items = @todoist.get_items_by_project_name(@config.todoist_sync_tracking_project)
    raise "Too many items in Todoist project used for syncing (#{@config.todoist_sync_tracking_project}): #{items.length}" if items.length > 1
    if items.length == 0 then
        initData()
    else
        @current_item_id = items[0]['id']
        notes = @todoist.get_notes(@current_item_id)
        unmarshall(notes[0]['content']) if notes and notes.length > 0
    end
end

Instance Method Details

#add_to_processed(new_processed) ⇒ Object



50
51
52
# File 'lib/everdone/sync.rb', line 50

def add_to_processed(new_processed)
    @data['processed'].push(new_processed)
end

#close(new_processed) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/everdone/sync.rb', line 36

def close(new_processed)
    @data['processed'].concat(new_processed)
    new_item_id = @todoist.add_item_to_project_by_name(@config.todoist_sync_tracking_project, 
        "Evernote sync data from #{DateTime.now.to_s} total processed = #{self.get_processed_total()}",
        1)
    new_data = self.marshall
    @todoist.add_note(new_item_id, new_data)
    @todoist.delete_item_by_id(@current_item_id) if @current_item_id
end

#get_processed_totalObject



46
47
48
# File 'lib/everdone/sync.rb', line 46

def get_processed_total()
    return @data['processed'].length
end

#init_dataObject



27
28
29
30
# File 'lib/everdone/sync.rb', line 27

def init_data
    @data['created'] = DateTime.now
    @data['processed'] = []
end

#is_already_processed(item_id) ⇒ Object



32
33
34
# File 'lib/everdone/sync.rb', line 32

def is_already_processed(item_id)
    return  !@data['processed'].find_index(item_id).nil?
end

#marshallObject



54
55
56
57
# File 'lib/everdone/sync.rb', line 54

def marshall
    data = JSON.generate(@data)
    return data
end

#unmarshall(data) ⇒ Object



59
60
61
# File 'lib/everdone/sync.rb', line 59

def unmarshall(data)
    @data = JSON.parse(data)
end